# How to handle different CXX standards in the same project

**URL:** https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510
**Category:** Usage
**Created:** [September 19, 2022, 11:46am UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510 "2022-09-19T11:46:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [September 19, 2022, 11:46am UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/1 "2022-09-19T11:46:23Z")

</div>

Hey guys,  
in our project we want to use the newest available CXX Standard. (We do not want some beta like `std:c++latest` but an official released standard.)  
At the moment this is C++20.

Our project is build with different compilers. As mentioned above, we want to use the newest supported CXX Standard supported by the specific compiler. For some (old) compilers this would be CXX 17, for newer ones this would be CXX 20.  
We are achieving this behaviour by directly setting.  
`set(CMAKE_CXX_STANDARD 20)` directly after `project()`

However: it turns out that a single lib does not compile with C++20.  
I have added  
`target_compile_features(${LIB_NAME} PRIVATE cxx_std_17)`  
to this lib but this does not seam to work

So my question is:  
What is the best way to use the latest CXX Standard the compiler supports per default but also be able to overwrite the CXX standard for single libs if necessary.  
🙂

Thx for your help 🙂

---

<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: [September 19, 2022, 11:56am UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/2 "2022-09-19T11:56:48Z")

</div>

Have you tried:  
`set_property(TARGET ${LIB_NAME} PROPERTY CXX_STANDARD 17)`?

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [September 19, 2022, 2:12pm UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/3 "2022-09-19T14:12:33Z")

</div>

> [@JuliusCaesar](#):
>
> target\_compile\_features(${LIB\_NAME} PRIVATE cxx\_std\_17)

Thx that did work.  
2 Questions regarding your proposal:

1. in this case the overwrite only applies to the single target (and is not exposed to all targets which link to ${LIB\_NAME} right?
2. what is the difference between set\_property and `target_compile_features`. i thought `target_compile_features` is the more modern way to do that?

---

<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: [September 20, 2022, 6:53am UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/4 "2022-09-20T06:53:41Z")

</div>

> [@JuliusCaesar](#):
>
> in this case the overwrite only applies to the single target (and is not exposed to all targets which link to ${LIB\_NAME} right?

Right. This is a target property, and properties are not inherited when you link to a target AFAIK.

> [@JuliusCaesar](#):
>
> what is the difference between set\_property and `target_compile_features`. i thought `target_compile_features` is the more modern way to do that?

I’m not sure. I proposed this method because `CMAKE_CXX_STANDARD` variable is used to initialize `CXX_STANDARD` property, so it’s only natural to overwrite that. It may be that when both thing are set, one of them just wins, or the newest standard will be chosen. Maybe some of the devs know.

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [September 20, 2022, 10:13am UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/5 "2022-09-20T10:13:20Z")

</div>

thank you Jakub for your help and explanations.  
I was just curious 😉 But if it works I’ll not question it 😃

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 20, 2022, 11:11pm UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/6 "2022-09-20T23:11:30Z")

</div>

> [@fenrir](#):
>
> It may be that when both thing are set, one of them just wins, or the newest standard will be chosen. Maybe some of the devs know.

Yes, CMake will use the standard that satisfies all requirements, both from the compile features and the `CXX_STANDARD` target property. In other words, it will pick the highest out all the ones specified.

---

<div class="post-metadata">

### Author: ![JuliusCaesar](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@JuliusCaesar](https://discourse.cmake.org/u/JuliusCaesar)
#### Post date: [September 21, 2022, 3:12pm UTC](https://discourse.cmake.org/t/how-to-handle-different-cxx-standards-in-the-same-project/6510/7 "2022-09-21T15:12:09Z")

</div>

ah thx Craig for your explanations 🙂
