使用 CMake 生成 C# 项目
我正在尝试在 Windows 上现有的 C++ CMake 代码库中生成 C# 项目。经过一番研究,我发现只有两个项目为 CMake 构建了自己的 CSharp 编译器: gdcm 和 kde。
我都尝试过。不幸的是,第一个未能生成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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从 CMake 3.8.2 开始,正式支持 CSharp 项目生成通过 CMake。
要使用 CMake 构建默认的 Visual Studio 2017 生成的 C#/WPF 项目,请创建 CMakeList.txt 文件,如下所示。
项目申报
包含 CMake
CSharpUtilities
如果您计划使用 WPF 或其他设计器属性。<前><代码>包括(CSharpUtilities)
添加所有
cs
、xaml
、设置
、属性
链接设计器文件、
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 项目)设置其他
csproj
文件标志添加库
有关工作 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
Include CMake
CSharpUtilities
if you are planning on using WPF or other designer properties.Add all
cs
,xaml
,settings
,properties
Link designer files,
xaml
files, and other properties files with their correspondingcs
filesSet app
App.xaml
properties file as program entry point (if project is a WPF project)Set other
csproj
file flagsAdd libraries
For a working WPF example, see https://github.com/bemehiser/cmake_csharp_example
For a WinForms example, see this answer.
CMake 2.8.9 及更高版本向
include_external_msproject
添加一个TYPE
参数,如下所示:这可以让您指定该项目是 C#(上面的神奇 GUID),否则事情会很困难(< a href="http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command%3ainclude_external_msproject">请参阅文档)。
您可能仍然希望使用其他地方提到的
configure_file
模板方法与您的.csproj
文件来获取正确的路径,除非您直接构建到源代码树中。好消息是,您可以在
.csproj.template
文件中使用通配符,如下所示:并且您需要在
CMakeLists.txt
中使用类似的内容来转换CMake 的 unix 风格的正斜杠路径分隔符转换为 Windows 风格的反斜杠,否则它将编译文件,但它们不会在 Visual Studio 中的项目中显示为链接:那么它只是:
在您的
CMakeLists.txt
使用正确的通配符路径将模板文件配置为真实的项目文件。HTH。
CMake 2.8.9 and up add a
TYPE
parameter toinclude_external_msproject
like so: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: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:Then it's just:
In your
CMakeLists.txt
to configure the template file into a real project file with the right wildcard path.HTH.
以防万一有人仍在寻找相关信息,实际上没有理由使用 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.
为了利用@the_storyteller提供的答案,CMake v3.1 8 及更高版本确实支持 C# 作为一流语言。由于已经提供了 WPF 示例,因此这里是一个简单 Windows 窗体应用程序的完整 CMake 示例。我提供了用于链接源树中本地构建的其他库以及链接第 3 方库依赖项的可选命令。
请注意,Windows 窗体应用程序需要使用
csharp_set_windows_forms_properties
CMake 命令,而 WPF 项目则使用csharp_set_designer_cs_properties
和csharp_set_xaml_cs_properties
。CMakeLists.txt
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 usecsharp_set_designer_cs_properties
andcsharp_set_xaml_cs_properties
.CMakeLists.txt
我终于能够使用第二个 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.
您可以使用 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 withinclude_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.我想指出的是,现在可以使用 CMake 3.23 使用 SDK 样式项目 XML 格式生成 C# 项目。
尽管它仍然是候选版本,但它与 CMake 3.23 rc2 配合得很好。要创建 C# SDK 样式项目,请使用 DOTNET_SDK 目标财产。
例如,可以使用这一行的 C# 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
Also remember that you don't need a
project()
command when creating C# Projects with CMake. Theproject()
command always creates a new solution which could be useful but not necessary. One can useenable_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.