0. 前言
Windows平台的程序包的依赖管理一直以来都是个大难题。之前有NuGet,现在有vcpkg。
本文的重点是先介绍一下vcpkg的特性,然后以一个实际例子说明,说明如何创建一个vcpkg安装包。
1. vcpkg简介
vcpkg是为了在windows平台能够方便获取一个C或者C++库。当前还处于预览版状态。不过已经有很多常见的库了,例如:
- openssl
- boost
- zlib
- glog
- libevent
- curl
等等,很多知名的C/C++库都已经提供vcpkg安装包。
2. vcpkg安装
首先,我们的windows系统必须安装有下列软件
- Visual Studio 2015 Update 3 or
- Visual Studio 2017
- CMake 3.8.0 or higher (note: downloaded automatically if not found)
- git.exe available in your path
然后,我们在windows命令行终端上运行:
d:\git> git clone https://github.com/Microsoft/vcpkg
d:\git> cd vcpkg
d:\git\vcpkg> .\bootstrap-vcpkg.bat
d:\git\vcpkg> .\vcpkg integrate install
3. 使用vcpkg获取C/C++库
使用vcpkg --help
命令查看联席帮助文档:
D:\git\vcpkg>.\vcpkg --help
Commands:
vcpkg search [pat] Search for packages available to be built
vcpkg install <pkg>... Install a package
vcpkg remove <pkg>... Uninstall a package
vcpkg remove --outdated Uninstall all out-of-date packages
vcpkg list List installed packages
vcpkg update Display list of packages for updating
vcpkg hash <file> [alg] Hash a file by specific algorithm, default SHA512
vcpkg integrate install Make installed packages available user-wide. Requires admin privileges on first use
vcpkg integrate remove Remove user-wide integration
vcpkg integrate project Generate a referencing nuget package for individual VS project use
vcpkg export <pkg>... [opt]... Exports a package
vcpkg edit <pkg> Open up a port for editing (uses %EDITOR%, default 'code')
vcpkg import <pkg> Import a pre-built library
vcpkg create <pkg> <url>
[archivename] Create a new package
vcpkg owns <pat> Search for files in installed packages
vcpkg cache List cached compiled packages
vcpkg version Display version information
vcpkg contact Display contact information to send feedback
Options:
--triplet <t> Specify the target architecture triplet.
(default: %VCPKG_DEFAULT_TRIPLET%, see 'vcpkg help triplet')
--vcpkg-root <path> Specify the vcpkg root directory
(default: %VCPKG_ROOT%)
For more help (including examples) see the accompanying README.md.
这里我们可以看到,很多有用的命令
- vcpkg search [pattern]
- 搜索一个C/C++库
- vcpkg install [pkg]
- 安装一个C/C++库
- vcpkg remove [pkg]
- 卸载(删除)一个C/C++库
下面举例,安装curl
这个常用的C语言写的网络库。
D:\git\vcpkg>.\vcpkg install curl
The following packages will be built and installed:
curl:x86-windows
* libssh2:x86-windows
Additional packages (*) will be installed to complete this operation.
Building package libssh2:x86-windows...
-- CURRENT_INSTALLED_DIR=D:/git/vcpkg/installed/x86-windows
-- DOWNLOADS=D:/git/vcpkg/downloads
-- CURRENT_PACKAGES_DIR=D:/git/vcpkg/packages/libssh2_x86-windows
-- CURRENT_BUILDTREES_DIR=D:/git/vcpkg/buildtrees/libssh2
-- CURRENT_PORT_DIR=D:/git/vcpkg/ports/libssh2/.
-- Downloading https://www.libssh2