8

Version 3.8 of CMake supports generating Visual Studio C# projects. By default, it sets the language version ("LangVersion") to version 3 in the .csproj file. I need to change to a more recent language version, like version 6.

How do I override the language version in my CMakeLists.txt file?

1 Answer 1

9

You can set this as a compile flag on a target:

project(MyProject CSharp)
add_executable(MyExe main.cs)
target_compile_options(MyExe PRIVATE "/langversion:6")

Alternatively, you can set the global variable, to apply it to all future targets:

set(CMAKE_CSharp_FLAGS "/langversion:6")
Sign up to request clarification or add additional context in comments.

3 Comments

I chose the set(CMAKE_CSharp_FLAGS "/langversion:6") method. Thanks!
Your options for langversion are described here. For example, you can specify "/langversion:latest" to tell the compiler to accept syntax for the latest version of C# it can support.
This solution does not work with cmake 3.14 and VS2019. CMake writes to value "lanversion" to the element <AdditionalOptions> </AdditionalOptions> in csproj file. But VS expects it will be in <LangVersion></LangVersion>.

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.