简体   繁体   English

CMake 和 C++/CLI、C#

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

I'd like to use CMake to compile code consisting of C++, C++/CLI and C# code.我想使用 CMake 来编译由 C++、C++/CLI 和 C# 代码组成的代码。 I know that there are some unofficial macros to support C# code.我知道有一些非官方的宏来支持 C# 代码。 Has anyone used them?有人用过吗? What is their quality?它们的质量如何? Are they dependable?他们可靠吗? Do they replicate VS9/MSBuild functionality?他们是否复制了 VS9/MSBuild 功能?

I was exploring ways of configuring c# projects using cmake here .我正在探索配置使用cmake C#项目的方式在这里 Unfortunately, cmake is limited by only being able to create c++ projects.不幸的是,cmake 受限于只能创建 c++ 项目。 While the solution can be built just fine, you'll be missing a lot from a normal cs project like Intellisense for example.虽然该解决方案可以很好地构建,但您会从像 Intellisense 这样的普通 cs 项目中遗漏很多。

As of CMake 3.8 , CMake now fully supports C# as a language, if you are building with Visual Studio 2010 or greater.CMake 3.8 开始,如果您使用 Visual Studio 2010 或更高版本进行构建,CMake 现在完全支持 C# 作为一种语言。 You should now be able to create C# assembly or executable targets with relative ease.您现在应该能够相对轻松地创建 C# 程序集或可执行目标。 Here's a complete example for a simple WinForm C# application:下面是一个简单的 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"
)

For assemblies or DLLs, just use add_library instead of add_executable .对于程序集或 DLL,只需使用add_library而不是add_executable

I've managed a few CMake projects that used C++ libraries, C++/CLI libraries, and C# libraries and executables.我管理过一些使用 C++ 库、C++/CLI 库以及 C# 库和可执行文件的 CMake 项目。 CMake was able to handle all three cleanly; CMake 能够干净利落地处理这三个问题; just make sure you enable all the languages needed (eg CSharp and CXX ) when calling project() .只需确保在调用project()时启用所有需要的语言(例如CSharpCXX project() For instance, here is an example of creating a C++/CLI target and a C# target in the same CMake project.例如,以下是在同一个 CMake 项目中创建 C++/CLI 目标和 C# 目标的示例

Here is a sample C# Macros file for CMake - click here .这是 CMake 的示例 C# 宏文件 - 单击此处 To be honest, I haven't tried it myself, but it looks ok.老实说,我自己还没有尝试过,但看起来还可以。

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

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