0

I have 3 MSVC projects: static library, dll (linking static lib), exe (linking dll).

  1. In static lib I define a function fnStaticLib() and declare it as exportable symbol.
  2. Dll links in that static library. fnStaticLib() is expected to be exported from Dll.
  3. Exe statically links Dll and calls fnStaticLib().

I get error message: 3>exe.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl fnStaticLib(void)" (_imp?fnStaticLib@@YAXXZ)

Seems like linker just throw out that symbol.

The thing is that it works in our production code, with VS projects. There we have quite large static libs linked to dll projects. I just don't which option or whatever makes it work. Now we try to substitute .vcxproj by CMake (via MSBuild). And it doesn't work with CMake-produced .vcxproj files.

Demo project https://github.com/RomanKhv/static_linkage_opt

Thank you in advance!

Roman Khvostikov
  • 305
  • 3
  • 14
  • 1
    It is by design - [Why can’t I __declspec(dllexport) a function from a static library?](https://devblogs.microsoft.com/oldnewthing/20140321-00/?p=1433) - the symbols from the static library must be referenced in the DLL to be exported. – dewaffled Oct 05 '22 at 13:02
  • https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Jesper Juhl Oct 05 '22 at 18:20
  • Things getting worse when it comes to linking with MFC dlls: – Roman Khvostikov Nov 15 '22 at 14:50
  • Things getting worse when it comes to linking with MFC dlls: - CMAKE_MFC_FLAG is set to 2, - _USRDLL is defined. Linker won't link rawdllmainproxy.obj from mfcs140u(d).lib as it is said in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\atlmfc\src\mfc\rawdllmainproxy.c. This leads to invoking `RawDllMain` from MFC's dllinit.cpp rather than from dllmodule.cpp. And MFC internal global structures aren't initialized correctly RawD – Roman Khvostikov Nov 15 '22 at 14:57

0 Answers0