VCpkg 開(kāi)源庫(kù)管理工具
Windows 下開(kāi)發(fā) C/C++ 程序,少不了編譯開(kāi)源的第三方庫(kù)扁掸。比如用于網(wǎng)絡(luò)連接的高性能庫(kù) libcurl翘县、用于壓縮解壓的 zlib 等等。使用這些庫(kù)開(kāi)發(fā)極大的方便了程序員谴分,使得我們不必重復(fù)造輪子锈麸。由于這些開(kāi)源庫(kù)絕大部分都來(lái)源于 Linux 系統(tǒng),其工程文件牺蹄、編譯系統(tǒng)都使用 gnu 系列工具忘伞,使得將其移植到 Windows 的 VC 開(kāi)發(fā)環(huán)境下一直是難點(diǎn)。
還需要考慮預(yù)先編譯出哪種類(lèi)型的開(kāi)源庫(kù)程序沙兰,比如:Debug 還是 Release氓奈、動(dòng)態(tài)庫(kù)還是靜態(tài)庫(kù)、MD 還是 MT鼎天、32 位還是 64 位探颈。光是這三種組合就有 16 種可能性。如果像 libcurl 這種還要考慮是否引用其他開(kāi)源庫(kù)的功能训措,那么編譯類(lèi)型的組合會(huì)更多。
VCpkg 就是解決這個(gè)問(wèn)題的:
- 自動(dòng)調(diào)用 git 等工具下載開(kāi)源庫(kù)源代碼光羞;
- 源碼包的緩存管理和版本管理绩鸣,可以升級(jí)版本;
- 緊密結(jié)合 CMake 輕松編譯纱兑;
- 依賴關(guān)系檢查呀闻,比如編譯 libcurl,會(huì)自動(dòng)下載 zlib潜慎、openssl 進(jìn)行編譯捡多;
- 無(wú)縫集成 Visual Studio蓖康,不需要設(shè)置庫(kù)文件、頭文件的所在目錄垒手,自動(dòng)集成蒜焊。
- Visual Studio 全平臺(tái)支持,支持 Debug/Release科贬、x86/x64 編譯泳梆,還支持 UWP、ARM 平臺(tái)的編譯榜掌。
一般使用流程:
- 執(zhí)行 vcpkg 安裝模塊优妙,等待編譯動(dòng)作完成;
- 執(zhí)行 vcpkg integrate 集成到項(xiàng)目或者 Visual Studio描孟,又或者全局集成忍饰;
- 在代碼中通過(guò)頭文件使用安裝好的模塊河咽;
vcpkg 主目錄文件夾結(jié)構(gòu):
| buildtrees | 所有下載好的 library 源代碼和構(gòu)建目錄 |
| docs | 文檔與示例 |
| downloads | 下載緩沖文件夾,執(zhí)行安裝命令時(shí)會(huì)先查詢這里 |
| installed | 包含安裝好的 library 頭文件和編譯文件 |
| packages | 內(nèi)部文件夾邪意,在安裝時(shí)用到 |
| ports | 包含分類(lèi)中的庫(kù)描述文件,包含版本朴恳、下載地址等 |
| scripts | 腳本目錄抄罕,如 cmake, powershell 腳本 |
| toolsrc | VcPkg C++ 源代碼和組件 |
| triplets | 包含支持架構(gòu)配置文件,如 x86-windows于颖、x64-uwp |
vcpkg 安裝依賴模塊的基本執(zhí)行流程:
- 環(huán)境初始化
- 下載源代碼呆贿,如果已經(jīng)在 cache 中,則跳過(guò)下載環(huán)節(jié)
- 校驗(yàn)文件有效性
- 解壓縮源代碼
- 利用配套工具配置源碼工程森渐,如 cmake做入,如果是 ffmpeg 則用 msys2
- 執(zhí)行 MSBuild 編譯源碼,一般會(huì)同時(shí)編譯 Release 和 Debug 版本同衣。
- 把編譯好的文件拷貝到相關(guān)目錄中去竟块,一般是 installed 目錄
如果沒(méi)有在 vcpkg 主目錄運(yùn)行,可能會(huì)遇到 Error: Could not detect vcpkg-root耐齐。
使用 PowerShell 執(zhí)行 Vcpkg 工程目錄下的 bootstrap-vcpkg.bat 進(jìn)行編譯浪秘,會(huì)在同級(jí)目錄下生成 vcpkg.exe 文件。
命令使用示范:
>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 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 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
...
>vcpkg search assimp
>vcpkg search | findstr assimp
assimp 5.0.1 The Open Asset import library
magnum-plugins[assimpimporter] AssimpImporter plugin
>vcpkg search | findstr glu
aws-sdk-cpp[glue] C++ SDK for the AWS glue service
freeglut 3.2.1-4 Open source implementation of GLUT with source and binary backwards compatibil...
glui 2019-11-30 GLUI is a GLUT-based C++ user interface library
mathgl[glut] glut module
>vcpkg install assimp:
Computing installation plan...
The following packages will be built and installed:
assimp[core]:x86-windows
* minizip[core]:x86-windows
* rapidjson[core]:x86-windows
* zlib[core]:x86-windows
Additional packages (*) will be modified to complete this operation.
Warning: The following VS instances are excluded because the English language pack is unavailable.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
Please install the English language pack.
安裝具有 Cuda 加速的 opencv 庫(kù):
vcpkg search opencv
vcpkg install opencv[cuda]:x64-windows
vcpkg --triplet x64-windows install opencv[cuda]
支持的架構(gòu)組合查詢:
>vcpkg help triplet
VCPKG built-in triplets | VCPKG community triplets |
---|---|
arm-uwp | arm-ios |
arm64-windows | arm-mingw |
x64-linux | arm-windows |
x64-osx | arm64-ios |
x64-uwp | arm64-mingw |
x64-windows-static | arm64-uwp |
x64-windows | arm64-windows-static |
x86-windows | wasm32-emscripten |
x64-ios | |
x64-mingw | |
x64-osx-dynamic | |
x64-windows-static-md | |
x86-ios | |
x86-mingw | |
x86-uwp | |
x86-windows-static-md | |
x86-windows-static |
作為 MinGW 的用戶埠况,我非愿意看到 triplet 列表中有 x64-mingw 和 x86-mingw 的身影耸携。
可以設(shè)置默認(rèn)的架構(gòu):
VCPKG_DEFAULT_TRIPLET=x64-windows
vcpkg 的 Triplet files 是用來(lái)記錄和庫(kù)相關(guān)的 OS、CPU辕翰、Compiler夺衍、Runtime 等信息的文件,它包含了編譯模塊時(shí)使用的工具鏈喜命。
安裝第三方的 MinGW 架構(gòu)依賴庫(kù)時(shí)出現(xiàn)錯(cuò)誤:
>vcpkg install assimp:x64-mingw
Computing installation plan...
The following packages will be built and installed:
assimp[core]:x64-mingw
* minizip[core]:x64-mingw
* rapidjson[core]:x64-mingw
* zlib[core]:x64-mingw
Additional packages (*) will be modified to complete this operation.
Unable to determine toolchain to use for triplet x64-mingw with CMAKE_SYSTEM_NAME MinGW
提示信息表明沟沙,vcpkg 無(wú)法從 CMAKE_SYSTEM_NAME 指定的 x64-mingw 架構(gòu)確定需要用到的工具鏈:
triplets/community/x64-mingw.cmake
這個(gè)問(wèn)題有點(diǎn)惡心河劝,因?yàn)?vcpkg 的源代碼忽略了 MinGW,解決方法是修改 build.cpp 重新編譯 vcpkg:
diff --git a/toolsrc/src/vcpkg/build.cpp b/toolsrc/src/vcpkg/build.cpp
index c61c6b7..d7c78aa 100644
--- a/toolsrc/src/vcpkg/build.cpp
+++ b/toolsrc/src/vcpkg/build.cpp
@@ -558,6 +558,10 @@ namespace vcpkg::Build
else if (cmake_system_name == "Android")
{
return m_paths.scripts / fs::u8path("toolchains/android.cmake");
+ }
+ else if (cmake_system_name == "MinGW")
+ {
+ return m_paths.scripts / fs::u8path("toolchains/mingw.cmake");
}
else if (cmake_system_name.empty() || cmake_system_name == "Windows" || cmake_system_name == "WindowsStore")
{
--
參考 https://github.com/microsoft/vcpkg/issues/12065
安裝好依賴模塊后矛紫,接著是將 vcpkg 集成到項(xiàng)目或進(jìn)行全局集成:
>vcpkg integrate install
Applied user-wide integration for this vcpkg root.
All MSBuild C++ projects can now #include any installed libraries.
Linking will be handled automatically.
Installing new libraries will make them instantly available.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
使用 CMake 的編譯項(xiàng)目中使用 vcpkg 安裝的庫(kù)赎瞎,最佳方式指定 CMake (Toolchain File) 工具鏈文件 scripts/buildsystems/vcpkg.cmake
,讓 find_package()
命令發(fā)現(xiàn)安裝的庫(kù)含衔。
要使用這個(gè)文件煎娇,通過(guò)命令參數(shù)傳入 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)
注意杭隙,要在 project()
命令之前設(shè)置哟绊。另外,類(lèi)似 CMAKE_SYSTEM_NAME
,CMAKE_C_COMPILER
等這些變量都要在 project()
命令之前設(shè)定痰憎,不然 CMake 會(huì)按照默認(rèn)的設(shè)置處理票髓。
如果電腦中沒(méi)有安裝 cmake,vcpkg 會(huì)自動(dòng)下載 cmake portable 版本铣耘。
集成安裝:
命令 | 說(shuō)明 |
---|---|
vcpkg integrate install | 為所有用戶集成安裝依賴包 |
vcpkg integrate remove | 為所有用戶移除集成安裝 |
vcpkg integrate project | 為獨(dú)立 VS 項(xiàng)目創(chuàng)建 nuget 包引用 |
vcpkg integrate powershell | 為 PowerShell Tab 自動(dòng)完成集成 |
---------------------------- | --------------------------------- |
依賴包的管理:
vcpkg.exe remove assimp
vcpkg.exe remove --outdated
vcpkg.exe list
vcpkg.exe export assimp --7zip
導(dǎo)出時(shí)必須指定導(dǎo)出的包格式洽沟。vcpkg支持5種導(dǎo)出包格式,有:
參數(shù) | 格式 |
---|---|
–raw | 以不打包的目錄格式導(dǎo)出 |
–nuget | 以 nuget 包形式導(dǎo)出 |
–ifw | 基于 IFW 的安裝包 |
–zip | 以 zip 壓縮包形式導(dǎo)出 |
–7zip | 以 7z 壓縮包形式導(dǎo)出 |