Home > database >  Cmake: How to statically link packages to shared library?
Cmake: How to statically link packages to shared library?

Time:09-17

I want to create a .dll library with all its dependencies packed inside the .dll.

However, there seems to be no easy way to achieve that with Cmake. My setup:

cmake_minimum_required(VERSION 3.0.0)
project(Main VERSION 0.1.0)

add_library(Main SHARED Main.cpp)

find_package(libzippp REQUIRED)

target_link_libraries(Main PRIVATE libzippp::libzippp) 

This will produce both Main.dll but also libzippp.dll.

I would like to have libzippp.dll packed (statically linked) into Main.dll.

CodePudding user response:

Of course you can't pack one DLL into another. You have to make libzippp a static library in the first place. To do this, build libzippp with BUILD_SHARED_LIBS set to NO at the CMake command line. Then libzippp::libzippp will be a static library when you go to find_package it.

This is easy enough to show steps for:

$ git clone [email protected]:ctabin/libzippp.git
$ cmake -S libzippp -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=NO -DCMAKE_INSTALL_PREFIX=$PWD/local -DLIBZIPPP_BUILD_TESTS=NO
$ cmake --build build --target install
$ tree local
local/
├── include
│   └── libzippp
│       └── libzippp.h
├── lib
│   └── libzippp_static.a
└── share
    └── libzippp
        ├── FindLIBZIP.cmake
        ├── libzipppConfig.cmake
        ├── libzipppConfigVersion.cmake
        ├── libzipppTargets.cmake
        └── libzipppTargets-release.cmake
  • Related