使用 CMake 生成 C# 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2074144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:16:30  来源:igfitidea点击:

Generate C# project using CMake

c#visual-studiobuildbuild-automationcmake

提问by Leonid

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: gdcmand kde.

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

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?

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

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

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

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

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

回答by Leonid

I was finally able to generate a valid solution using the second c# module - kde. Although, cmake created a number of .vcproj files while I expected to get .csproj, but I guess that is the only form "Visual Studio 8 2005" generator can offer.

我终于能够使用第二个 c# 模块 - kde 生成一个有效的解决方案。虽然,cmake 创建了许多 .vcproj 文件,而我希望得到 .csproj,但我想这是“Visual Studio 8 2005”生成器可以提供的唯一形式。

Nevertheless I was able to successfully build this solution and produce executables and dll libraries.

尽管如此,我还是能够成功构建此解决方案并生成可执行文件和 dll 库。

回答by Max Ehrlich

Just in case anyone is still looking for information about this, there is really no reason to generate C# projects with CMake, they are cross platform by design. On Linux, C# projects are generally managed with MonoDevelop which can read .csproj files from visual studio just fine. This should enable cross-platform development of C# projects. The only potential issue would be if you had native c++ projects mixed with c# projects (like a backend written in c++ with a GUI in c#), in this case just have cmake copy over your .csproj files as though they were data and you should be good to go. CMake is intended to set up your build environment to be cross platform. This comes in really handy with c++ where code is built in a very different way on linux than on windows (or other OSs if youre into that), but is kind of unnecessary for c# which can execute cross-platform, and, thanks to design decisions by the mono team, can build cross platform. CMake does provide some great tools to automate things, but much of this functionality can be recovered with a properly configured .csproj file. Anyway I know this question is over a year old but it's one of the top search results I stumbled on when I was looking up how to do this. I've since made this realization.

以防万一有人仍在寻找有关此的信息,真的没有理由使用 CMake 生成 C# 项目,它们是跨平台的设计。在 Linux 上,C# 项目通常使用 MonoDevelop 进行管理,MonoDevelop 可以很好地从 Visual Studio 读取 .csproj 文件。这应该能够实现 C# 项目的跨平台开发。唯一的潜在问题是,如果您将本机 c++ 项目与 c# 项目混合在一起(例如用 c++ 编写的后端和 c# 中的 GUI),在这种情况下,只需将 .csproj 文件复制到 .csproj 文件上,就好像它们是数据一样,您应该祝你好运。CMake 旨在将您的构建环境设置为跨平台。这在 C++ 中非常方便,其中代码在 linux 上的构建方式与在 Windows(或其他操作系统,如果您喜欢的话)上的构建方式非常不同,但是对于可以跨平台执行的c#来说是不必要的,并且由于mono团队的设计决策,可以构建跨平台。CMake 确实提供了一些很棒的工具来自动化操作,但是可以使用正确配置的 .csproj 文件恢复大部分功能。无论如何,我知道这个问题已经有一年多了,但它是我在查找如何执行此操作时偶然发现的顶级搜索结果之一。从那以后我就意识到了这一点。

回答by Jan Hudec

You can create a project with Visual Studio, than take it apart and make CMake write it using configure_filecommand (you'll have to generate a bit of XML with the list of sources) and add it to the solution with include_external_msproject(for other generators you'll need to create the custom target to run msbuild on it manually; cmake does not seem to support doing that yet). The project is rather simple, so it should be possible to do that.

您可以使用 Visual Studio 创建一个项目,而不是将其拆开并让 CMake 使用configure_file命令编写它(您必须使用源列表生成一些 XML)并将其添加到解决方案中include_external_msproject(对于其他生成器,您需要将需要创建自定义目标以在其上手动运行 msbuild;cmake 似乎还不支持这样做)。该项目相当简单,所以应该可以做到这一点。

回答by Alastair Maw

CMake 2.8.9 and up add a TYPEparameter to include_external_msprojectlike so:

CMake 2.8.9 及更高版本添加一个TYPE参数,include_external_msproject如下所示:

include_external_msproject(
    MyProject MyProject.csproj
    TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC)

This lets you specify that the project is C# (the magic GUID above), otherwise things struggle (see docs).

这让您可以指定项目是 C#(上面的魔法 GUID),否则事情会很困难(请参阅文档)。

You will probably still want to use the configure_filetemplate approach mentioned elsewhere with your .csprojfile to get the right paths into it, unless you're building straight into your source tree.

您可能仍然希望使用configure_file其他地方提到的模板方法.csproj来获取文件的正确路径,除非您直接构建到源代码树中。

The good news is that you can wildcard your C# files in the .csproj.templatefile like so:

好消息是你可以.csproj.template像这样在文件中通配你的 C# 文件:

<ItemGroup>
  <Compile Include="${DOS_STYLE_SOURCE_DIR}\**\*.cs" />
</ItemGroup>

And you'll need something like this in your CMakeLists.txtto convert CMake's unix-style forwardslash path separators into Windows-style backslashes, otherwise it will compile the files but they won't show up as links in the project in Visual Studio:

你需要这样的东西CMakeLists.txt来将 CMake 的 unix 风格的正斜杠路径分隔符转换为 Windows 风格的反斜杠,否则它会编译文件,但它们不会在 Visual Studio 的项目中显示为链接:

FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR)

Then it's just:

那么它只是:

CONFIGURE_FILE(MyProject.csproj.template MyProject.csproj)

In your CMakeLists.txtto configure the template file into a real project file with the right wildcard path.

在您CMakeLists.txt将模板文件配置为具有正确通配符路径的真实项目文件中。

HTH.

哈。

回答by the_storyteller

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

CMake 3.8.2 开始CMake正式支持 CSharp 项目生成。

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

要使用 CMake 构建默认的 Visual Studio 2017 生成的 C#/WPF 项目,请创建一个 CMakeList.txt 文件,如下所示。

  • Project Declaration

    project(Example VERSION 0.1.0 LANGUAGES CSharp)
    
  • Include CMake CSharpUtilitiesif 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, xamlfiles, and other properties files with their corresponding csfiles

    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.xamlproperties file as program entry point (if project is a WPF project)

    set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition")
    
  • Set other csprojfile 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")
    
  • 项目申报

    project(Example VERSION 0.1.0 LANGUAGES CSharp)
    
  • CSharpUtilities如果您计划使用 WPF 或其他设计器属性,请包括 CMake 。

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

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

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

For a WinFormsexample, see this answer.

有关WinForms示例,请参阅此答案

回答by squareskittles

To piggy-back on the answer provided by @the_storyteller, CMake v3.8and greater indeed supports C# as a first-class language. Because a WPF example was already provided, here is a complete CMake example for a simple Windows Forms application. I've provided the optional commands for linking in other libraries built locally in the source tree, and linking 3rd party library dependencies.

借助@the_storyteller 提供的答案,CMake v3.8及更高版本确实支持将 C# 作为一流语言。因为已经提供了 WPF 示例,所以这里是一个简单的 Windows 窗体应用程序的完整 CMake 示例。我提供了用于链接在源树中本地构建的其他库以及链接 3rd 方库依赖项的可选命令。

Note that Windows Forms applications require the use of the csharp_set_windows_forms_propertiesCMake command, whereas WPF projects use csharp_set_designer_cs_propertiesand csharp_set_xaml_cs_properties.

请注意,Windows 窗体应用程序需要使用csharp_set_windows_forms_propertiesCMake 命令,而 WPF 项目使用csharp_set_designer_cs_propertiescsharp_set_xaml_cs_properties.

CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

project(MyWinFormApp LANGUAGES CSharp)

# Include CMake utilities for CSharp, for WinForm and WPF application support.
include(CSharpUtilities)

# Define the executable, including any .cs files. 
# The .resx and other Properties files are optional here, but including them makes them visible in the VS solution for easy editing. 
add_executable(MyWinFormApp
    App.config
    Form1.cs
    Form1.Designer.cs
    Form1.resx
    Program.cs
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings
)

# Set the .NET Framework version for the executable.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# Set the executable to be 32-bit.
set_property(TARGET MyWinFormApp PROPERTY WIN32_EXECUTABLE TRUE)
# Set the C# language version (defaults to 3.0).
set(CMAKE_CSharp_FLAGS "/langversion:latest")

# Set the source file properties for Windows Forms use.
csharp_set_windows_forms_properties(
    Form1.cs
    Form1.Designer.cs
    Form1.resx
    Program.cs
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings
)

# If necessary, link in other library dependencies that were built locally in this source tree.
target_link_libraries(MyWinFormApp MyLocalLib)

# If necessary, link in other library/DLL references, such as 3rd party libraries.
set_property(TARGET MyWinFormApp PROPERTY 
    VS_DOTNET_REFERENCE_MyThirdPartyLib /path/to/libs/MyThirdPartyLib.dll)

# Add in the .NET reference libraries.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_REFERENCES
    "Microsoft.CSharp"
    "System"
    "System.Core"
    "System.Data"
    "System.Drawing"
    "System.Windows.Forms"
)