【问题标题】:Add DLL reference to CMake based C# project将 DLL 引用添加到基于 CMake 的 C# 项目
【发布时间】:2018-12-26 06:06:07
【问题描述】:

我正在使用 CMake 生成一个 C# Wpf 项目。我按照下面的例子https://github.com/bemehiser/cmake_csharp_wpf_example/blob/master/Example/CMakeLists.txt

我的问题是:如何使用 CMake 向该项目添加第 3 方 .NET DLL 引用?

【问题讨论】:

标签: c# cmake


【解决方案1】:

我添加这个详细的答案是因为目前在高级网页上有相当多的误导性示例。 squareskittles 的回答是正确的,但没有详细讨论潜在的陷阱。

要设置 Visual Studio 托管项目 .NET 引用,如 WindowsBase,请使用 cmake 目标属性 VS_DOTNET_REFERENCES。将多个引用指定为一个列表很重要,而不是单独指定。因此,使用"PresentationCore;WindowsBase;..." 或使用如本文底部示例中的 cmake 列表是正确的。 单独添加多个引用是正确的,就像在网络上经常看到的那样:

# this is incorrect, and will only set the first reference, not all:
set_target_properties(${PROJECT_NAME} PROPERTIES VS_DOTNET_REFERENCES
    "PresentationCore"
    "WindowsBase"
    ...)

要添加二进制 Visual Studio 托管项目 .NET 引用,建议为每个二进制引用使用一个 cmake 目标属性 VS_DOTNET_REFERENCE_refname。要求<refname> 替换为参考文件名,不带扩展名。 为引用指定不同的<refname> 是正确的:

# this is incorrect, because the refname and the reference file name mismatch:
set_target_properties(${PROJECT_NAME} PROPERTIES VS_DOTNET_REFERENCE_mythriftlib
     "${CMAKE_INSTALL_PREFIX}/bin/Thrift.dll")

最后,同样重要的是,我更喜欢使用set_target_properties(${PROJECT_NAME} ...),而不是同样有效的set_properties(TARGET ${PROJECT_NAME} ...)。无论您选择哪种方式,在您的CMakeLists.txt 中保持一致是很好的。

这是一个正确的完整示例,可帮助您入门:

project(WPFGui VERSION 0.1.0 LANGUAGES CSharp)

include(CSharpUtilities)

add_executable(${PROJECT_NAME}
    App.config
    App.xaml
    App.cs
    MainWindow.xaml
    MainWindow.cs
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings)

csharp_set_designer_cs_properties(
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings)

csharp_set_xaml_cs_properties(
    App.xaml
    App.cs
    MainWindow.xaml
    MainWindow.cs)

set_source_files_properties(App.xaml PROPERTIES
    VS_XAML_TYPE "ApplicationDefinition")

set_target_properties(${PROJECT_NAME} PROPERTIES
    DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")

set_target_properties(${PROJECT_NAME} PROPERTIES
    WIN32_EXECUTABLE TRUE)

LIST(APPEND VS_DOTNET_REFERENCES "Microsoft.CSharp")
LIST(APPEND VS_DOTNET_REFERENCES "PresentationCore")
LIST(APPEND VS_DOTNET_REFERENCES "PresentationFramework")
LIST(APPEND VS_DOTNET_REFERENCES "System")
LIST(APPEND VS_DOTNET_REFERENCES "System.Xaml")
LIST(APPEND VS_DOTNET_REFERENCES "System.Xml")
LIST(APPEND VS_DOTNET_REFERENCES "System.Xml.Linq")
LIST(APPEND VS_DOTNET_REFERENCES "WindowsBase")

set_target_properties(${PROJECT_NAME} PROPERTIES
    VS_DOTNET_REFERENCES "${VS_DOTNET_REFERENCES}"
    VS_DOTNET_REFERENCE_Thrift "${CMAKE_INSTALL_PREFIX}/bin/Thrift.dll")

【讨论】:

  • 如何在 csproj 文件中将 Private 设置为 False。我不想在构建后将第三方 dll 复制到输出目录。
  • 亲爱的@PawełIwaneczko 很抱歉,但如果您针对“如何在 csproj 文件中将 Private 设置为 False”提出一个新问题会更好,因为它是一个不同的主题。祝您的编码一切顺利!
  • 谢谢,我不需要了。我找到了解决方案:cmake.org/cmake/help/latest/prop_tgt/…
【解决方案2】:

例如,您可以对名为MyLibrary.dll 的第 3 方库尝试以下操作:

add_executable(MyTarget
    #  ... list source files here ...
)

# Add the reference to the 3rd-party library: MyLibrary
set_property(TARGET MyTarget PROPERTY 
    VS_DOTNET_REFERENCE_MyLibrary "/path/to/the/libs/MyLibrary.dll")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多