Linking problem
-
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?
-
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?
Why do you put the definition of your function in a header file ? Usually it is much better practice to move it to a cpp file. Anyway, what you can do to remove your problem is make use of include guards. Surround your code in the file with this (replace MYFILE by something unique, typically the file name):
#ifndef MYFILE
#define MYFILE.... // The rest of the header comes here
#endif
You can also put
#pragma once
at the top of your file. Google for "include guards" for more information.Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Why do you put the definition of your function in a header file ? Usually it is much better practice to move it to a cpp file. Anyway, what you can do to remove your problem is make use of include guards. Surround your code in the file with this (replace MYFILE by something unique, typically the file name):
#ifndef MYFILE
#define MYFILE.... // The rest of the header comes here
#endif
You can also put
#pragma once
at the top of your file. Google for "include guards" for more information.Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?
-
Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Don't put implementation code in header files. Place this function in one .cpp file only.
-
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?
-
Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
is in Test2 the methode ALSO defined? X|
Press F1 for help or google it. Greetings from Germany
-
For some issues we have included one function definition in header file. That we cannot change. However
#pragma once
should slove the problem. But it doesnt why?
KASR1 wrote:
For some issues we have included one function definition in header file. That we cannot change.
Why ? And why not trying to fix the original issue ?
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
KASR1 wrote:
For some issues we have included one function definition in header file. That we cannot change.
Why ? And why not trying to fix the original issue ?
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
For some issues we have included one function definition in header file. That we cannot change. However
#pragma once
should slove the problem. But it doesnt why?
You do not seem to understand the difference between declaration and implementation. Your header file should contain only the declaration e.g.
void TestMethod();
The implementation code should go in a single source file (Test.cpp) as follows:
void TestMethod()
{
// body of the TestMethod function
}Try reviewing the relevant sections in your C/C++ guides for further explanations.
-
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?
Mark the method as 'inline', or place it inside an anonymous namespace:
namespace {
void TestMethod()
{
..........
}
}[edit] To expand on that answer - by including a function body in a header, it's defined in lots of .obj files. When you link, the linker attempts to merge contents of all object files into an executable. As you have lots of functions with the same signature, it can't do that. The suggestions I've given prevent that. The inline suggestion should stop the function ever existing in the object file, while ht eanonymous namespace thing stops the function being visible outside each of the object files.[/edit]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Mark the method as 'inline', or place it inside an anonymous namespace:
namespace {
void TestMethod()
{
..........
}
}[edit] To expand on that answer - by including a function body in a header, it's defined in lots of .obj files. When you link, the linker attempts to merge contents of all object files into an executable. As you have lots of functions with the same signature, it can't do that. The suggestions I've given prevent that. The inline suggestion should stop the function ever existing in the object file, while ht eanonymous namespace thing stops the function being visible outside each of the object files.[/edit]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?