开发者

CMake and C++/CLI, C#

I'd like to use CMake to compile code consisting of C++, C++/CLI and C# code. I know that there are some unofficial macros to support C# code. Has anyon开发者_如何学运维e used them? What is their quality? Are they dependable? Do they replicate VS9/MSBuild functionality?


I was exploring ways of configuring c# projects using cmake here. Unfortunately, cmake is limited by only being able to create c++ projects. While the solution can be built just fine, you'll be missing a lot from a normal cs project like Intellisense for example.


As of CMake 3.8, CMake now fully supports C# as a language, if you are building with Visual Studio 2010 or greater. You should now be able to create C# assembly or executable targets with relative ease. Here's a complete example for a simple WinForm C# application:

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.

I've managed a few CMake projects that used C++ libraries, C++/CLI libraries, and C# libraries and executables. CMake was able to handle all three cleanly; just make sure you enable all the languages needed (e.g. CSharp and CXX) when calling project(). For instance, here is an example of creating a C++/CLI target and a C# target in the same CMake project.


Here is a sample C# Macros file for CMake - click here. To be honest, I haven't tried it myself, but it looks ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜