我添加这个详细的答案是因为目前在高级网页上有相当多的误导性示例。 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")