注意:在尝试以下示例之前,请先执行 './vcpkg integrate install' 集成命令。
1. 首先在同级文件夹下创建文件vcpkg.json, CMakeLists.txt 与 test.cpp 并写入对应代码:
vcpkg.json
{ "name": "test", "version-string": "0.0.1", "dependencies": [ "jsoncpp" ] }
CMakeLists.txt
cmake_minimum_required(VERSION 3.8) project(test) # Add source to this project's executable. add_executable (test "test.cpp") find_package(jsoncpp CONFIG REQUIRED) target_link_libraries(test PRIVATE jsoncpp_lib) target_compile_definitions(test PRIVATE -DJSON_PATH="${CMAKE_CURRENT_LIST_DIR}/")
test.cpp
// 打印清单文件中的项目名称 #include <iostream> #include <fstream> #include <sstream> #include <json/json.h> #ifndef JSON_PATH #define JSON_PATH #endif using namespace std; int main() { ifstream fs; string jsonPath = JSON_PATH; jsonPath.append("vcpkg.json"); fs.open(jsonPath); if (!fs.is_open()) return -1; ostringstream ss; ss << fs.rdbuf(); fs.close(); string rawJson = ss.str(); JSONCPP_STRING err; Json::CharReaderBuilder builder; const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); Json::Value root; if (!reader->parse(rawJson.c_str(), rawJson.c_str() + static_cast<int>(rawJson.length()), &root, &err)) { return -1; } if (root["name"].isString()) cout << "project name: " << root["name"].asString() << endl; return 0; }
2. 配置CMake工程:
"cmake.exe" -G "Visual Studio 16 2019" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows -DVCPKG_BUILD_TYPE=debug -DCMAKE_TOOLCHAIN_FILE:STRING="VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" "CMAKELISTS_PATH"
输出:
-- Running vcpkg install Detecting compiler hash for triplet x64-windows... The following packages will be built and installed: jsoncpp[core]:x64-windows -> 1.9.4 Could not locate cached archive: C:\Users\usr\AppData\Local\vcpkg\archives\b4\b482491c899582676a79208e0586e9af2691cb892ba260e11efe4aaab2d72ba0.zip Starting package 1/1: jsoncpp:x64-windows Building package jsoncpp[core]:x64-windows... -- Using VCPKG_ROOT/downloads/open-source-parsers-jsoncpp-9059f5cad030ba11d37818847443a53918c327b1.tar.gz -- Cleaning sources at VCPKG_ROOT/buildtrees/jsoncpp/src/3918c327b1-034a82149a.clean. Use --editable to skip cleaning for the packages you specify. -- Extracting source VCPKG_ROOT/downloads/open-source-parsers-jsoncpp-9059f5cad030ba11d37818847443a53918c327b1.tar.gz -- Using source at VCPKG_ROOT/buildtrees/jsoncpp/src/3918c327b1-034a82149a.clean -- Found external ninja('1.10.2'). -- Configuring x64-windows -- Building x64-windows-dbg -- Building x64-windows-rel -- Installing: VCPKG_ROOT/packages/jsoncpp_x64-windows/share/jsoncpp/copyright -- Performing post-build validation -- Performing post-build validation done Stored binary cache: C:\Users\usr\AppData\Local\vcpkg\archives\b4\b482491c899582676a79208e0586e9af2691cb892ba260e11efe4aaab2d72ba0.zip Building package jsoncpp[core]:x64-windows... done Installing package jsoncpp[core]:x64-windows... Installing package jsoncpp[core]:x64-windows... done Elapsed time for package jsoncpp:x64-windows: 15.19 s Total elapsed time: 15.19 s -- Running vcpkg install - done -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043. -- The C compiler identification is MSVC 19.28.29916.0 -- The CXX compiler identification is MSVC 19.28.29916.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/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 (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/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: PROJECT_PATH
3. 构建CMake工程:
"cmake.exe" --build "BUILD_DIR"
输出:
Microsoft (R) Build Engine version 16.9.2+58c36d143 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. Checking Build System Building Custom Rule PROJECT_PATH/CMakeLists.txt ManifestTest.cpp PROJECT_PATH\ManifestTest.cpp(24,11): warning C4996: 'Json::Reader': Use CharRe ader and CharReaderBuilder instead. [PROJECT_PATH\manifesttest.vcxproj] PROJECT_PATH\ManifestTest.cpp(24,18): warning C4996: 'Json::Reader::Reader': Us e CharReader and CharReaderBuilder instead [PROJECT_PATH\manifesttest.vcxproj] PROJECT_PATH\ManifestTest.cpp(27,16): warning C4996: 'Json::Reader::parse': Use CharReader and CharReaderBuilder instead. [PROJECT_PATH\manifesttest.vcxproj] PROJECT_PATH\ManifestTest.cpp(29,43): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [PROJECT_PATH\manifesttest.vcxproj] manifesttest.vcxproj -> PROJECT_PATH\Debug\manifesttest.exe Building Custom Rule PROJECT_PATH/CMakeLists.txt
4. 运行程序,显示:
project name: test
vcpkg会自动检测您的项目中是否包含vcpkg.json(与最顶级的CMakeLists.txt同级目录)从而自动激活manifest模式。vcpkg将在您的项目配置时自动将所有依赖库编译并安装至您的本地编译目录下( ${CMAKE_BINARY_DIR}/vcpkg_installed )。
您亦可使用以下选项控制manifest模式:
选项 |
说明 |
VCPKG_TARGET_TRIPLET |
指定安装的triplet。若不使用该参数则使用当前平台中默认的triplet |
VCPKG_HOST_TRIPLET |
指定依赖项的主机triplet。若不使用该参数则根据当前平台使用默认的主机triplet, 该选项适用于解决配置或编译过程中使用了非当前平台可运行的可执行程序问题。例如在编译arm平台时调用可执行程序生成源码。而由于该可执行程序为仅arm平台可运行导致在非arm平台下无法运行问题。 |
VCPKG_MANIFEST_MODE |
指定当前cmake集成使用vcpkg经典模式或vcpkg manifest模式。若您希望在清单文件存在的情况下禁用manifest模式,请将其设置为"OFF"。当清单文件存在或"VCPKG_MANIFEST_DIR"被设置为非空值时,该值默认为"ON"。 |
VCPKG_MANIFEST_DIR |
启用或禁用清单文件中依赖项的自动安装。默认值为"ON"。 |
VCPKG_MANIFEST_INSTALL |
启用或禁用清单文件中依赖项的自动安装。默认值为"ON"。 |
VCPKG_BOOTSTRAP_OPTIONS |
添加命令 "./bootstrap-vcpkg" 之后的参数。 |
VCPKG_OVERLAY_TRIPLETS |
设置安装依赖项时覆盖triplet文件所在的路径。 |
VCPKG_OVERLAY_PORTS |
设置安装依赖项时覆盖库的描述文件所在的路径。 |
VCPKG_MANIFEST_FEATURES |
设置为从manifest安装时启用的特性列表。vcpkg会自动安装该特性的依赖项。当在清单文件中为您的项目添加特性时,使用该选项将您项目中的'option()'与清单文件中的特性绑定。 |
VCPKG_MANIFEST_NO_DEFAULT_FEATURES |
禁用"VCPKG_MANIFEST_FEATURES"中列出的您的项目的所有默认特性。默认值为"OFF"。 |
VCPKG_INSTALL_OPTIONS |
添加manifest模式中安装命令的参数。例如"--debug", "--clean-after-build"等。 |
VCPKG_PREFER_SYSTEM_LIBS |
设置vcpkg是否优先寻找您系统中的库。默认值为"OFF"。 |
以上所有选项可添加至cmake命令中, 或通过'set'等命令在最顶级CMakeLists.txt中的第一个'project()'之前进行设置。
以下提供 VCPKG_MANIFEST_FEATURES 使用示例:
vcpkg.json
{ "name": "mylibrary", "version": "1.0", "dependencies": [ "curl" ], "features": { "samples": { "description": "Build Samples", "dependencies": [ "fltk" ] }, "tests": { "description": "Build Tests", "dependencies": [ "gtest" ] } } }
CMakeLists.txt
# CMakeLists.txt option(BUILD_TESTING "Build tests" OFF) if(BUILD_TESTING) list(APPEND VCPKG_MANIFEST_FEATURES "tests") endif() option(BUILD_SAMPLES "Build samples" OFF) if(BUILD_SAMPLES) list(APPEND VCPKG_MANIFEST_FEATURES "samples") endif() project(myapp) # ...