使用 CMake 生成 C# 项目

发布于 2024-08-18 00:33:00 字数 524 浏览 4 评论 0原文

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

陪你搞怪i 2024-08-25 00:33:00

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

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

  • 项目申报

     项目(示例版本 0.1.0 语言 CSharp)
    
  • 包含 CMake CSharpUtilities 如果您计划使用 WPF 或其他设计器属性。


    <前><代码>包括(CSharpUtilities)

  • 添加所有csxaml设置属性

     add_executable(示例
          应用程序配置
          应用程序.xaml
          应用程序.xaml.cs
          主窗口.xaml
          MainWindow.xaml.cs
    
          属性/AssemblyInfo.cs
          属性/Resources.Designer.cs
          属性/资源.resx
          属性/Settings.Designer.cs
          属性/设置.settings)
    
  • 链接设计器文件、xaml 文件,以及其他属性文件及其相应的 cs 文件


    <前><代码> csharp_set_designer_cs_properties(
    属性/AssemblyInfo.cs
    属性/Resources.Designer.cs
    属性/资源.resx
    属性/Settings.Designer.cs
    属性/设置.settings)

    csharp_set_xaml_cs_properties(
    应用程序.xaml
    应用程序.xaml.cs
    主窗口.xaml
    MainWindow.xaml.cs)

  • 将应用 App.xaml 属性文件设置为程序入口点(如果项目是 WPF 项目)

     set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE“ApplicationDefinition”)
    
  • 设置其他 csproj 文件标志

     set_property(TARGET 示例属性 VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
      set_property(目标示例属性WIN32_EXECUTABLE TRUE)
      // ...
    
  • 添加库

     set_property(目标示例属性 VS_DOTNET_REFERENCES
          “微软.CSharp”
          “演示核心”
          “演示框架”
          “系统”
          “系统.核心”
          “系统.数据”
          “系统.数据.数据集扩展”
          “系统.Net.Http”
          “系统.Xaml”
          “系统.Xml”
          “系统.Xml.Linq”
          “WindowsBase”)
    

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

对于 < a href="https://en.wikipedia.org/wiki/Windows_Forms" rel="noreferrer">WinForms 示例,请参阅

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.

來不及說愛妳 2024-08-25 00:33:00

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

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

这可以让您指定该项目是 C#(上面的神奇 GUID),否则事情会很困难(< a href="http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command%3ainclude_external_msproject">请参阅文档)。

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

好消息是,您可以在 .csproj.template 文件中使用通配符,如下所示:

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

并且您需要在 CMakeLists.txt 中使用类似的内容来转换CMake 的 unix 风格的正斜杠路径分隔符转换为 Windows 风格的反斜杠,否则它将编译文件,但它们不会在 Visual Studio 中的项目中显示为链接:

FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR)

那么它只是:

CONFIGURE_FILE(MyProject.csproj.template MyProject.csproj)

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

HTH。

CMake 2.8.9 and up add a TYPE parameter to include_external_msproject like so:

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

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

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

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

And you'll need something like this in your CMakeLists.txt to 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:

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.txt to configure the template file into a real project file with the right wildcard path.

HTH.

与君绝 2024-08-25 00:33:00

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

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.

只等公子 2024-08-25 00:33:00

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

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

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

To piggy-back on the answer provided by @the_storyteller, CMake v3.8 and 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.

Note that Windows Forms applications require the use of the csharp_set_windows_forms_properties CMake command, whereas WPF projects use csharp_set_designer_cs_properties and csharp_set_xaml_cs_properties.

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"
)
穿透光 2024-08-25 00:33:00

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

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

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.

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

时光倒影 2024-08-25 00:33:00

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

You can create a project with Visual Studio, than take it apart and make CMake write it using configure_file command (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.

小帐篷 2024-08-25 00:33:00

我想指出的是,现在可以使用 CMake 3.23 使用 SDK 样式项目 XML 格式生成 C# 项目。

尽管它仍然是候选版本,但它与 CMake 3.23 rc2 配合得很好。要创建 C# SDK 样式项目,请使用 DOTNET_SDK 目标财产。

例如,可以使用这一行的 C# SDK 样式项目

set_target_properties(myTarget PROPERTIES DOTNET_SDK "Microsoft.NET.Sdk")

另请记住,使用 CMake 创建 C# 项目时不需要 project() 命令。 project() 命令总是创建一个可能有用但不是必需的新解决方案。可以使用 enable_language(CSharp) 将目标声明为 C# 项目。

为了测试 C# SDK 样式的项目,我在 Windows 上使用了 Visual Studio 2019 和 Visual Studio 16 Generator。

I wanted to point out that it is now possible to generate C# Projects using the SDK-Style project XML format using CMake 3.23.

Although it is still a release candidate version, it works pretty well with CMake 3.23 rc2. To create C# SDK-style projects use DOTNET_SDK target property.

For example one could use C# SDK-style project with this line

set_target_properties(myTarget PROPERTIES DOTNET_SDK "Microsoft.NET.Sdk")

Also remember that you don't need a project() command when creating C# Projects with CMake. The project() command always creates a new solution which could be useful but not necessary. One can use enable_language(CSharp) to declare a Target being a C# Project.

To test C# SDK-style projects I used Visual Studio 2019 with Visual Studio 16 Generator on Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文