3

I'm trying to generate C# project for Visual Studio 2017 with a CMake 3.16.3, but I'm having problems setting C# language version. Even if I'm specifying it at top of CMakeLists.txt file as such:

target_compile_options(<MyApp> PRIVATE "/langversion:latest")

Or like this:

set(CMAKE_CSharp_FLAGS "/langversion:latest")

Inside the *.csproj file it is always set like this:

<Project>
    ...
    <PropertyGroup>
        ...
        <AdditionalOptions>/langversion:latest</AdditionalOptions>
        ...
        <LangVersion>3</LangVersion>
        ...
    </PropertyGroup>
    ...
</Project>

So no matter what value I put there, it is always version 3 (which is minimum version). Without specifying language version, it set to default and that is latestMajor, Version 7.0.

Only way around this problem so far is to create Directory.Build.props file inside build folder. And it looks like this:

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>
</Project>

I'm quite new with CMake and its documentation is quite complex, so I probably have missed some steps. Or is this a bug in CMake and has anyone had any better solutions for this problem?

Here is link for projects Gitlab page

  • Can you provide a minimal working example? A singe C# file a small CMake file should be enough. But as far as I know, C# is no supported language from CMake. – usr1234567 Apr 07 '20 at 07:57
  • I added example project to gitlab. The link is at bottom of question. – Sipi Raussi Apr 07 '20 at 13:38

3 Answers3

2

This is a limitation of CMake that has been only recently fixed in version 3.17.0 . You need to install CMake version 3.17 and generate the solutions from the command line.

See this issue in the CMake issue tracker.

vre
  • 6,041
  • 1
  • 25
  • 39
  • Thanks for the hint! I updated the CMake from 3.16.3 to 3.17.0, but the same problem still exists. Now the language version is always latestMajor, no matter what I specify in CMAKE_CSharp_FLAGS or in target_compile_options. – Sipi Raussi Apr 07 '20 at 18:07
  • Are you clearing the cache, i.e. deleting CMakeCache.txt from the build directory after applying your changes? `CMAKE_CSharp_FLAGS` is cached and won't be updated otherwise. – vre Apr 07 '20 at 20:11
  • Yes I'm deleting whole build folder that contains CMakeCache.txt file. – Sipi Raussi Apr 08 '20 at 05:46
  • Just tested your project. For the current state in the `csproj` file no `LangVersion` is set, as you are forcing to use `/langversion:latest`. If I change that to `/langversion:5` , delete the build folder and rerun cmake command an entry with `5` is added, so everything seems to work for me as you expect. I am using Visual Studio 2019. My cmake command was `cmake -S -B -G "Visual Studio 16 2019"`. – vre Apr 08 '20 at 06:24
  • Interesting, when you set `/langversion:7` or newer the `` property disappears and set it to `/langversion:"7 or newer"`. I was able to set version 5 and 6, that I tried. – Sipi Raussi Apr 08 '20 at 11:26
  • The Visual Studio 2019 solved my problem. Now by default it determines language version based on framework version. [With all .NET Framework versions language version is 7.3](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version). Thanks for helping me out! – Sipi Raussi Apr 08 '20 at 11:47
1

The problem was with Visual Studio 2017. By default Visual Studio 2019 determines C# language version based on framework version. Here is the Microsoft's documentation.

tl;dr Basically just upgrade to Visual Studio 2019

0

I found answer for this question in this article (default value shall be used):

set(CMAKE_CSharp_FLAGS "/langversion:default")

Or just set the property VS_GLOBAL_LangVersion to 8.0 (from this post):

set_property(TARGET myLib PROPERTY VS_GLOBAL_LangVersion "8.0")

Paweł Iwaneczko
  • 853
  • 1
  • 10
  • 13