将DLL参考添加到基于CMake的C#项目中 [英] Add DLL reference to CMake based C# project
问题描述
我正在使用CMake生成C#Wpf项目.我遵循以下示例 https://github.com/bemehiser/cmake_csharp_wpf_example/blob/master/Example/CMakeLists.txt
I am generating an C# Wpf project using CMake. I followed the following example https://github.com/bemehiser/cmake_csharp_wpf_example/blob/master/Example/CMakeLists.txt
我的问题是:如何使用CMake向该项目添加第三方.NET DLL参考?
My question is: How can add an 3rd party .NET DLL reference to this project using CMake ?
推荐答案
我要添加这个详细答案,因为当前在高级网页上有很多误导性的示例. skittlettles 的答案是正确的,但没有详细讨论潜在的陷阱.
I'm adding this detailed answer because currently there are quite a number of misleading examples on high-ranking webpages. The answer from squareskittles is correct, but does not include a detailed discussion of potential pitfalls.
要设置像 WindowsBase
这样的Visual Studio托管项目.NET引用,请使用cmake目标属性或使用cmake列表(如本文底部示例中的示例)是正确的.像在网络上经常看到的那样,单独添加多个引用是 的错误做法:
For setting the Visual Studio managed project .NET references like WindowsBase
, use the cmake target property VS_DOTNET_REFERENCES. Its important to specify multiple references as a list, not individually. So its correct to use either "PresentationCore;WindowsBase;..."
or use a cmake list like in the example at the bottom of this post. It is not correct to add multiple references individually, like often seen on the 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目标属性 VS_DOTNET_REFERENCE_refname 用于每个二进制引用.要求将< refname>
替换为参考文件名,且不带扩展名.为引用指定其他< refname>
不是是正确的:
To add a binary Visual Studio managed project .NET reference, it is recommended to use one cmake target property VS_DOTNET_REFERENCE_refname for every binary reference. It is required that the <refname>
is replaced with the reference file name, without extension. It is not correct to specify a different <refname>
for the reference:
# 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_properties(TARGET $ {PROJECT_NAME} ...)
上使用 set_target_properties($ {PROJECT_NAME} ...)
>.无论您选择哪种方法,最好在您的 CMakeLists.txt
中保持一致.
Last, not least, I prefer to use set_target_properties(${PROJECT_NAME} ...)
over the equally valid set_properties(TARGET ${PROJECT_NAME} ...)
. Whichever you choose, it is nice to be consistent in your CMakeLists.txt
.
这是一个正确的完整示例,可帮助您入门:
Here is a correct, full example to get you started:
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")
这篇关于将DLL参考添加到基于CMake的C#项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!