繁体   English   中英

CMake 和 C++/CLI、C#

[英]CMake and C++/CLI, C#

我想使用 CMake 来编译由 C++、C++/CLI 和 C# 代码组成的代码。 我知道有一些非官方的宏来支持 C# 代码。 有人用过吗? 它们的质量如何? 他们可靠吗? 他们是否复制了 VS9/MSBuild 功能?

我正在探索配置使用cmake C#项目的方式在这里 不幸的是,cmake 受限于只能创建 c++ 项目。 虽然该解决方案可以很好地构建,但您会从像 Intellisense 这样的普通 cs 项目中遗漏很多。

CMake 3.8 开始,如果您使用 Visual Studio 2010 或更高版本进行构建,CMake 现在完全支持 C# 作为一种语言。 您现在应该能够相对轻松地创建 C# 程序集或可执行目标。 下面是一个简单的 WinForm C# 应用程序的完整示例:

cmake_minimum_required(VERSION 3.8)

project(TestApp 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(${PROJECT_NAME}
    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 ${PROJECT_NAME} PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# Set the executable to be 32-bit.
set_property(TARGET ${PROJECT_NAME} 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
)

# Add in the .NET reference libraries.
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_REFERENCES
    "Microsoft.CSharp"
    "System"
    "System.Core"
    "System.Data"
    "System.Deployment"
    "System.Drawing"
    "System.Net.Http"
    "System.Windows.Forms"
    "System.Xml"
    "System.Xml.Linq"
)

对于程序集或 DLL,只需使用add_library而不是add_executable

我管理过一些使用 C++ 库、C++/CLI 库以及 C# 库和可执行文件的 CMake 项目。 CMake 能够干净利落地处理这三个问题; 只需确保在调用project()时启用所有需要的语言(例如CSharpCXX project() 例如,以下是在同一个 CMake 项目中创建 C++/CLI 目标和 C# 目标的示例

这是 CMake 的示例 C# 宏文件 - 单击此处 老实说,我自己还没有尝试过,但看起来还可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM