Toggle shared/static library: MSVC fails (linker); but AppleClang, gcc (Linux, MinGW) work

I made a little GitHub repo based off the official cmake\Help\guide\tutorial\Complete at 0fdfd6bf. Kept things simple, all C code, two CMakeLists.txt (one library; one executable). And GitHub Actions so you can replicate in a pull-request (or just review my full log files):

What am I doing wrong?

LINK : fatal error LNK1104: cannot open file ‘versions.lib’


I suspect that you export no symbols. Set the default visibility to hidden for gcc to get the same and only export the needed symbols.

Indeed. versions.h has declspec stuff commented out. The linker will not create a .lib file if no symbols are exported.

BTW: I’m hacking around with generate_export_header now… maybe if I manually set the visibility for the function and array in my versions.h then it’ll work (just going off your suggestion)

EDIT: The generated header file is not findable even after adding its directory to target_include_directories and trying "" and <> with basename, and various levels of relative paths. Weird.

Ended up cheating with:

set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/${LIBRARY_NAME}_export.h")
generate_export_header("${LIBRARY_NAME}" EXPORT_FILE_NAME "${_export_file}")

Unfortunately ${CMAKE_CURRENT_SOURCE_DIR} was the only thing that worked. Tried everything from relative paths to no EXPORT_FILE_NAME to completely different targets.

BTW: I actually did solve this linker error. With this export header setup properly it actually ran.