教學課程:從命令行安裝相依性

提示

如需安裝相依性的建議方法,請參閱 從指令清單檔 安裝相依性。

警告

某些 vcpkg 功能不適用於傳統模式。

vcpkg 有兩種作業模式:傳統模式和指令清單模式。 本文說明如何使用傳統模式安裝套件。 對於大部分的使用者,我們建議改用指令清單模式。

在傳統模式中,您會使用 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));
  }
}

程式代碼會參考開放原始碼連結庫: cxxoptsfmtrange-v3,這些連結庫全都可在 位於的 vcpkg 公用登錄 https://github.com/Microsoft/vcpkg中使用。

2 - 整合 vcpkg 與建置系統

在此步驟中,我們會示範如何整合 vcpkg 與 CMake 或 MSBuild,以便在建置專案時自動安裝或還原您的專案相依性。

如果您使用不同的組建系統,請跳至下一個步驟: 安裝相依性

若要在 MSBuild 專案中使用 vcpkg,請執行下列命令:

vcpkg integrate install

您只需要 vcpkg integrate install 在第一次啟用 MSBuild 整合時執行命令。 這可讓您針對所有現有和未來的項目進行 MSBuild 整合。 使用 vcpkg integrate remove 移除 MSBuild 全系統整合。

此整合方法會自動將 vcpkg 安裝的套件新增至下列項目屬性: Include DirectoriesLink DirectoriesLink Libraries。 此外,這會建立建置後動作,以確保任何必要的 DLL 會複製到組建輸出資料夾中。 這適用於所有使用 Visual Studio 2015 或更新版本的解決方案和專案。

3 - 安裝相依性

程式代碼會參考開放原始碼連結庫: cxxoptsfmtrange-v3;這些全都可在 的 vcpkg 公用登錄 https://github.com/Microsoft/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 - 建置專案

重要

請確定 已安裝套件的三倍 符合專案的組態。 使用 x64-windowsx64-windows-static 用於 64 位專案,或x86-windowsx86-windows-static用於 32 位專案。

啟用全系統整合後,只要執行 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 作為命令行介面來安裝簡單專案的相依性。

以下是一些其他工作,可嘗試下一步: