为什么要用Vcpkg?
作为一个Win C++ 程序员,最大的痛苦就是经常要引入一些第三方库时如libyuv,libheif,libcurl,这种我们首先需要解决的问题就是如何获取一个编译好的库,其次这些库如何引入到工程也是个问题。有的时候为了编译一个库,配置各种环境可能就已经劝退了很多的人。 这时候无比羡慕python、node那种一条命令安装的优越,当然linux下可以通过 wget或yum来安装第三方库。这时候用隆重请出微软推出的vcpkg。瞬间让我们的开发体验翻倍。 vcpkg 官方连接 Vcpkg官方的说明是可帮助您在 Windows、 Linux 和 MacOS 上管理 C 和 C++ 库。我目前只在windows下进行了验证,开发效率瞬间提升。
Vcpkg优点
- 包会有缓存管理和版本管理,可以升级版本
- 会自动检验包的依赖关系
- 无缝集成到Visual Studio中,无需设置值库的头文件所在目录(仅针对VS开发)
1.Vcpkg 前置要求
- Windows 7以上
- Git
- Visual Studio 2015 Update 3
这个配置要求真的不高,还在win7下开发的大部分都是涉密企业,这种在线安装库的形式基本上也不可能。
2.安装使用Vcpkg
使用Git安装Vcpkg
在powshell中输入以下命令下载并安装vcpkg
> git clone https://github.com/microsoft/vcpkg
> .\vcpkg\bootstrap-vcpkg.bat
在环境变量中添加vcpkg
主要是便于我们后续在任意的路径下,直接使用vcpkg进行安装第三方库 以我为例:
- vcpkg安装在了“E:\Git\vcpkg”
- 在环境变量Path路径的最后一行添加
vcpkg安装库注意点
安装库的命令
> vcpkg install package_name
vcpkg在Windows中默认编译并安装x86版本的库。 若要编译并安装x64版本限定
> vcpkg install package_name:x64-windows
> vcpkg install package_name:x64 --triplet=x64-windows
> 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 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 upgrade Rebuild all outdated packages
vcpkg x-history <pkg> (Experimental) Shows the history of CONTROL versions of a package
vcpkg hash <file> [alg] Hash a file by specific algorithm, default SHA512
vcpkg help topics Display the list of help topics
vcpkg help <topic> 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 <pkg>... [opt]... Exports a package
vcpkg edit <pkg> Open up a port for editing (uses %EDITOR%, default 'code')
vcpkg create <pkg> <url> [archivename]
Create a new package
vcpkg x-init-registry <path> Initializes a registry in the directory <path>
vcpkg format-manifest --all Formats all vcpkg.json files. Run this before committing to vcpkg.
vcpkg owns <pat> Search for files in installed packages
vcpkg depend-info <pkg>... 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=<t> Specify the target architecture triplet. See 'vcpkg help triplet'
(default: %VCPKG_DEFAULT_TRIPLET%)
--host-triplet=<t> Specify the host architecture triplet. See 'vcpkg help triplet'
(default: %VCPKG_DEFAULT_HOST_TRIPLET%)
--overlay-ports=<path> Specify directories to be used when searching for ports
(also: %VCPKG_OVERLAY_PORTS%)
--overlay-triplets=<path> Specify directories containing triplets files
(also: %VCPKG_OVERLAY_TRIPLETS%)
--binarysource=<path> Add sources for binary caching. See 'vcpkg help binarycaching'
--x-asset-sources=<path> Add sources for asset caching. See 'vcpkg help assetcaching'
--downloads-root=<path> Specify the downloads root directory
(default: %VCPKG_DOWNLOADS%)
--vcpkg-root=<path> Specify the vcpkg root directory
(default: %VCPKG_ROOT%)
--x-buildtrees-root=<path> (Experimental) Specify the buildtrees root directory
--x-install-root=<path> (Experimental) Specify the install root directory
--x-packages-root=<path> (Experimental) Specify the packages root directory
--x-json (Experimental) Request JSON output
@response_file Specify a response file to provide additional parameters
vcpkg第三方库与CMake的结合
cmake -B build_folder -S . “-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake”
注意这里的CMake路径为vcpkg安装的CMake路径 以我为例:
安装在了“E:\Git\vcpkg”
则Vcpkg的cmake安装在了
E:\Git\vcpkg\downloads\tools\cmake-3.22.2-windows\cmake-3.22.2-windows-i386\bin
最终执行的命令为
E:\Git\vcpkg\downloads\tools\cmake-3.22.2-windows\cmake-3.22.2-windows-i386\bin\cmake -B ./build_2019 -G “Visual Studio 15 2017 Win64” -S . “-DCMAKE_TOOLCHAIN_FILE=E:\Git\vcpkg\scripts\buildsystems\vcpkg.cmake”
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:
find_package(jsoncpp CONFIG REQUIRED)
target_link_libraries(main PRIVATE jsoncpp_lib jsoncpp_object JsonCpp::JsonCpp)
总结
这一套组合拳下来,真的是让Windows下的C++ 开发一下子幸福感爆满
|