使用CMake在同一解决方案中创建C#和C ++/CLR项目(针对Visual Studio的CMake) [英] Creating C# and C++/CLR projects in the same solution using CMake (CMake targeting visual studio)

查看:178
本文介绍了使用CMake在同一解决方案中创建C#和C ++/CLR项目(针对Visual Studio的CMake)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CMake在MSVC中创建一个解决方案,该解决方案具有两个项目(在CMake词汇表中,一个C#执行程序和一个C ++/CLR库).

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).

我该怎么做?我发现的所有示例都是关于CMake中的一种类型的项目(所有C ++或C#).

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

要澄清:

如果要使用CMake创建C ++/CLR项目,可以编写如下代码:

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 "")

如果我想创建一个针对C#的项目,我可以编写如下代码:

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)

我找不到任何方法来组合这两个CMake文件,因此我可以在C ++/CLR中创建一个可执行文件,并在C#中创建另一个项目.

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?

推荐答案

您当然可以同时拥有

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)

请注意,您应该使用CMake 3.8或更高版本来获得对C#的完整 CMake支持.同样,您的C#示例似乎缺少一些CSharp目标的基本CMake命令,如此响应所示.您应该能够根据需要将这些命令添加到同一 CMakeLists.txt 文件中,以修改C#源文件和C#目标的属性.

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在同一解决方案中创建C#和C ++/CLR项目(针对Visual Studio的CMake)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆