Consider a class that belongs to a project linked to dynamic library (foo.dll), with foo.lib generated on the side too:
class IMP_EXP_DIRECTIVE_MACRO Foo
{
void bar()
{
// do something
}
};
Then there is also another project bar (generating static library bar.lib) that includes this class. The project does not link with foo.
Finally there's an application project (resulting in .exe), which links with both foo.lib and bar.lib.
When it does, linker shouts with LNK2005 error that Foo::bar
is already defined in both foo.lib (which is good) and in bar.lib, which is unexpected and weird.
When treating bar.lib with dumpbin.exe /SYMBOLS according to https://learn.microsoft.com/en-us/cpp/build/reference/symbols?view=vs-2017 output shows that Foo::bar
is indeed defined in foo.lib (!). That is strange, because according to this brilliant https://stackoverflow.com/a/4955288 answer, any member function that's declared and defined in the body of a class is implicitly inlined, therefore doesn't cause ODR problems.
Finally, the most interesting part. When moving Foo::bar
definition to .cpp file and leaving only method's declaration in a class body, problem disappears.
Why is that so? Any idea what is being done wrong? I think I've read the whole internet about this topic and tried all various VS configuration switches, but only moving definition to .cpp helps. In principle I can eventually do it, but it doesn't seem like a proper solution to the issue.
Environment is: Visual Studio 10.
edit:
IMP_EXP_DIRECTIVE_MACRO is set to:
"__declspec(dllexport)", when building foo.dll
"", when building bar
edit 2:
Problem is also gone, when added a preprocessor flag to compiling bar.lib that causes class __declspec(dllimport) Foo
usage instead of class Foo
.
Is that the right way to go?