1

I want to create a solution in MSVC using CMake that has two projects (in CMake vocabulary one C# executive and one C++/CLR library).

How can I do this? All examples that I found are about one type of project in a CMake (all C++ or C#).

To clarify:

If I want to create a C++/CLR project using CMake, I can write code like this:

cmake_minimum_required (VERSION 3.5)

project (TestCppClr)

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


ADD_EXECUTABLE(testCppClr "${CMAKE_SOURCE_DIR}/main.cpp")
set_target_properties(testCppClr PROPERTIES COMMON_LANGUAGE_RUNTIME "")

and if I want to create a project targeting C#, I can write something such as this:

cmake_minimum_required (VERSION 3.5)

project(Example VERSION 0.1.0 LANGUAGES CSharp)

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


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)

I cannot find any way to combine these two CMake files, so I can create an executable in C++/CLR and another project in C#.

Is there any way that I can do this?

Kevin
  • 16,549
  • 8
  • 60
  • 74
mans
  • 17,104
  • 45
  • 172
  • 321

1 Answers1

2

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.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • How to define that "testCppClr" is a C++/clr and "Example" is a c# application? in this code, both of them seems similar to each other. – mans Jun 16 '20 at 11:28
  • @mans Well, you can name them something more verbose to tell you and other developers what they are. The C# target name `Example` is pretty vague, so you could change it to `MyCSharpExecutable` or something similar to make it more clear. This would only be for your own sanity; CMake is smart and will *infer* what to build based on the **file extensions** of the source files you provide to the `add_executable` command. So, source files with `.cpp` file extensions will be built into C++ libraries/executables, and `.cs` files will be built into C# libraries/executables. – Kevin Jun 16 '20 at 11:54
  • Thanks. This makes sense. Cmalke understands the file extension and uses the appropriate project type for them. – mans Jun 18 '20 at 08:54