简体   繁体   中英

CMake doesn't set LangVersion for C# project

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM