我正在尝试在 Windows 上的现有 C++ CMake 代码库中生成 C# 项目。经过一番研究,我发现只有两个项目为 CMake 构建了自己的 CSharp 编译器: gdcm和 kde .
我都试过了。不幸的是,第一个未能生成 C# 项目。相反,它创建了一个包含 cs 文件的 VS C++ 项目,并且由于为链接器设置了 C++ 标志,构建总是失败并出现错误。在试用了他们提供的示例项目后,我想知道这是否可能是由于“Visual Studio 8 2005”代码生成器的限制?
第二个项目主要针对 Mono,所以我也没有成功。
有没有人在使用其中一个 CMake 模块或其他东西构建 C# 项目方面有过积极的经验?
最佳答案
截至 CMake 3.8.2 , CMake 正式支持 CSharp 项目生成。
要使用 CMake 构建默认的 Visual Studio 2017 生成的 C#/WPF 项目,请创建一个 CMakeList.txt 文件,如下所示。
项目声明
project(Example VERSION 0.1.0 LANGUAGES CSharp)
包含 CMake
CSharpUtilities
如果您打算使用 WPF 或其他设计器属性。include(CSharpUtilities)
添加所有
cs
、xaml
、settings
、properties
add_executable(Example App.config App.xaml App.xaml.cs MainWindow.xaml MainWindow.xaml.cs Properties/AssemblyInfo.cs Properties/Resources.Designer.cs Properties/Resources.resx Properties/Settings.Designer.cs Properties/Settings.settings)
链接设计器文件、
xaml
文件和其他属性文件及其对应的cs
文件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.xaml.cs MainWindow.xaml MainWindow.xaml.cs)
将应用程序
App.xaml
属性文件设置为程序入口点(如果项目是 WPF 项目)set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition")
设置其他
csproj
文件标志set_property(TARGET Example PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1") set_property(TARGET Example PROPERTY WIN32_EXECUTABLE TRUE) // ...
添加库
set_property(TARGET Example PROPERTY VS_DOTNET_REFERENCES "Microsoft.CSharp" "PresentationCore" "PresentationFramework" "System" "System.Core" "System.Data" "System.Data.DataSetExtensions" "System.Net.Http" "System.Xaml" "System.Xml" "System.Xml.Linq" "WindowsBase")
有关工作的 WPF 示例,请参阅 https://github.com/bemehiser/cmake_csharp_example
对于 WinForms例如,见 this answer .
关于c# - 使用 CMake 生成 C# 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2074144/