62

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?

7 Answers 7

57

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.

11
  • 2
    Sadly this is only supported on Windows Commented Jul 12, 2018 at 4:42
  • 2
    Generation of Visual Studio projects by CMake is only supported on Windows, probably because Visual Studio itself only has decent support for Windows. (Yes, there is partial support on Mac as well, but not in CMake.) Commented Jul 12, 2018 at 14:44
  • 2
    Right, but Visual Studio projects have been supported by Mono on Linux and Mac for over a decade. Commented Jul 13, 2018 at 5:23
  • 2
    Does this support mixed projects? I need a Visual Studio solution with c++ libraries and c# projects Commented Oct 6, 2018 at 16:14
  • 2
    Depending on how you set up your directory structure, yes. For instance, if you have a directory with C++ files which compile into a library, and another directory with C# files, you would use project(CPPExample VERSION 0.1.0 LANGUAGES CPP) in the C++ folder, build your library target, then utilize that target in the C# project. There may be other more elegant ways to do this as well, but that's how I've done it. Commented Oct 8, 2018 at 15:25
30

CMake 2.8.9 and up add a TYPE parameter to include_external_msproject like so:

include_external_msproject(
    MyProject MyProject.csproj
    TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC)

This lets you specify that the project is C# (the magic GUID above), otherwise things struggle (see docs).

You will probably still want to use the configure_file template approach mentioned elsewhere with your .csproj file to get the right paths into it, unless you're building straight into your source tree.

The good news is that you can wildcard your C# files in the .csproj.template file like so:

<ItemGroup>
  <Compile Include="${DOS_STYLE_SOURCE_DIR}\**\*.cs" />
</ItemGroup>

And you'll need something like this in your CMakeLists.txt to convert CMake's unix-style forwardslash path separators into Windows-style backslashes, otherwise it will compile the files but they won't show up as links in the project in Visual Studio:

FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR)

Then it's just:

CONFIGURE_FILE(MyProject.csproj.template MyProject.csproj)

In your CMakeLists.txt to configure the template file into a real project file with the right wildcard path.

HTH.

5
  • 1
    Could you give a complete example (or link to one) of how to: (1) Copy the source C# folder and files; (2) the .csproj.template and how to use it; and (3) how to update CMakeLists.txt to do all this?
    – Adi Shavit
    Commented Apr 3, 2013 at 15:53
  • 3
    You don't need to copy the C# source folder/files - they are referenced in-place. The csproj.template file is the same as any other csproj file, except for the wildcard noted above. Just copy one. I've added the actual CONFIGURE_FILE command to my answer, which was the only thing missing. Commented Apr 4, 2013 at 18:01
  • Do I need a extra CMakeLists.txt in my C# folder or do all these changes happen in the root CMakeLists.txt? If they happen in the root CMakeLists.txt, do I put them before or after the call to include_external_msproject()?
    – Adi Shavit
    Commented Apr 4, 2013 at 19:24
  • Got it to work with all the changes in the root CMakeLists.txt. However, in order to [re-]build my C# projects, I have to unload and reload them every time the .sln is [re-]generated otherwise they are "Skipped". Any ideas? I asked about it here: stackoverflow.com/q/15859576/135862
    – Adi Shavit
    Commented Apr 7, 2013 at 6:26
  • 1
    FYI, here's a list of the magic GUIDs for different Visual Studio projects: codeproject.com/Reference/720512/…
    – E-rich
    Commented Aug 1, 2017 at 3:02
21

Just in case anyone is still looking for information about this, there is really no reason to generate C# projects with CMake, they are cross platform by design. On Linux, C# projects are generally managed with MonoDevelop which can read .csproj files from visual studio just fine. This should enable cross-platform development of C# projects. The only potential issue would be if you had native c++ projects mixed with c# projects (like a backend written in c++ with a GUI in c#), in this case just have cmake copy over your .csproj files as though they were data and you should be good to go. CMake is intended to set up your build environment to be cross platform. This comes in really handy with c++ where code is built in a very different way on linux than on windows (or other OSs if youre into that), but is kind of unnecessary for c# which can execute cross-platform, and, thanks to design decisions by the mono team, can build cross platform. CMake does provide some great tools to automate things, but much of this functionality can be recovered with a properly configured .csproj file. Anyway I know this question is over a year old but it's one of the top search results I stumbled on when I was looking up how to do this. I've since made this realization.

10
  • 5
    Yes, but while cmake also provides install functionality plain C# projects (csproj) don't. I'd be interested in "make && make install" functionality for C# projects if cmake could provide it.
    – fog
    Commented Jun 7, 2012 at 14:29
  • It's not hard to set up a post build event for this. Assuming you need to integrate into an existing cmake build system, you can even use the cmake configure_file command to fill in the correct install location. Commented Jun 21, 2012 at 18:41
  • 35
    There is a good reason to generate C# projects by CMake. If you have mixed native + C++/CLI + C# project with lot of configuration done by CMake, you need the C# project to take the configuration done in CMake into account and refer to the projects generated for C++ and be included in the right solution.
    – Jan Hudec
    Commented Jun 25, 2012 at 8:31
  • @Jan again use configure_file (cmake.org/cmake/help/v2.8.8/cmake.html#command:configure_file) for this. Most projects I work on are C++, C++/CLI, and C#, and this works great in lieu of actual cmake support for C#. Commented Jun 25, 2012 at 13:59
  • 9
    I would say the best reason for generating C# projects using CMake is to get away from .csproj files. The biggest issue I have with .csproj files is that they are only editable through a queer set of dialogues and the result is an impenetrable XML file. CMake suffers from neither of these problems.
    – Magnus
    Commented Oct 1, 2014 at 6:45
8

To piggy-back on the answer provided by @the_storyteller, CMake v3.8 and greater indeed supports C# as a first-class language. Because a WPF example was already provided, here is a complete CMake example for a simple Windows Forms application. I've provided the optional commands for linking in other libraries built locally in the source tree, and linking 3rd party library dependencies.

Note that Windows Forms applications require the use of the csharp_set_windows_forms_properties CMake command, whereas WPF projects use csharp_set_designer_cs_properties and csharp_set_xaml_cs_properties.

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

project(MyWinFormApp LANGUAGES CSharp)

# Include CMake utilities for CSharp, for WinForm and WPF application support.
include(CSharpUtilities)

# Define the executable, including any .cs files. 
# The .resx and other Properties files are optional here, but including them makes them visible in the VS solution for easy editing. 
add_executable(MyWinFormApp
    App.config
    Form1.cs
    Form1.Designer.cs
    Form1.resx
    Program.cs
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings
)

# Set the .NET Framework version for the executable.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# Set the executable to be 32-bit.
set_property(TARGET MyWinFormApp PROPERTY WIN32_EXECUTABLE TRUE)
# Set the C# language version (defaults to 3.0).
set(CMAKE_CSharp_FLAGS "/langversion:latest")

# Set the source file properties for Windows Forms use.
csharp_set_windows_forms_properties(
    Form1.cs
    Form1.Designer.cs
    Form1.resx
    Program.cs
    Properties/AssemblyInfo.cs
    Properties/Resources.Designer.cs
    Properties/Resources.resx
    Properties/Settings.Designer.cs
    Properties/Settings.settings
)

# If necessary, link in other library dependencies that were built locally in this source tree.
target_link_libraries(MyWinFormApp MyLocalLib)

# If necessary, link in other library/DLL references, such as 3rd party libraries.
set_property(TARGET MyWinFormApp PROPERTY 
    VS_DOTNET_REFERENCE_MyThirdPartyLib /path/to/libs/MyThirdPartyLib.dll)

# Add in the .NET reference libraries.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_REFERENCES
    "Microsoft.CSharp"
    "System"
    "System.Core"
    "System.Data"
    "System.Drawing"
    "System.Windows.Forms"
)
2
  • Fantastic! Would you consider adding that as a second example (the first will have to be renamed) to my CMake C# example project on GitHub? If you do add the WinForms example and submit a pull request, I'll rename it to be more generic, and we can link it here as well. Commented Apr 12, 2019 at 18:28
  • Just created the pull request. Both WinForm and WPF examples work on VS 2017 and VS 2019.
    – Kevin
    Commented Apr 13, 2019 at 3:56
2

I was finally able to generate a valid solution using the second c# module - kde. Although, cmake created a number of .vcproj files while I expected to get .csproj, but I guess that is the only form "Visual Studio 8 2005" generator can offer.

Nevertheless I was able to successfully build this solution and produce executables and dll libraries.

1
  • 1
    Update: the fact that cmake created c++ projects with c# source files in them makes the project almost unusable - Intellisense doesn't work, app.config cannot be accessed through the regular means, etc. I'm discontinuing further investigation in this direction and switching to MsBuild -based project configuration.
    – Leonid
    Commented Jan 20, 2010 at 1:45
2

You can create a project with Visual Studio, than take it apart and make CMake write it using configure_file command (you'll have to generate a bit of XML with the list of sources) and add it to the solution with include_external_msproject (for other generators you'll need to create the custom target to run msbuild on it manually; cmake does not seem to support doing that yet). The project is rather simple, so it should be possible to do that.

1

I wanted to point out that it is now possible to generate C# Projects using the SDK-Style project XML format using CMake 3.23.

Although it is still a release candidate version, it works pretty well with CMake 3.23 rc2. To create C# SDK-style projects use DOTNET_SDK target property.

For example one could use C# SDK-style project with this line

set_target_properties(myTarget PROPERTIES DOTNET_SDK "Microsoft.NET.Sdk")

Also remember that you don't need a project() command when creating C# Projects with CMake. The project() command always creates a new solution which could be useful but not necessary. One can use enable_language(CSharp) to declare a Target being a C# Project.

To test C# SDK-style projects I used Visual Studio 2019 with Visual Studio 16 Generator on Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.