チュートリアル: マニフェスト ファイルから依存関係をインストールする
vcpkg には、クラシック モードとマニフェスト モードの 2 つの操作モードがあります。 この記事では、マニフェスト モードを使用してパッケージをインストールする方法について説明します。これは、ほとんどのユーザーに推奨されるワークフローです。
マニフェスト モードでは、プロジェクトの直接依存関係を、vcpkg.json
という名前のマニフェスト ファイルで宣言します。
マニフェスト ファイルには、すべてのパッケージが共通の %VCPKG_ROOT%/installed
ディレクトリにインストールされるクラシック モードとは異なり、依存関係をインストールする独自の vcpkg_installed
ディレクトリがあります。 そのため、各プロジェクトは、独自のマニフェストと、他のプロジェクトの依存関係と競合しない依存関係の独自のセットを持つことができます。
マニフェスト モードは、バージョン管理 やカスタム レジストリ などの高度な機能を使用するためにも必要です。
このチュートリアルでは、次の方法について説明します。
- マニフェスト を使用してプロジェクトを作成する
- vcpkg をビルド システム と統合する
- 依存関係 をインストールする
- プロジェクト をビルドする
前提 条件
- 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
; を参照します。これらはすべて、https://github.com/Microsoft/vcpkgの vcpkg パブリック レジストリで使用できます。
これらの依存関係を宣言するには、プロジェクトと同じディレクトリに vcpkg.json
という名前のファイルを作成します。
vcpkg.json
:
{
"dependencies": [
"cxxopts",
"fmt",
"range-v3"
]
}
"dependencies"
一覧で直接依存関係を指定するだけで済みます。 vcpkg を実行すると、必要な推移的な依存関係が解決され、インストールされます。
2 - vcpkg をビルド システムと統合する
この手順では、プロジェクトをビルドするたびにプロジェクトの依存関係が自動的にインストールまたは復元されるように、vcpkg を CMake または MSBuild と統合する方法について説明します。
別のビルド システムを使用している場合は、次の手順に進みます。依存関係インストールします。
MSBuild から vcpkg を使用する方法の詳細を確認する
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 /p:VcpkgEnableManifest=true
を渡すことで、MSBuild 呼び出しでマニフェスト モードを有効にすることもできます。
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.
次の手順
このガイドでは、マニフェスト ファイルを使用して、単純なプロジェクトの依存関係をインストールしました。
次に試すその他のタスクを次に示します。
- トリプレット を使用して、カスタム プラットフォーム、コンパイラ、またはビルド アーキテクチャ用のパッケージをインストールする
- バージョン管理 を使用して、反復可能なビルドのバージョンをロックダウンする
- バイナリ キャッシュ を使用して、ローカルまたは継続的インテグレーションの実行間でバイナリを再利用する
- カスタム レジストリを使用してプライベート ライブラリを管理
vcpkg