[关闭]
@khan-lau 2022-03-21T01:30:49.000000Z 字数 2835 阅读 473

包管理工具vcpkg

vcpkg MacOS


安装 vcpkg

  1. $ brew install vcpkg



配置


  1. {
  2. "cmake.configureSettings": {
  3. "CMAKE_TOOLCHAIN_FILE": "/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake"
  4. }
  5. }


打开CMake设置选项,将 vcpkg toolchain 文件路径在 CMake toolchain file 中:
[vcpkg root]/scripts/buildsystems/vcpkg.cmake


打开 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


  1. set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
  2. CACHE STRING "Vcpkg toolchain file")

使用此种方式可无需设置 CMAKE_TOOLCHAIN_FILE 即可使用vcpkg,且更容易完成配置工作。


  1. $ cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=/opt/homebrew/opt/vcpkg/libexec/scripts/buildsystems/vcpkg.cmake
  2. $ cmake --build [build directory]

vcpkg 常用指令

  1. # 搜索包
  2. $ vcpkg search sqlite
  3. > libodb-sqlite 2.4.0 Sqlite support for the ODB ORM library
  4. > sqlite3 3.32.1 SQLite is a software library that implements a se...
  5. >
  6. # 安装包
  7. $ vcpkg install sqlite3
  8. > Computing installation plan...
  9. > The following packages will be built and installed:
  10. > sqlite3[core]:x86-windows
  11. > Starting package 1/1: sqlite3:x86-windows
  12. > Building package sqlite3[core]:x86-windows...
  13. > -- Downloading https://sqlite.org/2020/sqlite-amalgamation-3320100.zip...
  14. > -- Extracting source C:/src/vcpkg/downloads/sqlite-amalgamation-3320100.zip
  15. > -- Applying patch fix-arm-uwp.patch
  16. > -- Using source at C:/src/vcpkg/buildtrees/sqlite3/src/3320100-15aeda126a.clean
  17. > -- Configuring x86-windows
  18. > -- Building x86-windows-dbg
  19. > -- Building x86-windows-rel
  20. > -- Performing post-build validation
  21. > -- Performing post-build validation done
  22. > Building package sqlite3[core]:x86-windows... done
  23. > Installing package sqlite3[core]:x86-windows...
  24. > Installing package sqlite3[core]:x86-windows... done
  25. > Elapsed time for package sqlite3:x86-windows: 12 s
  26. >
  27. > Total elapsed time: 12.04 s
  28. >
  29. > The package sqlite3:x86-windows provides CMake targets:
  30. >
  31. > find_package(unofficial-sqlite3 CONFIG REQUIRED)
  32. > target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3))
  33. # 浏览已安装包
  34. $ vcpkg list
  35. > sqlite3:x86-windows 3.32.1 SQLite is a software library that implements a se...
  36. # 删除过时的包
  37. $ vcpkg remove --outdated
  38. # 更新已安装的包
  39. $ vcpkg upgrade --no-dry-run
  40. # 全局集成环境
  41. $ vcpkg integrate install
  42. > Applied user-wide integration for this vcpkg root.
  43. >
  44. > All C++ projects can now #include any installed libraries.
  45. > Linking will be handled automatically.
  46. > Installing new libraries will make them instantly available.
  47. # 删除全局集成环境
  48. $ vcpkg integrate remove

常规情况下,我们需要设置include目录、lib目录等,会有很多工作量。而vcpkg提供了一套机制,可以全自动的适配目录,而开发者不需要关心已安装的库的目录在哪里,也不需要设置。这是vcpkg的一大优势。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注