概述

vcpkg是微软开发的在Windows, Linux和MacOS平台管理C/C++库的开源工具。

快速开始

要求

使用vcpkg需满足如下条件:

  • Windows 10, 8.1, 7, Linux, or MacOS
  • Visual Studio 2017 or Visual Studio 2015 Update 3 (on Windows)
  • Git
  • CMake 3.10.2 (optional)

安装vcpkg

> git clone https://github.com/Microsoft/vcpkg
> cd vcpkg PS> .\bootstrap-vcpkg.bat
Ubuntu:~/$ ./bootstrap-vcpkg.sh

为了让计算机的所有用户都可以使用vcpkg,运行如下命令(首次运行需管理员权限):

PS> .\vcpkg integrate install
Ubuntu:~/$ ./vcpkg integrate install

安装库

通过如下命令便可以安装库:

PS> .\vcpkg install sdl2 curl
Ubuntu:~/$ ./vcpkg install sdl2 curl

对于Windows平台,vcpkg默认安装32位库,如果想要设置为默认安装64位库,在环境变量中加上VCPKG_DEFAULT_TRIPLET=x64-windows即可。

如果你在安装库时下载速度非常慢甚至下载失败,可以拷贝下载链接自行下载好库的压缩包,然后放在downloads文件夹,这样vcpkg便直接使用下载好的库来编译安装。

对于有些库,默认可能不是所有的依赖都安装,如ceres-solver,默认不支持suitesparse,cxsparse,此时可以通过命令.\vcpkg install ceres[suitesparse,cxsparse]:x64-windows --recurse重新安装,其中--recurse表示可以卸载之前的库。更多install参数可以通过命令.\vcpkg help install查看。

再比如支持cuda的opencv版本,可以通过命令.\vcpkg install opencv[cuda]:x64-windows来安装。

卸载vcpkg

直接删除vcpkg的文件夹即可。

使用库

CMake

在CMake中使用通过vcpkg安装的库的最佳方式是通过工具链文件(toolchain file) scripts/buildsystems/vcpkg.cmake,让安装的库通过find_package()被发现。

要使用这个文件,只需通过命令-DCMAKE_TOOLCHAIN_FILE=[vcpkg root]/scripts/buildsystems/vcpkg.cmake将其加入CMake命令行中即可。例如

cmake .. -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake (Linux/MacOS)
cmake .. -DCMAKE_TOOLCHAIN_FILE=vcpkg\scripts\buildsystems\vcpkg.cmake (Windows)

再比如,如果要用VS2017编译器,输入下面命令即可:

cmake .. -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake -G "Visual Studio 15 2017 Win64"

还有一种方法,直接在CMakeLists.txt文件中指定CMAKE_TOOLCHAIN_FILE,即

set(CMAKE_TOOLCHAIN_FILE "D:\vcpkg\scripts\buildsystems\vcpkg.cmake")
project(PROJECT_NAME)

这里需要注意的是,设置CMAKE_TOOLCHAIN_FILE要在project()命令之前。另外多说一句,类似CMAKE_TOOLCHAIN_FILE, CMAKE_SYSTEM_NAME, CMAKE_C_COMPILER等这些变量都要在project()命令之前设定,不然CMake仍然会按照默认的设置来。

VisualStudio

在VS中,所有已经安装的库都被VS项目自动包含(通过前面提到的vcpkg integrate install命令实现),无需配置便可直接使用。

CLion

在CLion中的配置如下File -> Settings -> Build, Execution, Deployment -> CMake,在CMake Options中添加-DCMAKE_TOOLCHAIN_FILE=[vcpkg root]/scripts/buildsystems/vcpkg.cmake

vcpkg命令

  • vcpkg help: 帮助
  • vcpkg search: 搜索库
  • vcpkg install: 安装库

    对于Windows平台

    • .\vcpkg install openmesh:x86-windows:安装32位库
    • .\vcpkg install openmesh:x64-windows:安装64位库
    • .\vcpkg install openmesh:x64-windows-static 安装64位静态库
  • vcpkg list:列出所有已经安装的库
  • vcpkg upgrade:列出所有可以升级的库,如果需要升级,需额外添加--no-dry-run命令
  • vcpkg update
  • vcpkg remove:移除某个已经安装的库,如果需要移除依赖该库的其他库,添加--recurse命令

vcpkg文件夹构成

  • buildtrees -- contains subfolders of sources from which each library is built
  • docs -- documentation and examples
  • downloads -- cached copies of any downloaded tools or sources. vcpkg searches here first when you run the install command
  • installed-- Contains the headers and binaries for each installed library. When you integrate with Visual Studio, you are essentially telling it add this folder to its search paths
  • packages -- Internal folder for staging between installs
  • ports -- Files that describe each library in the catalog, its version, and where to download it. You can add your own ports if needed
  • scripts -- Scripts (cmake, powershell) used by vcpkg
  • toolsrc -- C++ source code for vcpkg and related components
  • triplets -- Contains the settings for each supported target platform (for example, x86-windows or x64-uwp)

更新vcpkg

在vcpkg根目录下的ports文件夹中可以看到当前版本包含的所有库,但由于vcpkg项目正在活跃开发中,有时候有些库在你当前的版本中并没有加入,这时可以考虑更新vcpkg。首先拉取vcpkg的远程仓库,更新本地仓库:

git fetch origin master:temp  // 从远程的origin仓库的master分支下载到本地并新建一个分支temp
git diff temp // 比较本地的仓库和远程仓库的区别
git merge temp // 合并temp分支到master分支
git branch -d temp // 如果不想要temp分支了,可以删除此分支

然后重新编译生成vcpkg.exe工具

PS> .\bootstrap-vcpkg.bat
Linux:~/$ ./bootstrap-vcpkg.sh

然后可以通过命令.\vcpkg update .\vcpkg upgrade更新已经安装好的库。再通过install命令安装新的库。

参考

最新文章

  1. 【集合框架】JDK1.8源码分析之Collections && Arrays(十)
  2. Activity中获取当前Fragment 中的子控件
  3. Java Concurrency in Practice 读书笔记 第二章
  4. WCF例子
  5. ionic环境搭建和安装
  6. CALayer总结(一)
  7. Linux下ThinkPHP网站目录权限设置
  8. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
  9. C# 简化优化if/switch 表驱动法
  10. 杭电OJ分类
  11. Java中的回调函数学习
  12. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)
  13. CORS jsonp
  14. php ,session 详解.
  15. DataGuard之Apply Services(redo应用和SQL应用)
  16. LeetCode Generate Parentheses (DFS)
  17. GBT算法在拖动滑块辨别人还是机器中的应用
  18. pionter指针小结
  19. 雷林鹏分享:Ruby CGI Cookies
  20. E.Text Editor (Gym 101466E + 二分 + kmp)

热门文章

  1. AJAX学习笔记——JSON
  2. redis 与 序列化
  3. RedisTemplate和StringRedisTemplate的区别
  4. msaa mrt load store action unity
  5. MVC框架和MTV框架
  6. Spring事务采坑 —— timeout
  7. MongoDB 建立与删除索引
  8. luogu 3702 [SDOI2017]序列计数 矩阵乘法+容斥
  9. Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)
  10. 【csp模拟赛九】--dfs