[英]Including nuget packages in VS2019 C++ cross platform program
I've been tasked with starting to lay out a C++ cross platform program with CMake.我的任务是开始用 CMake 设计一个 C++ 跨平台程序。 One of our main dependencies involves in-house nuget packages.
我们的主要依赖项之一涉及内部 nuget 包。 With our Windows C++ projects, I'd just right click the project and choose Manage Nuget Packages .
对于我们的 Windows C++ 项目,我只需右键单击该项目并选择Manage Nuget Packages 。 In the cross platform, there's no such option, and I am struggling to find any relevant information on how I'd go about including those dependencies.
在跨平台中,没有这样的选项,我正在努力寻找有关如何包含这些依赖项的 go 的任何相关信息。 Can anyone link me to any good sources of info, or demo?
任何人都可以将我链接到任何好的信息来源或演示吗?
EDIT : As of CMake 3.15, CMake supports referencing Nuget packages with VS_PACKAGE_REFERENCES
.编辑:从 CMake 3.15 开始,CMake 支持使用
VS_PACKAGE_REFERENCES
引用 Nuget 包。 Now, this is a much cleaner solution than the work-around proposed below.现在,这是一个比下面提出的解决方法更清洁的解决方案。 To add a Nuget package reference to a CMake target, use the package name and package version separated by an underscore
_
; To add a Nuget package reference to a CMake target, use the package name and package version separated by an underscore
_
; here is an example for BouncyCastle
version 1.8.5:这是
BouncyCastle
1.8.5 版的示例:
set_property(TARGET MyApplication
PROPERTY VS_PACKAGE_REFERENCES "BouncyCastle_1.8.5"
)
Prior to CMake 3.15, CMake has no built-in commands for Nuget support, so you will have to use the nuget
command line utilities to include Nuget dependencies using CMake. Prior to CMake 3.15, CMake has no built-in commands for Nuget support, so you will have to use the
nuget
command line utilities to include Nuget dependencies using CMake.
You can use CMake's find_program()
to locate the nuget
command line utility (once installed), coupled with add_custom_command()
or execute_process()
to execute nuget
commands from CMake.您可以使用 CMake 的
find_program()
来定位nuget
命令行实用程序(安装后),再加上add_custom_command()
或execute_process()
从 ZDF49ADAB9245E0C191764 执行nuget
命令。 The answers to this question discuss in more detail, but it could essentially look something like this:这个问题的答案更详细地讨论,但它基本上看起来像这样:
# 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()
This assumes you have an existing packages.config
file listing the nuget dependencies for your project.这假设您有一个现有的
packages.config
文件,其中列出了您的项目的 nuget 依赖项。
To tie dependencies to a specific target, you (unfortunately) have to use the full path to where nuget
placed the assembly/library.要将依赖项绑定到特定目标,您(不幸的是)必须使用
nuget
放置程序集/库的位置的完整路径。
For .NET nuget packages this would look like this:对于 .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
)
For C++-flavored nuget packages, it could look like this:对于 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
)
As an aside, CMake does support the creation of Nuget packages with CPack.顺便说一句,CMake确实支持使用 CPack创建Nuget 包。 Here is the documentation for the CPack Nuget generator.
这是 CPack Nuget 生成器的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.