[CMake] C++ standard version fallbacks.

Stephen Kelly steveire at gmail.com
Mon Jun 5 17:50:11 EDT 2017


Roger Leigh wrote:

> Hi folks,
> 
> I'm currently using this logic to use C++14 with a fallback to C++11
> when C++14 is unavailable:
> 
>    if(NOT CMAKE_CXX_STANDARD)
>      set(CMAKE_CXX_STANDARD 14)
>    endif()
>    if(NOT CMAKE_CXX_STANDARD_REQUIRED)
>      set(CMAKE_CXX_STANDARD_REQUIRED 11)
>    endif()
> 
> which seems to work OK.
> 
> However, for some new stuff, I'd like to use C++17 when available, but
> fall back to C++14, C++11 or C++98.  Is it possible to do this?

Probably set CMAKE_CXX_STANDARD 

without CMAKE_CXX_STANDARD_REQUIRED (That variable doesn't really make sense 
to me and I think it is overused when not needed).

> - I'd like it to work on older CMake versions where "17" isn't a valid
> version for the above; is there any way to introspect the supported
> standards?

Not currently. It could be added now (as a global property), but that won't 
help you for existing CMake versions.

> - And I'd like it to fall back intelligently, i.e. if 17 isn't available
> I want it to select the newest standard possible, rather than falling
> back all the way to 98

I think it already does that.

Thanks,

Steve.




More information about the CMake mailing list