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 anyone used them? What is their quality? Are they dependable? Do they replicate VS9/MSBuild functionality?
-
Can I ask why? What are you trying to achieve with CMake? – Adam W Jan 20 '10 at 17:05
-
1I know CMake (I used it for C++ projects). I dislike the manual way of building solutions and projects for VS. I'd like to make some scripts to generate them depending on some options, and thought that if CMake already can generate them, maybe it is possible to adapt it. But: we already have a solution consisting of ~40 manually tuned projects and we wouldn't want to lose features we use. – liori Jan 20 '10 at 20:21
3 Answers
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.
-
This answer is no longer accurate (although the linked page is helpful). CMake now supports C++, C, C#, Fortran, Objective C, and CUDA, amongst perhaps a couple others. For a C# example, see this [answer](https://stackoverflow.com/a/55421261/3987854). – Kevin Nov 25 '19 at 04:00
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.

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

- 11,367
- 15
- 60
- 80