# Override CMAKE\_CXX\_STANDARD at target level.

**URL:** <https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068>\
**Category:** Code\
**Created:** [April 21, 2020, 8:01pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068 "2020-04-21T20:01:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [April 21, 2020, 8:01pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068/1 "2020-04-21T20:01:33Z")

</div>

I have a top level project that sets the standard to 14, set(CMAKE\_CXX\_STANDARD 14).  
I have a target below (that is included via add\_subdirectory) that links to an external library that requires the -std=gnu++1y flag to be set.

I added target\_compile\_options(myTarget PUBLIC -std=gnu++1y) to the target. However, when I build this target, the end of my makefile looks like “…-std=gnu++1y -std=c++14 -o myFile.cpp”. The set CMAKE\_CXX\_STANDARD is overriding the target compile option. **Is there a way for me to work around this?**

Note, I originally just didn’t set the CMAKE\_CXX\_STANDARD at all. Instead, I had an add\_compile\_options(-std=gnu++1y) set in the top level project. However after linking (target\_link\_library) a new library (Qt), this library appears to be setting the -std=c++11 internally, overriding the -std=gnu++1y setting, which leaves me with the desire to set the CMAKE\_CXX\_STANDARD at the top level. Alternatively if I can enforce that -std=gnu++1y will always be set for the project, that may be ideal.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [April 21, 2020, 8:22pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068/2 "2020-04-21T20:22:06Z")

</div>

```cmake
set_property(TARGET tgt PROPERTY CXX_STANDARD 14)

```

Alternatively (not recommended):

```cmake
set(CMAKE_CXX_STANDARD 11)
add_executable(tgt1 tgt1.cxx)
set(CMAKE_CXX_STANDARD 14)
add_executable(tgt2 tgt2.cxx)

```

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [April 21, 2020, 8:36pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068/3 "2020-04-21T20:36:15Z")

</div>

Thanks for the quick response!

On that feedback, I was able to do

```auto
unset(CMAKE_CXX_STANDARD)
add_library(foo foo.cxx)
set(CMAKE_CXX_STANDARD 14)

```

Unfortunately, it seems like I need my project to just use the `-std=gnu++1y` flag, rather than `-std=c++14`. That said is there a way to enforce the `-std=gnu++1y` to always be at the end of the compilation flags. Specifically when I link in and build with Qt (`target_link_libraries(myTarget PRIVATE Qt::Core)`, I don’t want their `std=c++11` flag to come after mine, overriding it. Any ideas?

I did try

```auto
find_package(Qt5Core)
set_property(TARGET Qt5::Core PROPERTY COMPILE_OPTIONS -std=gnu++1y)

```

But there is still something in there that is forcing whatever I link to Qt5::Core to append `-std=c++11` at the end of the build flags.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [April 21, 2020, 8:50pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068/4 "2020-04-21T20:50:47Z")

</div>

If you want the GNU extensions, please try the following:

```cmake
set_property(TARGET foo PROPERTY CXX_STANDARD 14)
set_property(TARGET foo PROPERTY CXX_EXTENSIONS ON)

```

We generally try to avoid manually passing `-std=` flags - CMake prefers to handle this on its own.

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [April 21, 2020, 9:09pm UTC](https://discourse.cmake.org/t/override-cmake-cxx-standard-at-target-level/1068/5 "2020-04-21T21:09:25Z")

</div>

Yes! That fixed my issue! Previously I thought I had tried to compile with `CXX_EXTENSIONS ON`, but apparently not. That seems to be equivalent to `-std=gnu++1y`.

Side note, if I did need my flag (~-std=gnu++1y`) to be at the end, behind the `CXX\_STANDARD` flag, I’m not sure how I would’ve accomplished this. This issue seems to relate to my struggle, [https://gitlab.kitware.com/cmake/cmake/issues/16608](https://gitlab.kitware.com/cmake/cmake/issues/16608). Though my issue is solved 🙂 , thank you!
