使用CMake生成C#项目 我试图在Windows上的现有C++ C++代码基础上生成一个C项目。经过一些研究,我发现只有两个项目为CMake构建了自己的CSharp编译器: 和
我两个都试过了。不幸的是,第一个未能生成C#项目。相反,它创建了一个带有CS文件的VS C++项目,并且由于链接器设置了C++标志,生成总是错误地失败。在试用了他们提供的示例项目之后,我想知道这是否是由于“VisualStudio82005”代码生成器的限制造成的 第二个项目主要针对Mono,所以我也没有成功使用CMake生成C#项目 我试图在Windows上的现有C++ C++代码基础上生成一个C项目。经过一些研究,我发现只有两个项目为CMake构建了自己的CSharp编译器: 和,c#,visual-studio,build,build-automation,cmake,C#,Visual Studio,Build,Build Automation,Cmake,我两个都试过了。不幸的是,第一个未能生成C#项目。相反,它创建了一个带有CS文件的VS C++项目,并且由于链接器设置了C++标志,生成总是错误地失败。在试用了他们提供的示例项目之后,我想知道这是否是由于“VisualStudio82005”代码生成器的限制造成的 第二个项目主要针对Mono,所以我也没有成功 有没有人有过使用CMake模块或其他东西构建C#项目的积极经验 我终于能够使用第二个c#模块kde生成一个有效的解决方案。 虽然cmake在我希望获得.csproj时创建了许多.vcpro
有没有人有过使用CMake模块或其他东西构建C#项目的积极经验 我终于能够使用第二个c#模块kde生成一个有效的解决方案。 虽然cmake在我希望获得.csproj时创建了许多.vcproj文件,但我想这是“VisualStudio8 2005”生成器所能提供的唯一形式
尽管如此,我还是成功地构建了这个解决方案,并生成了可执行文件和dll库。如果有人仍在寻找这方面的信息,那么就没有理由使用CMake生成C#项目,它们是跨平台设计的。在Linux上,C#项目通常由MonoDevelop管理,它可以从VisualStudio中读取.csproj文件。这应该能够实现C#项目的跨平台开发。 唯一的潜在问题是,如果你有与C++项目混合的本地C++项目(比如用C++编写的后端,用C语言中的GUI),在这种情况下,只需在C.PROPRJ文件上复制cFIX拷贝,就好像它们是数据,你应该很好地去。 CMake旨在将构建环境设置为跨平台。这在C++上非常有用,其中代码在Linux上与Windows(或者其他OSS,如果你想这样做)的方式不同,但是对于C平台来说不需要执行跨平台,而且,由于MNO团队的设计决定,可以构建跨平台。CMake确实提供了一些很好的工具来实现自动化,但是通过正确配置.csproj文件可以恢复大部分功能。
无论如何,我知道这个问题已经有一年多的历史了,但它是我在查找如何解决这个问题时偶然发现的顶级搜索结果之一。我已经实现了这一点。您可以使用Visual Studio创建一个项目,然后使用
configure\u file
命令将其拆开并让CMake编写(您必须生成一点带有源代码列表的XML),然后使用include\u外部\msproject将其添加到解决方案中(对于其他生成器,您需要创建自定义目标以在其上手动运行msbuild;cmake似乎还不支持这样做)。该项目相当简单,因此应该可以这样做。cmake 2.8.9及以上添加一个TYPE
参数到include\u external\msproject
,如下所示:
include_external_msproject(
MyProject MyProject.csproj
TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC)
这使您可以指定项目为C#(上面的魔法GUID),否则会出现问题()
您可能仍然希望在.csproj
文件中使用其他地方提到的配置_文件
模板方法来获得正确的路径,除非您直接构建到源代码树中
好消息是,您可以在.csproj.template
文件中对C#文件进行通配符,如下所示:
<ItemGroup>
<Compile Include="${DOS_STYLE_SOURCE_DIR}\**\*.cs" />
</ItemGroup>
那就是:
CONFIGURE_FILE(MyProject.csproj.template MyProject.csproj)
在CMakeLists.txt
中,将模板文件配置为具有正确通配符路径的实际项目文件
HTH.截至,CSharp项目生成由CMake正式支持
要使用CMake构建默认的Visual Studio 2017生成的C#/WPF项目,请创建一个CMakeList.txt文件,如下所示
- 项目声明
project(Example VERSION 0.1.0 LANGUAGES CSharp)
- 如果您计划使用WPF或其他设计器属性,请包括CMake
include(CSharpUtilities)
- 添加所有
cs
,xaml
,设置
,属性
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
app.xaml
properties文件设置为程序入口点(如果项目是WPF项目)
- 设置其他
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示例,请参见
例如,请参阅。以借鉴@the_storyteller提供的答案,并且更高版本确实支持C#作为一种一流语言。因为已经提供了一个WPF示例,这里是一个简单的Windows窗体应用程序的完整CMake示例。我提供了可选命令,用于在本地构建的其他库中链接源代码树,并链接第三方库依赖项
请注意,Windows窗体应用程序需要使用csharp\u set\u Windows\u Forms\u properties
CMake命令,而WPF项目使用csharp\u set\u designer\u cs\u properties
和csharp\u set\u xaml\u cs\u 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"
)
更新:CMASE用C++源文件创建C++项目,使项目几乎无法使用——智能感知不起作用,App.CONFIG不能通过常规方式访问。等等,我不再朝这个方向进一步研究,转向基于MSBudio的项目配置。是的,但CMADE还提供s安装功能普通C#projects(csproj)没有。我对“make&&make install”感兴趣如果cmake可以提供C#项目的功能。为其设置生成后事件并不困难。假设您需要集成到现有的cmake生成系统中,您甚至可以使用cmake configure#u file命令填充正确的安装位置。有充分的理由由cmake生成C#项目。如果您有
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")
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"
)