應該如何使用vcpkg 提供的庫呢,vcpkg提供2種使用方式,一種是在CMake 工程使用,另一種是在MSbuild 工程使用,我們分別提供了示例。
如何在CMake 工程中使用?示例:sqlite3
第一步: 安裝庫
PS E:\vcpkg\clean\vcpkg> ./vcpkg install sqlite3:x64-windows
Computing installation plan...
The following packages will be built and installed:
sqlite3[core]:x64-windows -> 3.34.1 Detecting compiler hash for triplet x64-windows... Could not locate cached archive: C:\Users\phoebe\AppData\Local\vcpkg\archives\1e\1e772ef47022be1c658daf2d07997ec134ada023.zip Starting package 1/1: sqlite3:x64-windows Building package sqlite3[core]:x64-windows... -- Downloading https://sqlite.org/2021/sqlite-amalgamation-3340100.zip -> sqlite-amalgamation-3340100.zip... -- Extracting source E:/vcpkg/clean/vcpkg/downloads/sqlite-amalgamation-3340100.zip -- Applying patch fix-arm-uwp.patch -- Using source at E:/vcpkg/clean/vcpkg/buildtrees/sqlite3/src/3340100-3d888d9856.clean -- Configuring x64-windows -- Building x64-windows-dbg -- Building x64-windows-rel -- Performing post-build validation -- Performing post-build validation done Stored binary cache: C:\Users\phoebe\AppData\Local\vcpkg\archives\1e\1e772ef47022be1c658daf2d07997ec134ada023.zip Building package sqlite3[core]:x64-windows... done Installing package sqlite3[core]:x64-windows... Installing package sqlite3[core]:x64-windows... done Elapsed time for package sqlite3:x64-windows: 38.07 s Total elapsed time: 54.06 s The package sqlite3:x64-windows provides CMake targets: find_package(unofficial-sqlite3 CONFIG REQUIRED) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
第二步:集成
通過以下命令可以把vcpkg提供的庫集成到我們的工程中,之后的文章會詳細介紹這部分內容,現在只需要拿到toolchain 文件。
PS E:\vcpkg\clean\vcpkg> ./vcpkg integrate install
Applied user-wide integration for this vcpkg root. All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available. CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/clean/vcpkg/scripts/buildsystems/vcpkg.cmake"
第三步:使用
我們可以通過visual studio IDE 集成cmake 來使用vcpkg 提供的庫, 也可以通過cmake 命令行直接來使用,以下分別給出示例。
CMake 命令行使用示例:
1,在 E:\vcpkg\vcpkgtest\sliqte3 目錄下創建文件CMakeLists.txt 與 main.cpp文件。
# CMakeLists.txt cmake_minimum_required(VERSION 3.0) project(test) find_package(unofficial-sqlite3 CONFIG REQUIRED) add_executable(main main.cpp) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
// main.cpp
#include <sqlite3.h>
#include <stdio.h>
int main() { printf("%s\n", sqlite3_libversion()); return 0; }
2, 打開x64 Native Tools 命令框,執行以下命令創建build文件夾 並進入build目錄。
********************************************************************** ** Visual Studio 2019 Developer Command Prompt v16.9.0 ** Copyright (c) 2021 Microsoft Corporation ********************************************************************** [vcvarsall.bat] Environment initialized for: 'x64' C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>pushd E:\vcpkg\vcpkgtest\sliqte3 E:\vcpkg\vcpkgtest\sliqte3>mkdir build E:\vcpkg\vcpkgtest\sliqte3>cd build
3, 執行CMake 命令生成VS工程文件。
cmake.exe .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/clean/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows
注意:CMAKE_TOOLCHAIN_FILE 文件就是在第二步集成時獲取的,VCPKG_TARGET_TRIPLET需要與安裝sqlite3選用的triplet保持一致。
輸出信息:
E:\vcpkg\vcpkgtest\sliqte3\build>cmake.exe .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/clean/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows -DVCPKG_BUILD_TYPE=debug -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042. -- The C compiler identification is MSVC 19.28.29910.0 -- The CXX compiler identification is MSVC 19.28.29910.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: E:/vcpkg/vcpkgtest/sliqte3/build
4, 構建
E:\vcpkg\vcpkgtest\sliqte3\build>cmake --build . Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. Checking Build System Building Custom Rule E:/vcpkg/vcpkgtest/sliqte3/CMakeLists.txt main.cpp main.vcxproj -> E:\vcpkg\vcpkgtest\sliqte3\build\Debug\main.exe Building Custom Rule E:/vcpkg/vcpkgtest/sliqte3/CMakeLists.txt
5,測試
E:\vcpkg\vcpkgtest\sliqte3\build> .\Debug\main.exe
3.34.1
通過visual studio IDE 集成cmake 來使用示例:
1,創建cmake 工程 ‘sqlite3test’。
2,修改CMakeSettings.json配置文件。
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Visual Studio 16 2019 Win64",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/clean/vcpkg/scripts/buildsystems/vcpkg.cmake",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}
3, 打開 與Sqlite3test.cpp 同一級目錄的CMakeLists.txt 文件, 添加:
find_package(unofficial-sqlite3 CONFIG REQUIRED) target_link_libraries(sqlite3test PRIVATE unofficial::sqlite3::sqlite3)
4,打開 Sqlite3test.cpp 添加:
// Sqlite3test.cpp
#include "sqlite3test.h" #include <sqlite3.h> #include <stdio.h> int main() { printf("%s\n", sqlite3_libversion()); return 0; }
5,CMakeSettings.json 保存之后會自動配置生成 vs 工程文件,現在只需要build 這個工程, 選中最外層的CMakeLists.txt 文件 點擊build。
6,構建好之后,選中sqlite3test.exe, 並且點擊運行測試。
可以看到執行的結果是:
3.34.1
注意:如果需要改動 CMakeSettings.json配置文件,最好把CMake cache 文件刪掉,重新再生成一遍。