次の方法で共有


チュートリアル: マニフェスト ファイルから依存関係をインストールする

vcpkg には、クラシック モードとマニフェスト モードの 2 つの操作モードがあります。 この記事では、マニフェスト モードを使用してパッケージをインストールする方法について説明します。これは、ほとんどのユーザーに推奨されるワークフローです。

マニフェスト モードでは、プロジェクトの直接依存関係をマニフェスト vcpkg.jsonファイルで宣言します。

マニフェスト ファイルには、すべてのパッケージが共通%VCPKG_ROOT%/installedディレクトリにインストールされるクラシック モードとは異なり、依存関係をインストールする独自vcpkg_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));
  }
}

このコードは、vcpkg パブリック レジストリhttps://github.com/Microsoft/vcpkgですべて使用できるオープン ソース ライブラリ cxxoptsfmt、、および range-v3; を参照します。

これらの依存関係を宣言するには、プロジェクトと同じディレクトリに名前の付いた 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 統合が可能になります。 MSBuild システム全体の統合を削除するために使用 vcpkg integrate remove します。

この統合メソッドは、vcpkg がインストールされたパッケージを次のプロジェクト プロパティに自動的に追加します。 Include DirectoriesLink DirectoriesLink 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 プロジェクトでは無効になっています。

プロジェクトでマニフェストを有効にするには、ファイルでプロパティをVcpkgEnableManifest.vcxproj設定します。

<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.

次のステップ

このガイドでは、マニフェスト ファイルを使用して、単純なプロジェクトの依存関係をインストールしました。

次に試すその他のタスクを次に示します。