Skip to content

Instantly share code, notes, and snippets.

@scivision
Created March 16, 2023 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/f748011fa76f0d2d861c116862dbb3f7 to your computer and use it in GitHub Desktop.
Save scivision/f748011fa76f0d2d861c116862dbb3f7 to your computer and use it in GitHub Desktop.
Check highest language standard supported by compiler
# NOTE: for check_*() functions, CMAKE_CXX_STANDARD and CMAKE_C_STANDARD are automatically applied
# if the standard requested is higher than the compiler default.
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
function(check_std_flag lang std_flag)
set(CMAKE_REQUIRED_FLAGS)
if(lang STREQUAL "CXX")
set(stds 20 17 11)
set(stub "c++")
elseif(lang STREQUAL "C")
set(stds 17 11)
set(stub "c")
else()
message(FATAL_ERROR "check_std_flag: unknown language ${lang}")
endif()
foreach(s IN LISTS stds)
if(MSVC)
set(flag /std:${stub}${s})
else()
set(flag -std=${stub}${s})
endif()
if(lang STREQUAL "CXX")
check_cxx_compiler_flag(${flag} HAVE_${lang}${s})
elseif(lang STREQUAL "C")
check_c_compiler_flag(${flag} HAVE_${lang}${s})
endif()
if(HAVE_${lang}${s})
set(${std_flag} ${flag} PARENT_SCOPE)
return()
endif()
endforeach()
message(STATUS "check_std_flag: no suitable ${lang} standard flag found for ${CMAKE_${lang}_COMPILER_ID} ${CMAKE_${lang}_COMPILER_VERSION")
endfunction(check_std_flag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment