@khan-lau
2022-03-21T09:30:49.000000Z
字数 2835
阅读 641
vcpkg
MacOS
$ brew install vcpkg
如使用 QtCreator
在 QtCreator 工程的 [构建设置
] --> [CMake
] --> [Initial CMake parameters
] 中加入参数 -DCMAKE_TOOLCHAIN_FILE=/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake
如使用Visual Studio Code
中的 CMake Tools
将以下内容添加到您的工作区的 settings.json 中将使CMake Tools自动使用vcpkg中的第三方库:
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake"
}
}
Visual Studio
CMake 工程中使用 vcpkg打开CMake设置选项,将 vcpkg toolchain 文件路径在 CMake toolchain file 中:
[vcpkg root]/scripts/buildsystems/vcpkg.cmake
CLion
中使用 vcpkg
打开 Toolchains
设置 (File
> Settings
on Windows and Linux, CLion > Preferences
on macOS), 并打开 CMake 设置 (Build, Execution, Deployment > CMake)。 最后在 CMake options 中添加 -DCMAKE_TOOLCHAIN_FILE=/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake
将 vcpkg
作为一个子模块
当您希望将vcpkg作为一个子模块加入到您的工程中时, 您可以在第一个 project() 调用之前将以下内容添加到 CMakeLists.txt 中, 而无需将 CMAKE_TOOLCHAIN_FILE 传递给cmake调用。
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
使用此种方式可无需设置 CMAKE_TOOLCHAIN_FILE 即可使用vcpkg,且更容易完成配置工作。
终端
$ cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake
$ cmake --build [build directory]
# 搜索包
$ vcpkg search sqlite
> libodb-sqlite 2.4.0 Sqlite support for the ODB ORM library
> sqlite3 3.32.1 SQLite is a software library that implements a se...
>
# 安装包
$ vcpkg install sqlite3
> Computing installation plan...
> The following packages will be built and installed:
> sqlite3[core]:x86-windows
> Starting package 1/1: sqlite3:x86-windows
> Building package sqlite3[core]:x86-windows...
> -- Downloading https://sqlite.org/2020/sqlite-amalgamation-3320100.zip...
> -- Extracting source C:/src/vcpkg/downloads/sqlite-amalgamation-3320100.zip
> -- Applying patch fix-arm-uwp.patch
> -- Using source at C:/src/vcpkg/buildtrees/sqlite3/src/3320100-15aeda126a.clean
> -- Configuring x86-windows
> -- Building x86-windows-dbg
> -- Building x86-windows-rel
> -- Performing post-build validation
> -- Performing post-build validation done
> Building package sqlite3[core]:x86-windows... done
> Installing package sqlite3[core]:x86-windows...
> Installing package sqlite3[core]:x86-windows... done
> Elapsed time for package sqlite3:x86-windows: 12 s
>
> Total elapsed time: 12.04 s
>
> The package sqlite3:x86-windows provides CMake targets:
>
> find_package(unofficial-sqlite3 CONFIG REQUIRED)
> target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3))
# 浏览已安装包
$ vcpkg list
> sqlite3:x86-windows 3.32.1 SQLite is a software library that implements a se...
# 删除过时的包
$ vcpkg remove --outdated
# 更新已安装的包
$ vcpkg upgrade --no-dry-run
# 全局集成环境
$ vcpkg integrate install
> Applied user-wide integration for this vcpkg root.
>
> All C++ projects can now #include any installed libraries.
> Linking will be handled automatically.
> Installing new libraries will make them instantly available.
# 删除全局集成环境
$ vcpkg integrate remove
常规情况下,我们需要设置include目录、lib目录等,会有很多工作量。而vcpkg提供了一套机制,可以全自动的适配目录,而开发者不需要关心已安装的库的目录在哪里,也不需要设置。这是vcpkg的一大优势。