I have two projects; static library and tests.
I am sure that I configured Visual Studio correctly to "tests" project use static library. Here is what I did:
- Configured library project to build as static library .lib
- Added additional include directories in tests (C/C++ -> General -> Additional include directories)
- Added additional library directories in tests (Linker -> General-> Additional library directories)
- Added my library as dependency (Linker -> Input-> Additional dependencies)
I can see that my "tests" project see the .lib file and seems to "try" to link to it. I noticed that when I use objects (defined in classes) in my tests project, everthing is fine. Problem is when I try to use functions (not member functions!).
Example:
In my header (.h) file:
/*includes omitted*/
namespace kx
{
void func();
/*...and more API functions*/
}
In my source file (.cpp)
#include "../MyHeader.h"
namespace kx
{
void func()
{
/*definition...*/
}
/*and more definitions...*/
}
Will result in linker error; unresolved external symbol "void __cdecl kx::func()"
I read somewhere that this is caused by mixing C with C++ code, and solution to this is to use extern "C"
in function declarations. I tried that and it solved my problem, but It seems unclear to me, since I use only c++.
Additionally, If I understand the mechanism right, when I use extern "C"
my namespaces will be ignored, which is exactly what I dont want in this design.
Next, I thought that maybe __cdecl
causes this problem, so I changed my function declarations to void __stdcall func()
, but I had exactly the same linker error unresolved external symbol "void __cdecl kx::func()"
I work as C++ programmer and when I asked senior stuff about my problem, they told me that design described above should work without extern C
, unless I dont mix it with C code.
I would like to know if there is something wrong with my setup/approach/design. In the end I can live with extern C
approach, but I would be very surprised if this is the only solution.
I use visual studio 2017.
extern "C"
controls wether C++ name mangling is applied. (Withextern "C"
it is not.) As C++ supports polymorphy, the linker would have problems with multiple functions with same name. Hence the C++ compiler "decorates" the function names (in definition as well as in calls). If you want to use C functions you have to disable decoration to prevent link errors.extern "C"
for C++ functions as well if we want to use them in plug-ins. Name-mangling is implementation-dependent. If you want to link at run-time i.e. search a function pointer by string,extern "C"
is useful. Otherwise, we had to search the decorated name but (due to implementation dependent name-mangling) its difficult to determine.extern "C"
and C++ namespace? Hmm. Found this in google: SO: extern “C” linkage inside C++ namespace?