다음을 통해 공유


자습서: 매니페스트 파일에서 종속성 설치

vcpkg에는 클래식 모드와 매니페스트 모드의 두 가지 작업 모드가 있습니다. 이 문서에서는 대부분의 사용자에게 권장되는 워크플로인 매니페스트 모드를 사용하여 패키지를 설치하는 방법을 설명합니다.

매니페스트 모드에서는 프로젝트의 직접 종속성을 매니 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));
  }
}

코드는 오픈 소스 라이브러리를 cxxopts참조합니다. . fmtrange-v3https://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 통합이 가능합니다. 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.

다음 단계

이 가이드에서는 매니페스트 파일을 사용하여 간단한 프로젝트에 대한 종속성을 설치했습니다.

다음에 시도할 몇 가지 추가 작업은 다음과 같습니다.