作为一个Win C++ 程序员,最大的痛苦就是经常要引入一些第三方库时如libyuv,libheif,libcurl,这种我们首先需要解决的问题就是如何获取一个编译好的库,其次这些库如何引入到工程也是个问题。有的时候为了编译一个库,配置各种环境可能就已经劝退了很多的人。
这时候无比羡慕python、node那种一条命令安装的优越,当然linux下可以通过 wget或yum来安装第三方库。这时候用隆重请出微软推出的vcpkg。瞬间让我们的开发体验翻倍。
vcpkg 官方连接
Vcpkg官方的说明是可帮助您在 Windows、 Linux 和 MacOS 上管理 C 和 C++ 库。我目前只在windows下进行了验证,开发效率瞬间提升。
- 包会有缓存管理和版本管理,可以升级版本
- 会自动检验包的依赖关系
- 无缝集成到Visual Studio中,无需设置值库的头文件所在目录(仅针对VS开发)
- Windows 7以上
- Git
- Visual Studio 2015 Update 3
这个配置要求真的不高,还在win7下开发的大部分都是涉密企业,这种在线安装库的形式基本上也不可能。
2.安装使用Vcpkg 使用Git安装Vcpkg在powshell中输入以下命令下载并安装vcpkg
> git clone https://github.com/microsoft/vcpkg > .vcpkgbootstrap-vcpkg.bat在环境变量中添加vcpkg
主要是便于我们后续在任意的路径下,直接使用vcpkg进行安装第三方库
以我为例:
- vcpkg安装在了“E:Gitvcpkg”
- 在环境变量Path路径的最后一行添加
安装库的命令
#package_name 替换自己需要安装的即可 > vcpkg install package_name #vcpkg install libyuv
vcpkg在Windows中默认编译并安装x86版本的库。 若要编译并安装x64版本限定
#安装64位 > vcpkg install package_name:x64-windows > vcpkg install package_name:x64 --triplet=x64-windows #安装32位 > vcpkg install package_name > vcpkg install package_name:x86-windows > vcpkg install package_name:x64 --triplet=x86-windows
会自动下载package对应的编译依赖库
安装前可以先进行搜索
> vcpkg search package_name
更多的命令可以通过vcpkg --help查看
> vcpkg --help > Commands: vcpkg search [pat] Search for packages available to be built vcpkg installvcpkg第三方库与CMake的结合... Install a package vcpkg remove ... 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 upgrade Rebuild all outdated packages vcpkg x-history (Experimental) Shows the history of CONTROL versions of a package vcpkg hash [alg] Hash a file by specific algorithm, default SHA512 vcpkg help topics Display the list of help topics vcpkg help Display help for a specific topic 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 integrate powershell Enable PowerShell tab-completion vcpkg export ... [opt]... Exports a package vcpkg edit Open up a port for editing (uses %EDITOR%, default 'code') vcpkg create [archivename] Create a new package vcpkg x-init-registry Initializes a registry in the directory vcpkg format-manifest --all Formats all vcpkg.json files. Run this before committing to vcpkg. vcpkg owns Search for files in installed packages vcpkg depend-info ... Display a list of dependencies for packages vcpkg env Creates a clean shell environment for development or compiling vcpkg version Display version information vcpkg contact Display contact information to send feedback Options: --triplet= Specify the target architecture triplet. See 'vcpkg help triplet' (default: %VCPKG_DEFAULT_TRIPLET%) --host-triplet= Specify the host architecture triplet. See 'vcpkg help triplet' (default: %VCPKG_DEFAULT_HOST_TRIPLET%) --overlay-ports= Specify directories to be used when searching for ports (also: %VCPKG_OVERLAY_PORTS%) --overlay-triplets= Specify directories containing triplets files (also: %VCPKG_OVERLAY_TRIPLETS%) --binarysource= Add sources for binary caching. See 'vcpkg help binarycaching' --x-asset-sources= Add sources for asset caching. See 'vcpkg help assetcaching' --downloads-root= Specify the downloads root directory (default: %VCPKG_DOWNLOADS%) --vcpkg-root= Specify the vcpkg root directory (default: %VCPKG_ROOT%) --x-buildtrees-root= (Experimental) Specify the buildtrees root directory --x-install-root= (Experimental) Specify the install root directory --x-packages-root= (Experimental) Specify the packages root directory --x-json (Experimental) Request JSON output @response_file Specify a response file to provide additional parameters
cmake -B build_folder -S . “-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake”
注意这里的CMake路径为vcpkg安装的CMake路径
以我为例:
安装在了“E:Gitvcpkg”
则Vcpkg的cmake安装在了
E:Gitvcpkgdownloadstoolscmake-3.22.2-windowscmake-3.22.2-windows-i386bin
最终执行的命令为
CMake中如何添加安装的库E:Gitvcpkgdownloadstoolscmake-3.22.2-windowscmake-3.22.2-windows-i386bincmake -B ./build_2019 -G “Visual Studio 15 2017 Win64” -S . “-DCMAKE_TOOLCHAIN_FILE=E:Gitvcpkgscriptsbuildsystemsvcpkg.cmake”
这个真的钞机无敌简单,因为你在安装之后,Vcpkg就直接把命令给了你,少走十年弯路啊!!!
> vcpkg install jsoncpp:x64-windows
Computing installation plan...
The following packages will be built and installed:
jsoncpp[core]:x64-windows -> 1.9.5
Detecting compiler hash for triplet "x64-windows"...
-- Automatically setting HTTP(S)_PROXY environment variables to "127.0.0.1:7890".
......省略
CMake Warning at scripts/cmake/vcpkg_copy_pdbs.cmake:44 (message):
Could not find a matching pdb file for:
E:/Git/vcpkg/packages/jsoncpp_x64-windows/bin/jsoncpp.dll
E:/Git/vcpkg/packages/jsoncpp_x64-windows/debug/bin/jsoncpp.dll
Call Stack (most recent call first):
ports/jsoncpp/portfile.cmake:30 (vcpkg_copy_pdbs)
scripts/ports.cmake:146 (include)
-- Installing: E:/Git/vcpkg/packages/jsoncpp_x64-windows/share/jsoncpp/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache:
......省略
jsoncpp provides CMake targets:
# this is heuristically generated, and may not be correct
#看到没?如何在CMake中添加已经直接把命令给你了!!!
find_package(jsoncpp CONFIG REQUIRED)
target_link_libraries(main PRIVATE jsoncpp_lib jsoncpp_object JsonCpp::JsonCpp)
总结
这一套组合拳下来,真的是让Windows下的C++ 开发一下子幸福感爆满



