C++包管理vcpkg的使用


本筆記以Windows作為開發環境,只是寫下了最常用的操作,更多詳細還是看我參考那篇博客,寫的是真的棒。

參考

why

為什么使用vcpkg?

因為習慣了Linux下那種命令編譯的便利性,厭倦了Windows下編譯步驟的復雜,簡直是浪費時間和青春。
這是Windows下較為順手的一個包管理,其他還有幾個就不說了,一言難盡。

安裝

https://github.com/microsoft/vcpkg

We recommend somewhere like C:\src\vcpkg or C:\dev\vcpkg, since otherwise you may run into path issues for some port build systems.

個人在系統盤外的其他盤建立了一個dev目錄 如D:\dev 然后cloen,然后安裝GitHub上vcpkg的文檔來操作,如下

推薦使用 PowerShell 而不是 CMD 命令行來執行各種操作. 在文件管理器中按住shift 然后右鍵就可以在右鍵菜單看到PowerShell了
建議掛上vpn, 並且給您的 git 配置代理 (使用之前需要先安裝git和最好配置下git)

> git clone https://github.com/microsoft/vcpkg
> .\vcpkg\bootstrap-vcpkg.bat

基本使用

查看幫助

 .\vcpkg.exe help

搜索庫

一般先模糊搜索一下庫,這里以jsoncpp為例 (直接.\vcpkg search 會列出所有支持的開源庫列表)

.\vcpkg search jsoncpp

安裝庫

> .\vcpkg install [packagesname]:triplet

需要注意的是:不指定triplet(可以認為是架構),默認安裝的是x86,大概率還是動態庫版本

查看支持的架構

PS E:\dev\vcpkg> ./vcpkg.exe help triplet
Available architecture triplets
VCPKG built-in triplets:
  arm-uwp
  arm64-windows
  x64-linux
  x64-osx
  x64-uwp
  x64-windows-static
  x64-windows
  x86-windows

VCPKG community triplets:
  arm-android
  arm-ios
  arm-linux
  arm-mingw-dynamic
  arm-mingw-static
  arm-neon-android
  arm-windows-static
  arm-windows
  arm64-android
  arm64-ios
  arm64-linux
  arm64-mingw-dynamic
  arm64-mingw-static
  arm64-osx-dynamic
  arm64-osx
  arm64-uwp
  arm64-windows-static-md
  arm64-windows-static
  armv6-android
  ppc64le-linux
  s390x-linux
  wasm32-emscripten
  x64-android
  x64-freebsd
  x64-ios
  x64-mingw-dynamic
  x64-mingw-static
  x64-openbsd
  x64-osx-dynamic
  x64-windows-static-md
  x86-android
  x86-freebsd
  x86-ios
  x86-mingw-dynamic
  x86-mingw-static
  x86-uwp
  x86-windows-static-md
  x86-windows-static
  x86-windows-v120
  • 我們來安裝一個x64靜態庫版本的jsoncpp, 那么操作如下 (jsoncpp:x64-windows-static是這個包的全名,后面的操作你會體會到)
.\vcpkg.exe install jsoncpp:x64-windows-static

會幫我們編譯安裝jsoncpp庫,編譯好后存放在 E:\dev\vcpkg\installed\x64-windows-static 也就是vcpkg的installed下的x64-windows-static 以后你安裝靜態庫版本都是存放在此處的。installed目錄下會分很多不同類型的文件夾作為區分。

注意:你的VS需要安裝英文的語言包。不然你會看到vcpkg的報告。解決辦法:到 VS 安裝向導,修改安裝,點語言包,勾選英語;安裝即可。

移除庫

移除包的名字和你安裝時候的名字要一樣, 不知道詳細名字可以使用 ./vcpkg.exe list 看一下你安裝的包

./vcpkg.exe remove jsoncpp:x64-windows-static

還有個 --oudated

導出包

導出包的名字和你安裝時候的名字要一樣,不然你安裝有多個,可能導出的不是你想要的。

./vcpkg.exe export jsoncpp:x64-windows-static --zip

更新包和依賴

./vcpkg update

更新vcpkg

git pull

集成以及注意事項

集成VS

集成cmake

直接看上面參考的博客吧,已經很詳細了。

常用開源庫舉例

jsoncpp

 .\vcpkg.exe install jsoncpp:x64-windows-static

上面生成的是靜態庫,debug為MTd Relase下為MT

debug工程需要使用debug目錄下的jsoncpp.lib 並把工程的代碼生成修改為MTd
E:\dev\vcpkg\installed\x64-windows-static\debug
Realease 修改為MT

for cmake

The package jsoncpp:x64-windows-static provides CMake targets:

    find_package(jsoncpp CONFIG REQUIRED)
    target_link_libraries(main PRIVATE jsoncpp_object jsoncpp_static)

PS E:\dev\vcpkg>


免責聲明!

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



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