向基于CMake的C#项目添加DLL引用
我正在使用CMake生成一个C#Wpf项目。我遵循下面的例子向基于CMake的C#项目添加DLL引用,c#,cmake,C#,Cmake,我正在使用CMake生成一个C#Wpf项目。我遵循下面的例子 我的问题是:如何使用CMake将第三方.NET DLL引用添加到此项目?例如,您可以为名为MyLibrary.DLL的第三方库尝试以下操作: add_executable(MyTarget # ... list source files here ... ) # Add the reference to the 3rd-party library: MyLibrary set_property(TARGET MyTarge
我的问题是:如何使用CMake将第三方.NET DLL引用添加到此项目?例如,您可以为名为
MyLibrary.DLL的第三方库尝试以下操作:
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")
我添加这个详细的答案是因为目前在高级网页上有很多误导性的例子。来自的答案是正确的,但没有包括对潜在陷阱的详细讨论
要设置Visual Studio托管的project.NET引用,如WindowsBase
,请使用cmake target属性。重要的是将多个引用指定为一个列表,而不是单独指定。因此,正确的做法是使用“PresentationCore;WindowsBase;…”
或使用本文底部示例中的cmake列表。单独添加多个引用是不正确的,就像在web上经常看到的那样:
# 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目标属性。要求将
替换为参考文件名,不带扩展名。为参考指定不同的
是不正确的:
# 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\u属性(target${PROJECT\u NAME}…)上使用set\u目标属性(${PROJECT\u 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”会更好,因为这是一个不同的主题。为您的编码做最好的准备!谢谢,我不再需要了。我找到了解决办法: