Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calling global function causing linking error.

Calling global function causing linking error.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structurestoolshelp
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Comp_Users
    wrote on last edited by
    #1

    Hi, In my project, I have an .cpp file which contains a set of utility functions like TrimMyString() etc which could be called a lot of times. I have made these functions global and included the .cpp file in places where I call these utility functions. When I call these global methods from another function in my user defined class (say I want to trim a character array in one of my user defined class functions), I am getting a linking error "fatal error LNK1169: one or more multiple defined symobls found". However, when I make them global and static by including the static keyword, then this error no longer exists and every thing works perfectly. How do i make this a non-static global function because this is what the current requirment is? Thanks.

    P I 2 Replies Last reply
    0
    • C Comp_Users

      Hi, In my project, I have an .cpp file which contains a set of utility functions like TrimMyString() etc which could be called a lot of times. I have made these functions global and included the .cpp file in places where I call these utility functions. When I call these global methods from another function in my user defined class (say I want to trim a character array in one of my user defined class functions), I am getting a linking error "fatal error LNK1169: one or more multiple defined symobls found". However, when I make them global and static by including the static keyword, then this error no longer exists and every thing works perfectly. How do i make this a non-static global function because this is what the current requirment is? Thanks.

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      Comp_Users wrote:

      I have an .cpp file which contains a set of utility functions like TrimMyString() etc which could be called a lot of times.

      I assume you have declared its prototypes in a header file, and are using it wherever that function in needed , isn't it ?

      C 1 Reply Last reply
      0
      • P prasad_som

        Comp_Users wrote:

        I have an .cpp file which contains a set of utility functions like TrimMyString() etc which could be called a lot of times.

        I assume you have declared its prototypes in a header file, and are using it wherever that function in needed , isn't it ?

        C Offline
        C Offline
        Comp_Users
        wrote on last edited by
        #3

        No. I am not. I just have the global function body in the .cpp file and including this file in places where I am calling it.

        P C 2 Replies Last reply
        0
        • C Comp_Users

          No. I am not. I just have the global function body in the .cpp file and including this file in places where I am calling it.

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          Comp_Users wrote:

          I just have the global function body in the .cpp file and including this file in places where I am calling it.

          You should not include cpp file in other files. Do , what I said in my first post.

          C 1 Reply Last reply
          0
          • C Comp_Users

            No. I am not. I just have the global function body in the .cpp file and including this file in places where I am calling it.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Comp_Users wrote:

            I just have the global function body in the .cpp file and including this file in places where I am calling it.

            :omg: You really need a good C tutorial. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            C 1 Reply Last reply
            0
            • P prasad_som

              Comp_Users wrote:

              I just have the global function body in the .cpp file and including this file in places where I am calling it.

              You should not include cpp file in other files. Do , what I said in my first post.

              C Offline
              C Offline
              Comp_Users
              wrote on last edited by
              #6

              Tried what you asked me to do. Created a new .h file and added the function declarations in the global space in the .h file and in the utility .cpp file, included this newly created .h file. Now in the .cpp file where i am calling these utility functions, I have included the newly added .h file. But the error still persists. These are the exact error messages.

              1>XMLLoader.obj : error LNK2005: "void __cdecl TrimString(wchar_t *)" (?TrimString@@YAXPA_W@Z) already defined in UtilityFunctions.obj

              1>Output/Sample.dll : fatal error LNK1169: one or more multiply defined symbols found

              C 1 Reply Last reply
              0
              • C CPallini

                Comp_Users wrote:

                I just have the global function body in the .cpp file and including this file in places where I am calling it.

                :omg: You really need a good C tutorial. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                C Offline
                C Offline
                Comp_Users
                wrote on last edited by
                #7

                Could you please tell me as to what is causing this to happen? Is it the way I am calling an global function? Pls help.

                D 1 Reply Last reply
                0
                • C Comp_Users

                  Tried what you asked me to do. Created a new .h file and added the function declarations in the global space in the .h file and in the utility .cpp file, included this newly created .h file. Now in the .cpp file where i am calling these utility functions, I have included the newly added .h file. But the error still persists. These are the exact error messages.

                  1>XMLLoader.obj : error LNK2005: "void __cdecl TrimString(wchar_t *)" (?TrimString@@YAXPA_W@Z) already defined in UtilityFunctions.obj

                  1>Output/Sample.dll : fatal error LNK1169: one or more multiply defined symbols found

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  Did you put include guards[^] in your header file ? Otherwise it will be included multiple times.

                  Cédric Moonen Software developer
                  Charting control [v1.5] OpenGL game tutorial in C++

                  1 Reply Last reply
                  0
                  • C Comp_Users

                    Could you please tell me as to what is causing this to happen? Is it the way I am calling an global function? Pls help.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Comp_Users wrote:

                    Could you please tell me as to what is causing this to happen?

                    The linker is telling you exactly what the problem is. You need to put the function declarations in a .h file like:

                    #if !defined(ABC123)
                    #define ABC123

                    #pragma once

                    int TrimMyString();
                    int PaintMyHouse();
                    int SellMyStock();

                    #endif

                    You need to put the function definitions in a .cpp file. Make sure both files are added to the project. Then in any file that will be using those functions, simply #include the .h file near the top.

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    1 Reply Last reply
                    0
                    • C Comp_Users

                      Hi, In my project, I have an .cpp file which contains a set of utility functions like TrimMyString() etc which could be called a lot of times. I have made these functions global and included the .cpp file in places where I call these utility functions. When I call these global methods from another function in my user defined class (say I want to trim a character array in one of my user defined class functions), I am getting a linking error "fatal error LNK1169: one or more multiple defined symobls found". However, when I make them global and static by including the static keyword, then this error no longer exists and every thing works perfectly. How do i make this a non-static global function because this is what the current requirment is? Thanks.

                      I Offline
                      I Offline
                      Iain Clarke Warrior Programmer
                      wrote on last edited by
                      #10

                      Others have beaten you over the head with it, so I'll join in. You DON'T include cpp files - you include headers. Try:

                      // TrimMyString.h
                      extern CString TrimMyString (CString s); // this will be included everywhere you need it. Note the extern.

                      // TrimMyString.cpp
                      #include "stdafx.h" // I assume you're using VC++, in which you'll likely need this
                      #include "TrimMyString.h"

                      CString TrimMyString (CString s)
                      {
                      do stuff with the string...
                      return s;
                      }

                      // somefile.cpp
                      #include "stdafx.h" // I assume you're using VC++, in which you'll likely need this
                      #include #include "Other.h"
                      #include "TrimMyString.h" // <-- the important one!

                      BOOL SomeFunction ()
                      {
                      s = " Hello ";
                      s = TrimMyString (s);
                      return TRUE;
                      }

                      I hope that made a little sense, Iain.

                      Codeproject MVP for C++, I can't believe it's for my lounge posts...

                      C 1 Reply Last reply
                      0
                      • I Iain Clarke Warrior Programmer

                        Others have beaten you over the head with it, so I'll join in. You DON'T include cpp files - you include headers. Try:

                        // TrimMyString.h
                        extern CString TrimMyString (CString s); // this will be included everywhere you need it. Note the extern.

                        // TrimMyString.cpp
                        #include "stdafx.h" // I assume you're using VC++, in which you'll likely need this
                        #include "TrimMyString.h"

                        CString TrimMyString (CString s)
                        {
                        do stuff with the string...
                        return s;
                        }

                        // somefile.cpp
                        #include "stdafx.h" // I assume you're using VC++, in which you'll likely need this
                        #include #include "Other.h"
                        #include "TrimMyString.h" // <-- the important one!

                        BOOL SomeFunction ()
                        {
                        s = " Hello ";
                        s = TrimMyString (s);
                        return TRUE;
                        }

                        I hope that made a little sense, Iain.

                        Codeproject MVP for C++, I can't believe it's for my lounge posts...

                        C Offline
                        C Offline
                        Comp_Users
                        wrote on last edited by
                        #11

                        I have done exactly the same. But inspite of this, I keep getting the same link error. I am using VC++ project.

                        C 1 Reply Last reply
                        0
                        • C Comp_Users

                          I have done exactly the same. But inspite of this, I keep getting the same link error. I am using VC++ project.

                          C Offline
                          C Offline
                          Comp_Users
                          wrote on last edited by
                          #12

                          Problem solved. It was the exact problem that you guys had rigthly pointed out. Thanks a lot. :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups