注意:在尝试以下示例之前,请先执行 '
./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
output
3. 构建CMake工程:
"cmake.exe" --build "BUILD_DIR"
Microsoft (R) Build Engine version 16.9.2+58c36d143 for .NET Framework
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
output
4. 运行程序,显示:
project name: test
vcpkg会自动检测您的项目中是否包含vcpkg.json(与最顶级的CMakeLists.txt同级目录)从而自动激活manifest模式。vcpkg将在您的项目配置时自动将所有依赖库编译并安装至您的本地编译目录下( ${CMAKE_BINARY_DIR}/vcpkg_installed )。
您亦可使用以下选项控制manifest模式:
以上所有选项可添加至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)
# ...