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()
andenable_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
Run .Exe Executable File in Azure Function
Generics: Casting and Value Types, Why Is This Illegal
How to Delay Shutdown and Run a Process in Window Service
"Interface Not Implemented" When Returning Derived Type
Get Line Number for Xelement Here
C# Creating an Unknown Generic Type at Runtime
How to Get a Combination of Keys in C#
Executing Query with Parameters
Configureawait Pushes the Continuation to a Pool Thread
Changing Font for Richtextbox Without Losing Formatting
ASP.NET MVC Route to Catch All *.Aspx Requests
Any Way to Turn the "Internet Off" in Windows Using C#
How to Check If System.Net.Webclient.Downloaddata Is Downloading a Binary File
Detect If PDF File Is Correct (Header PDF)
How to Check If Int Is Legal Enum in C#
Should I Worry About "This Async Method Lacks 'Await' Operators and Will Run Synchronously" Warning