# CMake does not set the compiler option -std to gnu17 or c++17 although I set the target\_compile\_features to cxx\_std\_17

**URL:** https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299
**Category:** Usage
**Tags:** comp:gcc
**Created:** [May 12, 2021, 10:44am UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299 "2021-05-12T10:44:30Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 12, 2021, 10:44am UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/1 "2021-05-12T10:44:30Z")

</div>

I updated the versions of GCC and CMake. After the update CMake no longer sets the right command line option for the c++ standard.

Here is how I set it:

```auto
add_library(project_options INTERFACE)
target_compile_features(project_options INTERFACE cxx_std_17)

```

and then I link to the project\_options

```auto
target_link_libraries(app PRIVATE project_options)

```

With GCC 8.3 (on rhel) this sets the std compile flag to `-std=gnu17`

Now it is set to `-std=c++11`

If I set the compiler globally it works as expected:

```auto
set(CMAKE_CXX_STANDARD 17)

```

What am I missing? Or is this a bug in the new Tools?

My System:

- GCC: 11.1.0
- CMake: 3.20.2

This is a cross post from:  
[c++ - CMake does not set the compiler option -std to gnu17 or c++17 although I set the target\_compile\_features to cxx\_std\_17 - Stack Overflow](https://stackoverflow.com/questions/67500771/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c17-although-i-set-the)

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [May 12, 2021, 11:01am UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/2 "2021-05-12T11:01:16Z")

</div>

This has probably something to do with the fact that GCC11 defaults to -std=gnu++17  
[https://gcc.gnu.org/gcc-11/changes.html](https://gcc.gnu.org/gcc-11/changes.html)

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 12, 2021, 11:20am UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/3 "2021-05-12T11:20:19Z")

</div>

Interesting.

But why is then the standard set to c++11, if I use GCC11

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 12, 2021, 12:35pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/4 "2021-05-12T12:35:37Z")

</div>

This sounds like a regression to me. What version of CMake and GCC worked before?

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 12, 2021, 12:39pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/5 "2021-05-12T12:39:42Z")

</div>

It also did not work with the CMake Version 19.x and GCC11  
But it worked with CMake Version 19.x and GCC10.2

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 12, 2021, 1:06pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/6 "2021-05-12T13:06:37Z")

</div>

OK, sounds like an environmental regression then. We are in the process of updating our CI to Fedora 34 which includes GCC 11, so this might show up there.

FYI @brad.king

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 12, 2021, 1:38pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/7 "2021-05-12T13:38:32Z")

</div>

Thank you!  
Could you write here once this is fixed?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [May 13, 2021, 12:42pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/8 "2021-05-13T12:42:45Z")

</div>

This is working as designed. The `cxx_std_17` feature is [documented here](https://cmake.org/cmake/help/v3.20/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#high-level-meta-features-indicating-c-standard-support). It does not mean “pass -std=c++17”. It means “run the compiler in a mode that supports C++17”. If the default compiler mode supports C++17, no flag is added.

gcc 11 defaults to C++17, and so does not need any `-std=` flag to satisfy `cxx_std_17`. On Fedora 34:

```auto
$ g++ --version
g++ (GCC) 11.1.1 20210428 (Red Hat 11.1.1-1)
$ g++ -x c++ /dev/null -E -dM | grep __cplusplus
#define __cplusplus 201703L

```

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 13, 2021, 1:13pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/9 "2021-05-13T13:13:59Z")

</div>

Ok thank you, this makes perfect sense So why is there the c++11 flag after setting it? Maybe inherited from somewhere else? Can I override it?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [May 13, 2021, 1:25pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/10 "2021-05-13T13:25:10Z")

</div>

Please post an example `CMakeLists.txt` file demonstrating how a `-std=c++11` flag is showing up.

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 14, 2021, 6:48am UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/11 "2021-05-14T06:48:42Z")

</div>

Thank you very much for your help!  
The problem was that a fetched and build dependency sets the global standard to `set(CMAKE_CXX_STANDARD 11)`.  
But I think it is quite cumbersome that even though I require a target to be built with c++17 it is not. Is there another option then setting it globally?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [May 14, 2021, 1:35pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/12 "2021-05-14T13:35:31Z")

</div>

I cannot reproduce that either:

```auto
$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(Forum3299 CXX)
set(CMAKE_CXX_STANDARD 11)
add_library(example STATIC example.cxx)
target_compile_features(example PRIVATE cxx_std_17)

$ cmake ..
-- The CXX compiler identification is GNU 11.0.1
...

$ grep CMAKE_CXX_STANDARD_COMPUTED_DEFAULT CMakeFiles/*/CMakeCXXCompiler.cmake
set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "17")

$ make VERBOSE=1 
...
/usr/bin/c++ -std=gnu++17 -o CMakeFiles/example.dir/example.cxx.o -c /.../example.cxx
...

```

---

<div class="post-metadata">

### Author: ![the\_shmoo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/the_shmoo/32/1436_2.png) [@the\_shmoo](https://discourse.cmake.org/u/the_shmoo)
#### Post date: [May 17, 2021, 2:05pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/13 "2021-05-17T14:05:29Z")

</div>

I also wasn’t able to reproduce a minimal not working example.

I will try to figure out was causes the problem. Thank you for helping!

---

<div class="post-metadata">

### Author: ![colinfang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/colinfang/32/2370_2.png) [@colinfang](https://discourse.cmake.org/u/colinfang)
#### Post date: [April 24, 2022, 9:38pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/14 "2022-04-24T21:38:57Z")

</div>

Your gcc was 11.0

It is v11.1 that come with c++17 as default

---

<div class="post-metadata">

### Author: ![colinfang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/colinfang/32/2370_2.png) [@colinfang](https://discourse.cmake.org/u/colinfang)
#### Post date: [April 24, 2022, 9:41pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/15 "2022-04-24T21:41:41Z")

</div>

I came accross this today as well. It’s quite annoying because some of the lint (e.g. clangd) rely on `compiler_commands.json` for the settings. They don’t know gcc version and hence cannot infer `--std`

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [April 26, 2022, 7:05pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/16 "2022-04-26T19:05:09Z")

</div>

> [@colinfang](#):
>
> lint (e.g. clangd) rely on `compiler_commands.json` for the settings. They don’t know gcc version and hence cannot infer `--std`

See my [above reply](https://discourse.cmake.org/t/3299/8). The behavior of `cxx_std_##` not always adding `-std=` is now also documented [here](https://cmake.org/cmake/help/v3.23/manual/cmake-compile-features.7.html#requiring-language-standards). If you want explicit `-std=` options to appear in `compile_commands.json`, you need to set `CMAKE_CXX_STANDARD` to the desired standard level. That setting uses the given standard unless a `cxx_std_##` feature needs a higher one, and therefore should always pass a `-std=` flag.

---

<div class="post-metadata">

### Author: ![LaPoule](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/898d66/32.png) [@LaPoule](https://discourse.cmake.org/u/LaPoule)
#### Post date: [December 7, 2022, 2:58pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/17 "2022-12-07T14:58:39Z")

</div>

Hello,

I am working with CMake 3.22.1, emacs IDE and clangd 12 as static analyzer. I have an interface library with a set of tests, and even if I set CXX\_STANDARD 17 either for my library or for my tests, I don’t get the -std=… option in the compile\_commands.json. I also tried setting CMAKE\_CXX\_STANDARD, but that doesn’t work either. Am I missing something?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 7, 2022, 3:29pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/18 "2022-12-07T15:29:52Z")

</div>

Brad linked to the 3.23 docs. I suspect that 3.23 might be where that behavior was introduced.

---

<div class="post-metadata">

### Author: ![LaPoule](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/898d66/32.png) [@LaPoule](https://discourse.cmake.org/u/LaPoule)
#### Post date: [December 7, 2022, 3:43pm UTC](https://discourse.cmake.org/t/cmake-does-not-set-the-compiler-option-std-to-gnu17-or-c-17-although-i-set-the-target-compile-features-to-cxx-std-17/3299/19 "2022-12-07T15:43:03Z")

</div>

Oops thanks for the remark!  
I managed to force the flag though, by adding the CXX\_STANDARD\_REQUIRED ON property along with the CXX\_STANDARD one.  
Thanks for you help.
