Manifest使⽤⽰例-CMake⼯程
注意:在尝试以下⽰例之前,请先执⾏ './vcpkg integrate install' 集成命令。
1. ⾸先在同级⽂件夹下创建⽂件vcpkg.json, 与 test.cpp 并写⼊对应代码:
vcpkg.json
{
"name": "test",
"version-string": "0.0.1",
"dependencies": [
"jsoncpp"
]
}
<
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> wCharReader());
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;
return0;
}
2. 配置CMake⼯程:
""  -G "Visual Studio 16 2019" -A x64  -DVCPKG_TARGET_TRIPLET=x64-windows -DVCP
KG_BUILD_TYPE=debug  -DCMAKE_TOOLCHAIN_FILE:STRING="VCPKG_ROOT\scripts\ake 输出:
-- Running vcpkg install
Detecting compiler hash for
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]:
-- Using VCPKG_ROOT/downloads/open-
-
- 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-
-- 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]: done
Installing package jsoncpp[core]:
Installing package jsoncpp[core]: 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/ - 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/ - 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⼯程:
"" --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_
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\
Building Custom Rule PROJECT_
output
4. 运⾏程序,显⽰:
project name: test
vcpkg会⾃动检测您的项⽬中是否包含vcpkg.json(与最顶级的同级⽬录)从⽽⾃动激活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'等命令在最顶级中的第⼀个'project()'之前进⾏设置。
以下提供 VCPKG_MANIFEST_FEATURES 使⽤⽰例:
vcpkg.json
{
"name": "mylibrary",
"version": "1.0",
"dependencies": [ "curl" ],
"features": {
"samples": {
"description": "Build Samples",
"dependencies": [ "fltk" ]
},
"tests": {为什么现在都用cmake
"description": "Build Tests",
"dependencies": [ "gtest" ]
}
}
}
<
#
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)
# ...

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。