指南:從清單檔案安裝依賴項
vcpkg 有兩種作業模式:傳統模式和指令清單模式。 本文說明如何使用指令清單模式來安裝套件,這是大部分用戶的建議工作流程。
在指令清單模式中,您會在名為 vcpkg.json
的指令清單檔中宣告專案的直接相依性。
清單檔案有自己的 vcpkg_installed
目錄,用來安裝相依性,不同於傳統模式下,所有套件都安裝在通用的 %VCPKG_ROOT%/installed
目錄中。 因此,每個專案可以有自己的指令清單和本身的相依性集,而不會與其他專案的相依性衝突。
清單模式也需要使用進階功能,例如 版本控制 和 自訂註冊表。
在本教學課程中,您將瞭解如何:
先決條件
- vcpkg
- 終端機
- 程式代碼編輯器
- C++編譯程式
- (選擇性)CMake 或 MSBuild
1 - 使用指令清單建立專案
在新資料夾中,使用下列內容建立名為 main.cxx
的來源檔案:
#include <cxxopts.hpp>
#include <fmt/format.h>
#include <range/v3/view.hpp>
namespace view = ranges::views;
int fib(int x)
{
int a = 0, b = 1;
for (int it : view::repeat(0) | view::take(x))
{
(void)it;
int tmp = a;
a += b;
b = tmp;
}
return a;
}
int main(int argc, char **argv)
{
cxxopts::Options options("fibo", "Print the fibonacci sequence up to a value 'n'");
options.add_options()("n,value", "The value to print to", cxxopts::value<int>()->default_value("10"));
auto result = options.parse(argc, argv);
auto n = result["value"].as<int>();
for (int x : view::iota(1) | view::take(n))
{
fmt::print("fib({}) = {}\n", x, fib(x));
}
}
程式碼引用了開放原始碼庫:cxxopts
、fmt
和 range-v3
,它們全都可以在 vcpkg 公共登錄中的 https://github.com/Microsoft/vcpkg找到。
若要宣告這些相依性,請在與專案相同的目錄中建立名為 vcpkg.json
的檔案:
vcpkg.json
:
{
"dependencies": [
"cxxopts",
"fmt",
"range-v3"
]
}
您只需要在 "dependencies"
清單中指定您的直接相依性。 執行時,vcpkg 會解析並安裝任何必要的可轉移相依性。
2 - 整合 vcpkg 與建置系統
在此步驟中,我們會示範如何整合 vcpkg 與 CMake 或 MSBuild,以便在建置專案時自動安裝或還原您的專案相依性。
如果您使用不同的組建系統,請跳至下一個步驟:安裝相依性。
若要在 MSBuild 專案中使用 vcpkg,請執行下列命令:
vcpkg integrate install
您只需要在第一次啟用 MSBuild 整合時執行 vcpkg integrate install
命令。 這可讓您針對所有現有和未來的項目進行 MSBuild 整合。 使用 vcpkg integrate remove
移除 MSBuild 全系統整合。
此整合方法會自動將 vcpkg 安裝的套件新增至下列專案屬性:Include Directories
、Link Directories
和 Link Libraries
。 此外,這會建立建置後動作,以確保任何必要的 DLL 會複製到組建輸出資料夾中。 這適用於所有使用 Visual Studio 2015 或更新版本的解決方案和專案。
3 - 安裝依賴項
如果您使用 CMake 或 MSBuild 並遵循上一個步驟,您可以跳到下一個步驟:建置專案。
如果您使用不同的建置系統或想要手動安裝相依性,您只需要在包含指令清單檔案的目錄中執行 vcpkg install
。
PS D:\projects\manifest-example> vcpkg install
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
cxxopts:x64-windows -> 3.1.1
fmt:x64-windows -> 10.0.0
range-v3:x64-windows -> 0.12.0#1
* vcpkg-cmake:x64-windows -> 2023-05-04
* vcpkg-cmake-config:x64-windows -> 2022-02-06#1
Additional packages (*) will be modified to complete this operation.
Installing 1/5 vcpkg-cmake-config:x64-windows...
Installing 2/5 vcpkg-cmake:x64-windows...
Installing 3/5 cxxopts:x64-windows...
Installing 4/5 fmt:x64-windows...
Installing 5/5 range-v3:x64-windows...
Total install time: 48 s
cxxopts provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(cxxopts CONFIG REQUIRED)
target_link_libraries(main PRIVATE cxxopts::cxxopts)
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
range-v3 provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(range-v3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE range-v3::meta range-v3::concepts range-v3::range-v3)
當命令完成時,所有建置的套件都會出現在 vcpkg_installed
目錄中。 此目錄的特定位置取決於您的組建系統;通常,在建置系統的預設輸出資料夾內,或 vcpkg.json
檔案旁。
4 - 建置專案
根據預設,MSBuild 專案中會停用指令清單模式。
若要在您的項目中啟用指令清單,請在 .vcxproj
檔案中設定 VcpkgEnableManifest
屬性:
<PropertyGroup Label="Vcpkg">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
或者,您可以在 MSBuild 呼叫中啟用清單模式,並將 msbuild /p:VcpkgEnableManifest=true
作為參數傳遞。
PS D:\projects\manifest-example> msbuild /p:VcpkgEnableManifest=true
MSBuild version 17.7.0-preview-23319-02+6829506b8 for .NET Framework
Build started 8/11/2023 11:29:50 AM.
Project "D:\projects\manifest-example\manifest-example.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
Building solution configuration "Debug|x64".
Project "D:\projects\manifest-example\manifest-example.sln" (1) is building "D:\projects\manifest-example\manifest-example.vcxproj" (2) on node 1 (default targets).
PrepareForBuild:
(omitted)
InitializeBuildStatus:
(omitted)
ComputeStdModulesCompileInputs:
(omitted)
SetModuleDependencies:
Creating directory "x64\Debug\manifest.ceffc6eb_MD.tlog\".
VcpkgTripletSelection:
Using triplet "x64-windows" from "D:\projects\manifest-example\vcpkg_installed\x64-windows\x64-windows\"
Using normalized configuration "Debug"
VcpkgInstallManifestDependencies:
Installing vcpkg dependencies to D:\projects\manifest-example\vcpkg_installed\x64-windows\
Creating directory "D:\projects\manifest-example\vcpkg_installed\x64-windows\".
"D:\vcpkg\vcpkg.exe" install --x-wait-for-lock --triplet "x64-windows" --vcpkg-root "D:\vcpkg\" "--x-manifest-root=D:\projects\manifest-example\" "--x-install-root=D:\projects\manifest-example\vcpkg_installed\x64-windows\"
"D:\vcpkg\vcpkg.exe" install --x-wait-for-lock --triplet "x64-windows" --vcpkg-root "D:\vcpkg\" "--x-manifest-root=D:\projects\manifest-example\" "--x-install-root=D:\projects\manifest-example\vcpkg_installed\x64-windows\"
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
cxxopts:x64-windows -> 3.1.1
fmt:x64-windows -> 10.0.0
range-v3:x64-windows -> 0.12.0#1
* vcpkg-cmake:x64-windows -> 2023-05-04
* vcpkg-cmake-config:x64-windows -> 2022-02-06#1
(omitted)
ClCompile:
(omitted)
Link:
(omitted)
AppLocalFromInstalled:
pwsh.exe -ExecutionPolicy Bypass -noprofile -File "D:\vcpkg\scripts\buildsystems\msbuild\applocal.ps1" "D:\projects\manif
est-mode-msbuild\x64\Debug\manifest-example.exe" "D:\projects\manifest-example\vcpkg_installed\x64-windows\x64-windows\debug\bin"
"x64\Debug\manifest.ceffc6eb.tlog\manifest-example.write.1u.tlog" "x64\Debug\vcpkg.applocal.log"
D:\projects\manifest-example\x64\Debug\fmtd.dll
FinalizeBuildStatus:
(omitted)
Done Building Project "D:\projects\manifest-example\manifest-example.vcxproj" (default targets).
Done Building Project "D:\projects\manifest-example\manifest-example.sln" (default targets).
Build succeeded.
後續步驟
在本指南中,您已使用 manifest 檔案安裝了一個簡單專案所需的相依性。
以下是一些其他任務,接下來嘗試:
- 使用 triplets 安裝自訂平臺、編譯器或建置架構的套件
- 使用 版本設定 鎖定您的可重複組建版本
- 利用 二進位快取,在本機或持續整合運行中重複利用二進位檔
- 使用自定義註冊來管理私人程式庫