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.