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

微笑、不失礼 提交于 2021-01-28 14:00:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/62386383/creating-c-sharp-and-c-clr-projects-in-the-same-solution-using-cmake-cmake-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!