培训
模块
创建新的 .NET 项目并使用包依赖项 - Training
创建 .NET 项目并了解如何在项目中添加包和管理包依赖项。 通过 Visual Studio Code 使用 .NET Core CLI 和 NuGet 注册表,向 C# 应用程序添加库和工具。
提示
有关安装依赖项的建议方法,请参阅 “从清单文件安装依赖项”。
警告
某些 vcpkg 功能在经典模式下不可用。
vcpkg 有两种作模式:经典模式和清单模式。 本文介绍如何使用经典模式安装包。 对于大多数用户,我们建议改用清单模式。
在经典模式下,使用 vcpkg 作为命令行接口在公共 安装目录安装依赖项。 通常,位于 %VCPKG_ROOT%/installed
,其中 %VCPKG_ROOT%
是 vcpkg 的安装目录。
在本教程中,你将了解如何:
在新文件夹中,使用以下内容创建名为 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/vcpkgvcpkg 公共注册表中都可用。
在此步骤中,我们将演示如何将 vcpkg 与 CMake 或 MSBuild 集成,以便在生成项目时自动安装或还原项目依赖项。
如果使用其他生成系统,请跳到下一步:安装依赖项。
若要在 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 或更高版本的解决方案和项目。
代码引用了开源库:cxxopts
、fmt
和 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)
重要
请确保您已安装的软件包中的 三元组 与项目的配置匹配。 对 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 作为命令行接口为简单项目安装了依赖项。
下面是接下来要尝试的一些其他任务:
培训
模块
创建新的 .NET 项目并使用包依赖项 - Training
创建 .NET 项目并了解如何在项目中添加包和管理包依赖项。 通过 Visual Studio Code 使用 .NET Core CLI 和 NuGet 注册表,向 C# 应用程序添加库和工具。