Generate C# Project Using Cmake

Creating C# and C++/CLR projects in the same solution using CMake (CMake targeting visual studio)

You can certainly have both C++/CLI and C# in the same CMake project - just enable both languages (C++ and C#) when you call project().

cmake_minimum_required (VERSION 3.5)

# Enable both C++ and C# languages.
project (TestCSharpAndCppClr VERSION 0.1.0 LANGUAGES CXX CSharp)

if(NOT MSVC)
message(FATAL_ERROR "This CMake files only works with MSVC.")
endif(NOT MSVC)

# Create your C++/CLI executable, and modify its properties.
add_executable(testCppClr "${CMAKE_SOURCE_DIR}/main.cpp")
set_target_properties(testCppClr PROPERTIES COMMON_LANGUAGE_RUNTIME "")

# Create your C# executable.
add_executable(Example
App.config
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs

Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings)

Please note, you should be using CMake 3.8 or greater to get full CMake support for C#. Also, your C# example appears to be missing some of the essential CMake commands for CSharp targets, as seen in this response. You should be able to add these commands as necessary to the same CMakeLists.txt file to modify the properties of the C# source files and C# target.

CMake generation fails on VS 2019 C# Project

The error message describes why this is not working:

C# is currently only supported for Microsoft Visual Studio 2010 and later.

This is because CMake's support for C# only works if you select the generator to be Microsoft Visual Studio 2010 or later. Here is the documentation from the CMake site when they released their C# support:

CMake learned to support CSharp (C#) as a first-class language that can be enabled via the project() and enable_language() commands. It is currently supported by the Visual Studio Generators for VS 2010 and above.

This is still the case with the latest versions of CMake available at the time of writing, and it will not work with the Ninja generator. Just change your generator to VS 2019:

{
"configurations": [
{
"name": "x64-Release",
"generator": "Visual Studio 16 2019",
...


Related Topics



Leave a reply



Submit