我正在使用CMake生成一个C#Wpf项目。我遵循了以下示例https://github.com/bemehiser/cmake_csharp_wpf_example/blob/master/Example/CMakeLists.txt
我的问题是:如何使用CMake将第三方.NET DLL引用添加到此项目?
我添加这个详细的答案是因为目前在高级网页上有很多误导性的例子。下面squareskittles的答案是正确的,但没有包括对潜在陷阱的详细讨论。这就是我的答案。
为了设置Visual Studio托管项目.NET引用(如WindowsBase
(,最好使用cmake目标属性VS_DOTNET_references。在3.22.4之前的cmake版本中,重要的是将多个引用指定为一个列表,而不是单独指定。新的cmake似乎不再有这个限制(感谢jasal指出这一点(,但将多个引用指定为一个列表可能仍然是一个不错的选择。因此,使用"PresentationCore;WindowsBase;..."
或像本文底部的示例中那样使用cmake列表是正确的。然而,单独添加多个引用是不正确的,就像在网络上经常看到的那样:
# this is incorrect, and will only add 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 "mythriftlib" and the
# reference file name "Thrift" of the DLL 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")
例如,您可以为名为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")
事实证明,在不指定以下引用名称的情况下,也可以一次性添加一堆DLL:
add_library(mylib SHARED)
set_property(TARGET mylib PROPERTY VS_DOTNET_REFERENCES "C:\a\b\c\lib1.dll;C:\d\e\f\wow.lib2.dll")
引用名称是根据DLL的文件名生成的,如lib1
和wow.lib2
。