Manifest使用示例3 - 安装并使用本地私有库
使用示例
本示例包括两部分,第一,构建本地的私有库,第二,使用本地私有库
一、构建本地私有库
构建本地私有库的相关文件, 示例为testport。文件结构:
E:/
|--Manifest/
|----my-ports/
|------testport/
|--------vcpkg.json
|--------portfile.cmake
|--------Source.zip
vcpkg.json 文件:
{ "name": "testport", "version": "0.0.1", "description": "Test manifest mode", "dependencies": [ { "name": "vcpkg-cmake", "host": true }, { "name": "vcpkg-cmake-config", "host": true } ] }
portfile.cmake 文件:
#设置源码包路径 set(ARCHIVE "${CMAKE_CURRENT_LIST_DIR}/Source.zip") #解压源码包, SOURCE_PATH的默认值为本场景使用的vcpkg下的buildtrees,本示例中SOURCE_PATH的绝对路径为: E:\Manifest\vcpkg\buildtrees\testport\src vcpkg_extract_source_archive_ex( OUT_SOURCE_PATH SOURCE_PATH ARCHIVE "${ARCHIVE}" ) #使用CMake配置源文件 vcpkg_cmake_configure( SOURCE_PATH "${SOURCE_PATH}" ) #使用CMake安装源文件 vcpkg_cmake_install() #修复库的config文件的路径 vcpkg_cmake_config_fixup(CONFIG_PATH share/${PORT}) #删除debug目录下的头文件,vcpkg默认直接调用release目录下的头文件 file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") #安装license文件 file(INSTALL "${SOURCE_PATH}/LICENSE" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)
二、使用本地私有库
在工程目录下创建文件 vcpkg.json, CMakeLists.txt 与 test.cpp 并写入对应代码,同时创建目录BUILD_DIR, 本示例的BUILD_DIR为E:\Manifest\testbuild
文件结构:
E:/
|--Manifest
|----vcpkg.json
|----CMakeLists.txt
|----test.cpp
|----testbuild/
vcpkg.json
{ "name": "test", "version-string": "0.0.1", "dependencies": [ "testport" ] }
CMakeLists.txt
cmake_minimum_required (VERSION 3.8) project(test) # Add source to this project's executable. add_executable (test "test.cpp") find_package(testport CONFIG REQUIRED) target_link_libraries(test PRIVATE testport::testport)
test.cpp
#include <iostream> #include <testport.h> using namespace std; int main() { cout << addition(10,5) << endl; cout << subtraction(10,5) << endl; cout << multiplication(10,5) << endl; cout << division(10,5) << endl; return 0; }
在CMake工程中使用该库
1. 在testbuild目录中打开cmd窗口,使用CMake命令行编译
"C:\Program Files\CMake\bin\cmake.exe" "E:\Manifest" -G "Visual Studio 17 2022" -A x64 -DCMAKE_CONFIGURATION_TYPES:STRING="Debug" -DCMAKE_TOOLCHAIN_FILE:STRING="E:\Manifest\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_OVERLAY_PORTS="E:\Manifest\my-ports"
输出:
-- Running vcpkg install Detecting compiler hash for triplet x64-windows... The following packages will be built and installed: testport[core]:x64-windows -> 0.0.1 -- E:\Manifest\my-ports\testport * vcpkg-cmake[core]:x64-windows -> 2022-08-18 * vcpkg-cmake-config[core]:x64-windows -> 2022-02-06#1 Additional packages (*) will be modified to complete this operation. Restored 0 package(s) from C:\Users\user\AppData\Local\vcpkg\archives in 283.9 us. Use --debug to see more details. Installing 1/3 vcpkg-cmake-config:x64-windows... Building vcpkg-cmake-config[core]:x64-windows... -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg-port-config.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/copyright -- Performing post-build validation -- Performing post-build validation done A suitable version of 7zip was not found (required v21.7.0). Downloading portable 7zip v21.7.0... Downloading 7zip... https://www.7-zip.org/a/7z2107-extra.7z -> E:\Manifest\vcpkg\downloads\7z2107-extra.7z Extracting 7zip... Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\de\de9be2b2b98415bc8ec5718c6de88e057ee9f439092e6f09d437285d8d1d0ca0.zip" Elapsed time to handle vcpkg-cmake-config:x64-windows: 2.193 s Installing 2/3 vcpkg-cmake:x64-windows... Building vcpkg-cmake[core]:x64-windows... -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_configure.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_build.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_install.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg-port-config.cmake -- Installing: E:/Manifest/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/copyright -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\93\93d1617963ef2aab82aaa193d745bbf66e92246287e02b5eb44e888b32114888.zip" Elapsed time to handle vcpkg-cmake:x64-windows: 121.4 ms Installing 3/3 testport:x64-windows... Building testport[core]:x64-windows... -- Installing port from location: E:\Manifest\my-ports\testport -- Extracting source E:/Manifest/my-ports/testport/Source.zip -- Using source at E:/Manifest/vcpkg/buildtrees/testport/src/Source-9f75bced7a.clean -- Found external ninja('1.10.2'). -- Configuring x64-windows -- Building x64-windows-dbg -- Building x64-windows-rel -- Installing: E:/Manifest/vcpkg/packages/testport_x64-windows/share/testport/copyright -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\5c\5c4d793b96360577b7319f05fc4c9bceafb5fe6554d7b279b201b16cb90a3143.zip" Elapsed time to handle testport:x64-windows: 5.443 s Total elapsed time: 10.74 s testport provides CMake targets: # this is heuristically generated, and may not be correct find_package(testport CONFIG REQUIRED) target_link_libraries(main PRIVATE testport::testport) -- Running vcpkg install - done -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044. -- The C compiler identification is MSVC 19.33.31630.0 -- The CXX compiler identification is MSVC 19.33.31630.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: E:/Manifest/testbuild
2. 构建CMake工程, 生成可执行文件test.exe
"C:\Program Files\CMake\bin\cmake.exe" --build .
输出:
Microsoft (R) Build Engine version 17.3.0-preview-22226-03+b6fc583d4 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. Checking Build System Building Custom Rule E:/Manifest/CMakeLists.txt test.cpp test.vcxproj -> E:\Manifest\testbuild\Debug\test.exe Building Custom Rule E:/Manifest/CMakeLists.txt
3. 运行test.exe
输出:
15 5 50 2
在MSBuild工程中使用该库
1. 在vcpkg目录中打开powershell并执行命令 .\vcpkg.exe integrate install
2. 用VS打开项目,更改以下项目属性
- 在项目 — 属性("Properties")— 配置属性("Configuration Properties")— vcpkg中将 "Use Vcpkg Manifest" 设置为 "Yes" 以启用manifest模式。
- 在项目 — 属性 — vcpkg中将 User Vcpkg 设为 true,将--overlay-ports="<overlay_abs_prefix>\\my-ports"添加进Additional Options中。
注意:本文档示例testport的安装文件目录为: E:\Manifest\vcpkg_installed\x64-windows。
VCPKG 团队期待您的贡献!