Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate C# project using CMake

I'm trying to generate a C# project within an existing C++ CMake code base on Windows. After some research, I could find only two projects that built their own CSharp compilers for CMake: gdcm and kde.

I tried both of them. Unfortunately, the first one failed to generate a C# project. Instead it created a VS C++ project with cs files in it, and because of C++ flags set for linker, the build always failed with errors. After experimenting with the sample project they provided, I wonder whether this could be due to a limitation of the "Visual Studio 8 2005" code generator?

The second project was primarily aimed at Mono, so I wasn't successful with it either.

Has anyone had a positive experience with building C# projects using one of those CMake modules or something else?

like image 525
Leonid Avatar asked Jan 15 '10 19:01

Leonid


People also ask

What is C Code generation?

20-sim has a C-Code Generator which automatically converts a complete 20-sim model or submodel into C-Code. The application can be used to generate Matlab™/Simulink™ S-Functions, to generate Stand-Alone executables or to generate input/output functions for use in other C and C++ programs.

Can you write C in MATLAB?

In MATLAB®, you can extend your C and C++ code with a MEX function and call it like any MATLAB built-in function. That means you can use existing C and C++ code without rewriting your algorithms in MATLAB. MEX functions enable C and C++ code to create and modify MATLAB arrays in the MATLAB workspace.

Can Simulink generate C Code?

Simulink® Coder™ generates standalone C and C++ code from Simulink models for deployment in a wide variety of applications. For a list of DSP System Toolbox™ features supported by Simulink Coder, see Blocks Supported for C Code Generation.


1 Answers

As of CMake 3.8.2, CSharp project generation is officially supported by CMake.

To build the default Visual Studio 2017 generated C#/WPF project using CMake, create a CMakeList.txt file as follows.

  • Project Declaration

      project(Example VERSION 0.1.0 LANGUAGES CSharp) 
  • Include CMake CSharpUtilities if you are planning on using WPF or other designer properties.

      include(CSharpUtilities) 
  • Add all cs, xaml, settings, properties

      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) 
  • Link designer files, xaml files, and other properties files with their corresponding cs files

      csharp_set_designer_cs_properties(       Properties/AssemblyInfo.cs       Properties/Resources.Designer.cs       Properties/Resources.resx       Properties/Settings.Designer.cs       Properties/Settings.settings)    csharp_set_xaml_cs_properties(       App.xaml       App.xaml.cs       MainWindow.xaml       MainWindow.xaml.cs) 
  • Set app App.xaml properties file as program entry point (if project is a WPF project)

      set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition") 
  • Set other csproj file flags

      set_property(TARGET Example PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")   set_property(TARGET Example PROPERTY WIN32_EXECUTABLE TRUE)   // ... 
  • Add libraries

      set_property(TARGET Example PROPERTY VS_DOTNET_REFERENCES       "Microsoft.CSharp"       "PresentationCore"       "PresentationFramework"       "System"       "System.Core"       "System.Data"       "System.Data.DataSetExtensions"       "System.Net.Http"       "System.Xaml"       "System.Xml"       "System.Xml.Linq"       "WindowsBase") 

For a working WPF example, see https://github.com/bemehiser/cmake_csharp_example

For a WinForms example, see this answer.

like image 88
the_storyteller Avatar answered Sep 21 '22 08:09

the_storyteller