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

2
  • 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. Commented Apr 7, 2020 at 7:57
  • I added example project to gitlab. The link is at bottom of question. Commented Apr 7, 2020 at 13:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
Yes I'm deleting whole build folder that contains CMakeCache.txt file.
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 <LangVersion>5</LangVersion> is added, so everything seems to work for me as you expect. I am using Visual Studio 2019. My cmake command was cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019".
Interesting, when you set /langversion:7 or newer the <LangVersion></LangVersion> property disappears and set it to <AdditionalOptions>/langversion:"7 or newer"</AdditionalOptions>. I was able to set version 5 and 6, that I tried.
|
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

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.