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?
-
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++ -
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++ -
is in Test2 the methode ALSO defined? X|
Press F1 for help or google it. Greetings from Germany
-
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?