Search code examples
c++visual-studiodllcmakevisual-studio-2019

VisualStudio CMake dynamic library: include/link all used functions in *single* dll


I have a VisualStudio-2019 C++ Project which uses CMake and Ninja to build a dll, the Project uses functions from a few Libraries like protobuf and spdlog, which I have installed using vcpkg.

When building, the output gets written to four distinct dll files and all of them are needed for the main-dll to run. Below are two screenshots: left the current state and right the expected state.

too many dependencies expectd result

The main.dll file should include all the functions it imports, the compiler shouldn't create separate dlls for each library.

I don't which know knob I need to turn, I can imagine several ways to edit:

  • The C++ Code of the Project (Classes, inlining functions ?)
  • The CMakeList.txt
  • The Arguments of MSVC (compiler, linker flags)
  • The Arguments of Ninja

Where should I start ?


Solution

  • Setting the mentioned flags or appending -static to the import library components had no effect, I went as far as

    set(BUILD_SHARED_LIBS OFF)
    set(CMAKE_EXE_LINKER_FLAGS "-static")
    set(CMAKE_MODULE_LINKER_FLAGS "-static")
    set(CMAKE_SHARED_LINKER_FLAGS "-static")
    set(CMAKE_STATIC_LINKER_FLAGS "-static")
    

    But this didn't help a bit.

    As it turned out this is a problem with vcpkg: I had installed the packages with the triplet x64-windows, which builds for dynamic linkage, installing the packages with vcpkg install --triplet=x64-windows-static protobuf spdlog makes static linking possible. If the static variants are not installed, the build proceeds silently with the dynamic ones.