From f474b226b6a8f99ebd99e20e9155dc80da1a41c5 Mon Sep 17 00:00:00 2001 From: Bill Torpey Date: Mon, 11 May 2020 16:47:02 -0400 Subject: [PATCH] allow C/C++ standard to be specified at build time, default to C++11 if supported --- CMakeLists.txt | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 972b5f31..4aafbcc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,15 +68,20 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL Darwin) endif() if (NOT MSVC) - check_cxx_compiler_flag("-std=gnu++11" COMPILER_SUPPORTS_CXX11) - if(COMPILER_SUPPORTS_CXX11) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") + if(NOT CMAKE_CXX_FLAGS MATCHES "-std=") + # use C++11 by default if supported + check_cxx_compiler_flag("-std=gnu++11" COMPILER_SUPPORTS_CXX11) + if(COMPILER_SUPPORTS_CXX11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") + endif() endif() - check_c_compiler_flag("-std=gnu11" COMPILER_SUPPORTS_C11) - if(COMPILER_SUPPORTS_C11) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11") - else() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") + if(NOT CMAKE_C_FLAGS MATCHES "-std=") + check_c_compiler_flag("-std=gnu11" COMPILER_SUPPORTS_C11) + if(COMPILER_SUPPORTS_C11) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") + endif() endif() # clang 6 has a warning that does not make sense on multi-platform code