使用 CMake 生成 C# 项目 [英] Generate C# project using CMake

查看:25
本文介绍了使用 CMake 生成 C# 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这两个我都试过了.不幸的是,第一个未能生成 C# 项目.相反,它创建了一个 VS C++ 项目,其中包含 cs 文件,并且由于为链接器设置了 C++ 标志,构建总是失败并出现错误.在试用了他们提供的示例项目后,我想知道这是否可能是由于Visual Studio 8 2005"代码生成器的限制?

第二个项目主要针对 Mono,所以我也没有成功.

有没有人在使用这些 CMake 模块之一或其他东西构建 C# 项目方面有积极的经验?

解决方案

As of 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(示例应用配置应用程序.xaml应用程序.xaml.cs主窗口.xaml主窗口.xaml.cs属性/AssemblyInfo.cs属性/Resources.Designer.cs属性/Resources.resx属性/设置.Designer.cs属性/设置.settings)

  • 将设计器文件、xaml 文件和其他属性文件与其对应的 cs 文件链接

    csharp_set_designer_cs_properties(属性/AssemblyInfo.cs属性/Resources.Designer.cs属性/Resources.resx属性/设置.Designer.cs属性/设置.settings)csharp_set_xaml_cs_properties(应用程序.xaml应用程序.xaml.cs主窗口.xaml主窗口.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(目标示例属性 WIN32_EXECUTABLE TRUE)...

  • 添加库

    set_property(TARGET Example PROPERTY VS_DOTNET_REFERENCES微软.CSharp"演示核心"演示框架"系统"系统.核心"系统.数据""System.Data.DataSetExtensions"系统.Net.Http"系统.Xaml"系统.xml"系统.Xml.Linq"WindowsBase")

有关有效的 WPF 示例,请参阅 https://github.com/bemehiser/cmake_csharp_example

有关 WinForms 示例,请参阅 这个答案.

I'm trying to generate a C# project within an existing C++ CMake code base on Windows. After some research, I could find only two projects that built their own CSharp compilers for CMake: gdcm and kde.

I tried both of them. Unfortunately, the first one failed to generate a C# project. Instead it created a VS C++ project with cs files in it, and because of C++ flags set for linker, the build always failed with errors. After experimenting with the sample project they provided, I wonder whether this could be due to a limitation of the "Visual Studio 8 2005" code generator?

The second project was primarily aimed at Mono, so I wasn't successful with it either.

Has anyone had a positive experience with building C# projects using one of those CMake modules or something else?

解决方案

As of CMake 3.8.2, CSharp project generation is officially supported by CMake.

To build the default Visual Studio 2017 generated C#/WPF project using CMake, create a CMakeList.txt file as follows.

  • Project Declaration

    project(Example VERSION 0.1.0 LANGUAGES CSharp)
    

  • Include CMake CSharpUtilities if you are planning on using WPF or other designer properties.

    include(CSharpUtilities)
    

  • Add all 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)
    

  • Link designer files, xaml files, and other properties files with their corresponding cs files

    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)
    

  • Set app App.xaml properties file as program entry point (if project is a WPF project)

    set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition")
    

  • Set other csproj file flags

    set_property(TARGET Example PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
    set_property(TARGET Example PROPERTY WIN32_EXECUTABLE TRUE)
    ...
    

  • Add libraries

    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")
    

For a working WPF example, see https://github.com/bemehiser/cmake_csharp_example

For a WinForms example, see this answer.

这篇关于使用 CMake 生成 C# 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆