【问题标题】:CMake and C++/CLI, C#CMake 和 C++/CLI、C#
【发布时间】:2010-01-12 17:18:15
【问题描述】:

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

【问题讨论】:

  • 我能问一下为什么吗?您想通过 CMake 实现什么目标?
  • 我知道 CMake(我将它用于 C++ 项目)。我不喜欢手动为 VS 构建解决方案和项目的方式。我想根据一些选项制作一些脚本来生成它们,并认为如果 CMake 已经可以生成它们,也许可以对其进行调整。但是:我们已经有一个由大约 40 个手动调整项目组成的解决方案,我们不想丢失我们使用的功能。

标签: c# cmake


【解决方案1】:

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

【讨论】:

  • 这个答案不再准确(尽管链接的页面很有帮助)。 CMake 现在支持 C++、C、C#、Fortran、Objective C 和 CUDA,也许还有其他一些。有关 C# 示例,请参阅此answer。
【解决方案2】:

从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() 时启用所有需要的语言(例如CSharp 和CXX)。例如,这里是 example 在同一个 CMake 项目中创建 C++/CLI 目标和 C# 目标。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多