次の方法で共有


チュートリアル: コマンド ラインから依存関係をインストールする

ヒント

依存関係をインストールする推奨方法については、「マニフェスト ファイルから依存関係をインストールする」 を参照してください。

警告

一部の vcpkg 機能は、クラシック モードでは使用できません。

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

クラシック モードでは、vcpkg をコマンド ライン インターフェイスとして使用して、共通の インストール ディレクトリに依存関係をインストールします。 通常、%VCPKG_ROOT%/installedにあります。ここで、%VCPKG_ROOT% は 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));
  }
}

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

2 - vcpkg をビルド システムと統合する

この手順では、プロジェクトをビルドするたびにプロジェクトの依存関係が自動的にインストールまたは復元されるように、vcpkg を CMake または MSBuild と統合する方法について説明します。

別のビルド システムを使用している場合は、次の手順に進みます。依存関係インストールします。

MSBuild から vcpkg を使用する方法の詳細を確認する

MSBuild プロジェクト で vcpkg使用するには、次のコマンドを実行します。

vcpkg integrate install

MSBuild 統合を初めて有効にする場合にのみ、vcpkg integrate install コマンドを実行する必要があります。 これにより、既存および将来のすべてのプロジェクトに対して MSBuild 統合が可能になります。 vcpkg integrate remove を使用して、MSBuild システム全体の統合を削除します。

この統合方法では、vcpkg がインストールされたパッケージが次のプロジェクト プロパティに自動的に追加されます:Include DirectoriesLink Directories、および Link Libraries。 さらに、ビルド後のアクションが作成され、必要な DLL がビルド出力フォルダーに確実にコピーされます。 これは、Visual Studio 2015 以降を使用するすべてのソリューションとプロジェクトで機能します。

3 - 依存関係のインストール

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

これらのパッケージをインストールするには、vcpkg install コマンドを使用します。

vcpkg install cxxopts fmt range-v3
$ ./vcpkg install cxxopts fmt range-v3
Computing installation plan...
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.
(omitted)
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)

4 - プロジェクトをビルドする

重要

インストールされているパッケージの トリプレット がプロジェクトの構成と一致していることを確認します。 64 ビット プロジェクトには x64-windows または x64-windows-static を使用し、32 ビット プロジェクトには x86-windows または x86-windows-static を使用します。

システム全体の統合を有効にした場合は、msbuild を実行してプロジェクトをビルドするだけです。

PS D:\projects\manifest-example> msbuild
MSBuild version 17.7.0-preview-23319-02+6829506b8 for .NET Framework
Build started 8/13/2023 3:07:36 PM.

Project "D:\projects\manifest-example\manifest-example.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  (omitted)
PrepareForBuild:
  (omitted)
InitializeBuildStatus:
  (omitted)
ComputeStdModulesCompileInputs:
  (omitted)
SetModuleDependencies:
VcpkgTripletSelection:
  Using triplet "x64-windows" from "D:\vcpkg\installed\x64-windows\"
  Using normalized configuration "Debug"
ClCompile:
  (omitted)
Link:
  (omitted)
AppLocalFromInstalled:
  pwsh.exe -ExecutionPolicy Bypass -noprofile -File "D:\vcpkg\scripts\buildsystems\msbuild\applocal.ps1" "D:\projects\manifest-example\x64\Debug\manifest-example.exe"
   "D:\vcpkg\installed\x64-windows\debug\bin" "x64\Debug\manifest-example.tlog\manifest-example.write.1u.tlog" "x64\Debug\vcpkg.applocal.log"
  D:\projects\manifest-example\x64\Debug\fmtd.dll
FinalizeBuildStatus:
  Deleting file "x64\Debug\manifest-example.tlog\unsuccessfulbuild".
  Touching "x64\Debug\manifest-example.tlog\manifest-example.lastbuildstate".
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 を使用して、単純なプロジェクトの依存関係をインストールしました。

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