我想在MSVC中使用CMake创建一个有两个项目的解决方案(在CMake词汇中,一个是C#执行,一个是C++CLR库)。
我怎样才能做到这一点?我找到的所有例子都是关于CMake中的一种类型的项目(都是C++或C#)。
澄清一下。
如果我想用CMake创建一个C++CLR项目, 我可以写这样的代码:
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#为目标的项目,我可以写这样的代码。
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#创建另一个项目。
有什么方法可以做到这一点吗?
你当然可以同时拥有两种类型 C++CLI 和C#在同一个CMake项目中--当你调用 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或更高的版本来获取 充分 CMake对C#的支持。另外,你的C#例子似乎缺少了一些CSharp目标的基本CMake命令,从这个例子中可以看出 回应. 您应该可以根据需要将这些命令添加到同一个 CMakeLists.txt
文件来修改C#源文件和C#目标的属性。