首页 > 技术文章 > C++包管理vcpkg的使用

__tudou__ 2021-07-17 17:34 原文

本笔记以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>

推荐阅读