CMake: Create DLL including dependencies instead of separate dll's

Viewed 488

Im writing a SDK for Windows and Mac OS in C++, and im using CMake. On windows, I'd like the compiled DLL to contain all necessary dependencies, instead of having separate DLLs for all third party libraries im using.

Here are the relevant sections of the MakeFile:

find_package(OpenSSL REQUIRED)
find_package(CURL CONFIG REQUIRED)
find_package(nlohmann_json 3.2.0 REQUIRED)
find_package(spdlog REQUIRED)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
....
add_library(${PROJECT_NAME} SHARED ${SRC_FILES})
...
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto CURL::libcurl nlohmann_json::nlohmann_json spdlog::spdlog_header_only unofficial::sqlite3::sqlite3)

This generates the following separate DLLs:

fmt.dll
libcrypto-1_1.dll
libcurl.dll
sqlite3.dll
zlib1.dll

Is it possible to create a library containing all dependecies, like it is on Mac OS with the generated dylib?

1 Answers

I'm using Vcpkg for simplicity but you should compile your dependencies as static libraries instead of shared.

If you're using vcpkg you can install the dependencies as static like such vcpkg.exe install openssl:x86-windows-static

Make sure you run CMake with VCPKG_TARGET_TRIPLET set to x86-windows-static or whatever platform you're on.

Example.dll statically linked to OpenSSL

As you can see example.dll is now statically linked to OpenSSL.

Cheers

Related