Unresolved external characters in the C ++ static library

I have two projects; static library and tests.

I am sure that I have correctly configured Visual Studio for "tests" using a static library. Here is what I did:

  • Configured library project to create as .lib static library
  • Added additional directories in tests (C / C ++ β†’ General β†’ Additional include directories)
  • Added additional library catalogs in tests (Linker β†’ General-> Additional library catalogs)
  • Added my library as a dependency (Linker -> Input-> Additional dependencies)

I see that my β€œtests” project sees the .lib file and seems to be β€œtrying” to link it. I noticed that when I use objects (defined in classes) in my test project, everything is fine. The problem is that I'm trying to use functions (not member functions!).

Example:

In my header file (.h):

/*includes omitted*/ namespace kx { void func(); /*...and more API functions*/ } 

In the source file (.cpp)

 #include "../MyHeader.h" namespace kx { void func() { /*definition...*/ } /*and more definitions...*/ } 

will result in a linker error; unresolved external symbol "void __cdecl kx::func()"

I read somewhere that this is caused by mixing C with C ++ code, and the solution to this is to use extern "C" in function declarations. I tried this and it solved my problem , but it seems incomprehensible to me, since I use only C ++.

Also, if I understand the mechanism correctly, when I use extern "C" , my namespaces will be ignored, which is exactly what I don't want in this project.

Then I thought that maybe __cdecl causing this problem, so I changed the function declarations to void __stdcall func() , but I had exactly the same linker error unresolved external symbol "void __cdecl kx::func()"

I work as a C ++ programmer, and when I asked senior staff about my problem, they told me that the design described above should work without extern C if I don't mix it with C code.

I would like to know that something is wrong with my setup / approach / design. In the end, I can live with an extern C approach, but I would be very surprised if this is the only solution.

I am using visual studio 2017.

+5
source share

Source: https://habr.com/ru/post/1272956/


All Articles