Manifest 使用示例 - CMake 工程


注意:在嘗試以下示例之前,請先執行 './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
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
output

 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) # ...

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM