【问题标题】:Including nuget packages in VS2019 C++ cross platform programVS2019 C++跨平台程序中包含nuget包
【发布时间】:2019-10-21 14:34:24
【问题描述】:

我的任务是开始使用 CMake 设计一个 C++ 跨平台程序。我们的主要依赖项之一涉及内部 nuget 包。对于我们的 Windows C++ 项目,我只需右键单击该项目并选择Manage Nuget Packages。在跨平台中,没有这样的选项,我正在努力寻找有关如何包含这些依赖项的任何相关信息。谁能将我链接到任何好的信息来源或演示?

【问题讨论】:

  • 对于它的价值,我觉得微软的 C++ 团队推荐 vcpkg 而不是 nuget,所以也许你可以调查一下。它可能会更好。 github.com/microsoft/vcpkg
  • @Jason CMake 3.15 支持添加 Nuget 包引用,这提供了一个更简单的解决方案。请参阅我的更新答案。
  • 感谢更新!

标签: c++ linux windows cmake nuget


【解决方案1】:

编辑:从 CMake 3.15 开始,CMake 支持使用 VS_PACKAGE_REFERENCES 引用 Nuget 包。现在,这是一个比下面提出的解决方法更清洁的解决方案。要将 Nuget 包引用添加到 CMake 目标,请使用以下划线 _ 分隔的包名称和包版本;这是BouncyCastle 1.8.5 版的示例:

set_property(TARGET MyApplication
    PROPERTY VS_PACKAGE_REFERENCES "BouncyCastle_1.8.5"
)

请注意,此解决方案仅适用于 C# 或混合 C#/C++ 项目。如here 所述,Microsoft 不支持 PackageReference 用于纯 C++ 项目。


在 CMake 3.15 之前,CMake 没有用于 Nuget 支持的内置命令,因此您必须使用 nuget command line utilities 来包含使用 CMake 的 Nuget 依赖项。

您可以使用 CMake 的 find_program() 找到 nuget 命令行实用程序(安装后),再加上 add_custom_command()execute_process() 从 CMake 执行 nuget 命令。对此question 的答案进行了更详细的讨论,但基本上看起来可能是这样的:

# Find Nuget (install the latest CLI here: https://www.nuget.org/downloads).
find_program(NUGET nuget)
if(NOT NUGET)
    message(FATAL "CMake could not find the nuget command line tool. Please install it!")
else()
    # Copy the Nuget config file from source location to the CMake build directory.
    configure_file(packages.config.in packages.config COPYONLY)
    # Run Nuget using the .config file to install any missing dependencies to the build directory.
    execute_process(COMMAND 
        ${NUGET} restore packages.config -SolutionDirectory ${CMAKE_BINARY_DIR}
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    )
endif()

这假设您有一个现有的packages.config 文件,其中列出了您的项目的 nuget 依赖项。

要将依赖项绑定到特定目标,您(很遗憾)必须使用 nuget 放置程序集/库的完整路径。

对于 .NET nuget 包,如下所示:

# Provide the path to the Nuget-installed references.
set_property(TARGET MyTarget PROPERTY 
    VS_DOTNET_REFERENCE_MyReferenceLib
    ${CMAKE_BINARY_DIR}/packages/path/to/nuget/lib/MyReferenceLib.dll
)

对于 C++ 风格的 nuget 包,它可能如下所示:

add_library(MyLibrary PUBLIC
    MySource.cpp
    MyClass1.cpp
    ...
)

# Provide the path to the Nuget-installed libraries.
target_link_libraries(MyLibrary PUBLIC 
    ${CMAKE_BINARY_DIR}/packages/path/to/nuget/lib/MyCppLib.dll
)

顺便说一句,CMake 确实支持使用 CPack创建 Nuget 包。这是 CPack Nuget 生成器的documentation

【讨论】:

  • 我遇到了上面提到的那个帖子,但希望在被问到之后的 6 年里事情发生了变化。我有我可以移植的 package.config 文件,这没什么大不了的。 target_link_libraries 的东西会进入什么文件?到目前为止,要加快速度,学习曲线看起来相当陡峭。
  • @Jason 虽然 CMake 已经改进以支持作为一流语言的 C#,但遗憾的是它还没有开发出对 Nuget(通常与 C# 一起使用)的完全支持。至于您的target_link_libraries() 命令,您可以将其直接放在您定义自己的 C++ 库的位置之后。我扩展了我的答案以显示这一点。这会将 Nuget 安装的库链接到您自己的库。您还可以根据需要向此调用添加许多其他 Nuget 依赖项。
  • 欣赏它。我怀疑我得做很多研究。
  • @Jason 如果您是 CMake 新手,有一篇很好的文章 here 描述了 CMake 魔法的工作原理。这里的 CMake 标签page 也有几个有用的链接。
  • @squareskittles 使用 cmake 3.15 解决方案,还需要其他步骤吗? Cmake 完成时没有错误或警告,但我在 VS 中看不到任何 nuget 包。使用set_property(TARGET myproj PROPERTY VS_PACKAGE_REFERENCES "Microsoft.Windows.CppWinRT_2.0.200316.3")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 2020-01-19
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多