From 3b06ab65ff5b953c56ee8d5a438fed120c8aaa42 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 4 Jan 2024 03:30:00 +0100 Subject: [PATCH] Don't set CMAKE_CXX_STANDARD to 11 by default Use the compiler default C++ dialect if it is C++11 or higher and only explicitly request C++11 if the compiler can't compile a small test using C++11 features by default. This prevents from unnecessarily adding -std=c++11 to the compiler flags under Unix, even with compilers using e.g. C++17 by default. --- CMakeLists.txt | 3 --- build/cmake/init.cmake | 19 +++++++++++++++++++ build/cmake/options.cmake | 2 -- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 89cb1b1f9d..f6ae7f6210 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,9 +97,6 @@ endif() project(wxWidgets VERSION ${wxVERSION} LANGUAGES ${wxLANGUAGES}) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - include(build/cmake/main.cmake) # Set the default startup project for Visual Studio diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake index e229dcdd1c..f5dab11199 100644 --- a/build/cmake/init.cmake +++ b/build/cmake/init.cmake @@ -10,6 +10,25 @@ if(DEFINED wxBUILD_CXX_STANDARD AND NOT wxBUILD_CXX_STANDARD STREQUAL COMPILER_DEFAULT) set(CMAKE_CXX_STANDARD ${wxBUILD_CXX_STANDARD}) + set(CMAKE_CXX_STANDARD_REQUIRED ON) +else() + # If the standard is not set explicitly, check whether we can use C++11 + # without any special options. + include(CheckCXXSourceCompiles) + check_cxx_source_compiles(" + #include + int main() { + std::vector v{1,2,3}; + for (auto& n : v) + --n; + return v[0]; + }" + wxHAVE_CXX11) + if(NOT wxHAVE_CXX11) + # If not, request it explicitly and let CMake check if it's supported. + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + endif() endif() if(MSVC) diff --git a/build/cmake/options.cmake b/build/cmake/options.cmake index 7887f1ef50..bde248f25c 100644 --- a/build/cmake/options.cmake +++ b/build/cmake/options.cmake @@ -46,8 +46,6 @@ endif() # support setting the C++ standard, present it an option to the user if(DEFINED CMAKE_CXX_STANDARD) set(wxCXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD}) -elseif(APPLE) - set(wxCXX_STANDARD_DEFAULT 11) else() set(wxCXX_STANDARD_DEFAULT COMPILER_DEFAULT) endif()