如果我们的工程需要使用一个库并且这个库没有版本限制, 我们可以直接在工程中添加一个vcpkg.json文件并将库的名字添加到depencencies当中. 以下提供一个在Manifest模式下使用sqlite3的简单示例。
1. 本示例使用sqlite3作为示例库.
本示例的文件结构:
C:/
| --test/
| --vcpkg/
| ----build/
| ----CMakeLists.txt
| ----main.cpp
| ----vcpkg.json
vcpkg.json
{
"name": "test",
"version": "0.1.0",
"dependencies": [
"sqlite3"
]
}
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
//调用sqlite3_libversion()来打印sqlite3的版本
#include <sqlite3.h>
#include <stdio.h>
int main()
printf("%s\n", sqlite3_libversion());
return 0;
2. 在build文件夹下打开PowerShell运行以下命令,注意:"C:\test\"为 CMakeLists.txt 文件所在的路径.
"C:\Program Files\CMake\bin\cmake.exe" "C:\test\" -G "Visual Studio 17 2022" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
A suitable version of powershell-core was not found (required v7.2.5). Downloading portable powershell-core v7.2.5...
Downloading powershell-core...
https://github.com/PowerShell/PowerShell/releases/download/v7.2.5/PowerShell-7.2.5-win-x86.zip -> C:vcpkg\downloads\PowerShell-7.2.5-win-x86.zip
Extracting powershell-core...
The following packages will be built and installed:
sqlite3[core]:x64-windows -> 3.39.2
* vcpkg-cmake[core]:x64-windows -> 2022-07-18
* vcpkg-cmake-config[core]:x64-windows -> 2022-02-06#1
Additional packages (*) will be modified to complete this operation.
A suitable version of 7zip was not found (required v21.7.0). Downloading portable 7zip v21.7.0...
Downloading 7zip...
https://www.7-zip.org/a/7z2107-extra.7z -> C:vcpkg\downloads\7z2107-extra.7z
Extracting 7zip...
Restored 3 package(s) from C:\Users\username\AppData\Local\vcpkg\archives in 12.05 s. Use --debug to see more details.
Installing 1/3 vcpkg-cmake-config:x64-windows...
Elapsed time to handle vcpkg-cmake-config:x64-windows: 26.27 ms
Installing 2/3 vcpkg-cmake:x64-windows...
Elapsed time to handle vcpkg-cmake:x64-windows: 24.18 ms
Installing 3/3 sqlite3:x64-windows...
Elapsed time to handle sqlite3:x64-windows: 80.91 ms
Total elapsed time: 1.414 min
sqlite3 provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
-- Running vcpkg install - done
-- The C compiler identification is MSVC 19.33.31630.0
-- The CXX compiler identification is MSVC 19.33.31630.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.33.31629/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/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.33.31629/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: C:/test/build
3. 使用以下命令生成可执行文件:
"C:\Program Files\CMake\bin\cmake.exe" --build "C:\test\build"
MSBuild version 17.3.1+2badb37d1 for .NET Framework
Building Custom Rule C:/test/CMakeLists.txt
main.cpp
main.vcxproj -> C:\test\build\Debug\main.exe
Building Custom Rule C:/test/CMakeLists.txt
4. 运行main.exe.
3.39.2
MSBuild工程使用Manifest模式:
对于Manifest模式使用MSBuild工程, 请参阅: 博客园: https://www.cnblogs.com/vcpkg/p/15019899.html