Calling global function causing linking error.
-
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.
-
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.
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 ?
-
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 ?
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.
-
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.
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.
-
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.
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] -
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.
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
-
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]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.
-
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
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++ -
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.
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
-
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.
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...
-
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...
I have done exactly the same. But inspite of this, I keep getting the same link error. I am using VC++ project.
-
I have done exactly the same. But inspite of this, I keep getting the same link error. I am using VC++ project.
Problem solved. It was the exact problem that you guys had rigthly pointed out. Thanks a lot. :)