diff --git a/cmake/enable_cxx11_support.cmake b/cmake/enable_cxx11_support.cmake index 848fac30af30c1b5f14084b25316397e9d864807..b5f87dd77b6e4754034c9561e9dfc8747047b51e 100644 --- a/cmake/enable_cxx11_support.cmake +++ b/cmake/enable_cxx11_support.cmake @@ -1,6 +1,10 @@ ####################################################################################################################### # enable_cxx11_support.cmake # +# NOTE: Do not use this routine! Use enable_latest_css_support.cmake instead, even if you only need C++11, as the +# compiler always uses the strictest ("oldest") specified standard if mulitple flags are given. This will create +# problems when combining multiple projects (libraries...) requiring different C++ standards. +# # Enable C++-11 support by selecting the appropriate compiler flag. The flag will be appended to all of the following # cmake variables: # - CMAKE_CXX_FLAGS diff --git a/cmake/enable_latest_cxx_support.cmake b/cmake/enable_latest_cxx_support.cmake new file mode 100644 index 0000000000000000000000000000000000000000..0b9b6a82c0b83922e93915fb772dbbb7296e5c1f --- /dev/null +++ b/cmake/enable_latest_cxx_support.cmake @@ -0,0 +1,39 @@ +####################################################################################################################### +# enable_latest_cxx_support.cmake +# +# Enable the latest C++ standard supported by the compiler. by selecting the appropriate compiler flag. The flag will +# be appended to all of the following cmake variables: +# - CMAKE_CXX_FLAGS +# - ${PROJECT_NAME}_CXX_FLAGS +# - ${PROJECT_NAME}_CMAKE_CXX_FLAGS +# +####################################################################################################################### + +####################################################################################################################### +# +# IMPORTANT NOTE: +# +# DO NOT MODIFY THIS FILE inside a project. Instead update the project-template repository and pull the change from +# there. Make sure to keep the file generic, since it will be used by other projects, too. +# +# If you have modified this file inside a project despite this warning, make sure to cherry-pick all your changes +# into the project-template repository immediately. +# +####################################################################################################################### + +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17) +CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) +if(COMPILER_SUPPORTS_CXX11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + set(${PROJECT_NAME}_CXX_FLAGS "${${PROJECT_NAME}_CXX_FLAGS} -std=c++11") + set(${PROJECT_NAME}_CMAKE_CXX_FLAGS "${${PROJECT_NAME}_CMAKE_CXX_FLAGS} -std=c++11") +elseif(COMPILER_SUPPORTS_CXX0X) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") + set(${PROJECT_NAME}_CXX_FLAGS "${${PROJECT_NAME}_CXX_FLAGS} -std=c++0x") + set(${PROJECT_NAME}_CMAKE_CXX_FLAGS "${${PROJECT_NAME}_CMAKE_CXX_FLAGS} -std=c++0x") +else() + message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") +endif()