c# - 使用 CMake 生成 C# 项目

标签 c# visual-studio build build-automation cmake

我正在尝试在 Windows 上的现有 C++ CMake 代码库中生成 C# 项目。经过一番研究,我发现只有两个项目为 CMake 构建了自己的 CSharp 编译器: gdcmkde .

我都试过了。不幸的是,第一个未能生成 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)
    
  • 添加所有csxamlsettingsproperties

      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/

相关文章:

c# - ajax工具包:TabContainer confirm when adding tab

visual-studio - 有没有办法突出显示当前列 Visual Studio 2012 代码编辑器?

c - 在各种机器上优化 C 库构建/编译任务的最佳方法

c# - IsSingleByte Encoding的GetByteCount为什么要进行计算

c# - 在客户端控制台应用程序中将 SQL Server 连接字符串保存在何处?

c# - 如何配置 asp.net webforms WebMethod 日期格式

visual-studio-2010 - Visual Studio 属性表 : Why is Character Set missing?

c# - Visual Studio 2013 调试崩溃

build - 带有 set_target_properties 的 FindPkgConfig 无法用于设置 CFLAGS/LDFLAGS

java - 添加 Maven 依赖项时会发生什么?