在 Linux 上生成动态库

使用 vcpkg ,可以现成地为许多配置生成库。 但是,这目前不包括 Linux 和 Mac OS 上的共享库。

这并不意味着你无法使用 vcpkg 在这些平台上生成动态库! 本文档将指导你创建自己的自定义三元组件 --overlay-triplets ,以便轻松在 Linux 上生成动态库。

步骤 1:创建自定义三元文件

若要节省时间,请复制现有的 x64-linux.cmake 三元组文件。

~/git$ mkdir custom-triplets
~/git$ cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-dynamic.cmake

并修改 custom-triplets/x64-linux-dynamic.cmake 以匹配以下内容:

# ~/git/custom-triplets/x64-linux-dynamic.cmake
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic) # This changed from static to dynamic
set(VCPKG_CMAKE_SYSTEM_NAME Linux)

步骤 2:使用 --overlay-triplets 生成动态库

--overlay-triplets使用 选项在 目录中包括三元。custom-triplets

~/git$ vcpkg/vcpkg install sqlite3:x64-linux-dynamic --overlay-triplets=custom-triplets
The following packages will be built and installed:
    sqlite3[core]:x64-linux-dynamic
Starting package 1/1: sqlite3:x64-linux-dynamic
Building package sqlite3[core]:x64-linux-dynamic...
-- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux-dynamic.cmake
-- Downloading https://sqlite.org/2019/sqlite-amalgamation-3280000.zip...
-- Extracting source /home/victor/git/vcpkg/downloads/sqlite-amalgamation-3280000.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at /home/victor/git/vcpkg/buildtrees/sqlite3/src/3280000-6a3ff7ce92
-- Configuring x64-linux-dynamic-dbg
-- Configuring x64-linux-dynamic-rel
-- Building x64-linux-dynamic-dbg
-- Building x64-linux-dynamic-rel
-- Performing post-build validation
-- Performing post-build validation done
Building package sqlite3[core]:x64-linux-dynamic... done
Installing package sqlite3[core]:x64-linux-dynamic...
Installing package sqlite3[core]:x64-linux-dynamic... done
Elapsed time for package sqlite3:x64-linux-dynamic: 44.82 s
Total elapsed time: 44.82 s
The package sqlite3:x64-linux-dynamic provides CMake targets:
    find_package(unofficial-sqlite3 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)

使用 vcpkg installvcpkg updatevcpkg upgradevcpkg remove时,覆盖三元支持自定义三元文件。

使用 --overlay-triplets 选项时,如下所示的消息会告知你正在使用自定义三元:

-- Loading triplet configuration from: /home/custom-triplets/x64-linux-dynamic.cmake

重写默认三元

你可能已注意到,Windows (x86-windows 的默认三元组和 x64-windows) 安装动态库,而静态库需要后缀 (-static) 。 这与 Linux 和 Mac OS 不同,其中静态库由 x64-linuxx64-osx生成。

使用 --overlay-triplets 它可以替代默认三元,以在 Linux 上实现相同的行为:

  • x64-linux:生成动态库,
  • x64-linux-static:生成静态库。
  • 步骤 1:创建覆盖三元

    使用在上一示例中创建的自定义三元组,将 重命名 custom-triplets/x64-linux-dynamic.cmakecustom-triplets/x64-linux.cmake。 然后,复制默认 x64-linux 的三元 (在文件夹中生成静态库) custom-triplets ,并将其重命名为 x64-linux-static.cmake

    ~/git$ mv custom-triplets/x64-linux-dynamic.cmake custom-triplets/x64-linux.cmake
    ~/git$ cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-static.cmake
    

    步骤 2:使用 --overlay-triplets 替代默认三元

    --overlay-triplets使用 选项在 目录中包括三元。custom-triplets

    ~/git$ vcpkg/vcpkg install sqlite3:x64-linux --overlay-triplets=custom-triplets
    The following packages will be built and installed:
        sqlite3[core]:x64-linux
    Starting package 1/1: sqlite3:x64-linux
    Building package sqlite3[core]:x64-linux...
    -- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux.cmake
    -- Downloading https://sqlite.org/2019/sqlite-amalgamation-3280000.zip...
    -- Extracting source /home/victor/git/vcpkg/downloads/sqlite-amalgamation-3280000.zip
    -- Applying patch fix-arm-uwp.patch
    -- Using source at /home/victor/git/vcpkg/buildtrees/sqlite3/src/3280000-6a3ff7ce92
    -- Configuring x64-linux-dbg
    -- Configuring x64-linux-rel
    -- Building x64-linux-dbg
    -- Building x64-linux-rel
    -- Performing post-build validation
    -- Performing post-build validation done
    Building package sqlite3[core]:x64-linux... done
    Installing package sqlite3[core]:x64-linux...
    Installing package sqlite3[core]:x64-linux... done
    Elapsed time for package sqlite3:x64-linux: 44.82 s
    Total elapsed time: 44.82 s
    The package sqlite3:x64-linux provides CMake targets:
        find_package(unofficial-sqlite3 CONFIG REQUIRED)
        target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
    

    默认三元由自定义三元屏蔽:

    -- Loading triplet configuration from: /home/victor/git/custom-triplets/x64-linux.cmake