From 09c71aed141210bf2b14582974ed9d231c24edd5 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Mon, 31 Mar 2025 17:31:14 +0900 Subject: [PATCH] Merge pull request #27107 from Kumataro:fix27105 build: Check supported C++ standard features and user setting #27107 Close #27105 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- cmake/OpenCVDetectCXXCompiler.cmake | 31 +++++++++++-------- cmake/checks/cxx11.cpp | 13 -------- .../config_reference.markdown | 15 +++++++++ 3 files changed, 33 insertions(+), 26 deletions(-) delete mode 100644 cmake/checks/cxx11.cpp diff --git a/cmake/OpenCVDetectCXXCompiler.cmake b/cmake/OpenCVDetectCXXCompiler.cmake index 54cec78ba3..a8a6966108 100644 --- a/cmake/OpenCVDetectCXXCompiler.cmake +++ b/cmake/OpenCVDetectCXXCompiler.cmake @@ -207,30 +207,35 @@ if(CMAKE_VERSION VERSION_LESS "3.1") endforeach() endif() +# See https://github.com/opencv/opencv/issues/27105 +# - CMAKE_COMPILE_FEATURES is used to detect what features are available by the compiler. +# - CMAKE_CXX_STANDARD is used to detect what features are available in this configuration. if(NOT OPENCV_SKIP_CMAKE_CXX_STANDARD) + if(DEFINED CMAKE_CXX_STANDARD AND ((CMAKE_CXX_STANDARD EQUAL 98) OR (CMAKE_CXX_STANDARD LESS 11))) + message(FATAL_ERROR "OpenCV 4.x requires C++11, but your configuration does not enable(CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}).") + endif() + ocv_update(CMAKE_CXX_STANDARD 11) ocv_update(CMAKE_CXX_STANDARD_REQUIRED TRUE) ocv_update(CMAKE_CXX_EXTENSIONS OFF) # use -std=c++11 instead of -std=gnu++11 - if(CMAKE_CXX11_COMPILE_FEATURES) +endif() + +# Meta-feature "cxx_std_XX" in CMAKE_CXX_COMPILE_FEATURES are supported in CMake 3.8+. +# - See https://cmake.org/cmake/help/latest/release/3.8.html +# For CMake 3.7-, use CMAKE_CXXxx_COMPILE_FEATURES instead of it. +if(CMAKE_CXX11_COMPILE_FEATURES OR ("cxx_std_11" IN_LIST CMAKE_CXX_COMPILE_FEATURES)) + if((NOT DEFINED CMAKE_CXX_STANDARD) OR (CMAKE_CXX_STANDARD GREATER_EQUAL 11)) set(HAVE_CXX11 ON) endif() - if(CMAKE_CXX17_COMPILE_FEATURES) - set(HAVE_CXX17 ON) - endif() endif() -if(NOT HAVE_CXX11) - ocv_check_compiler_flag(CXX "" HAVE_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp") - if(NOT HAVE_CXX11) - ocv_check_compiler_flag(CXX "-std=c++11" HAVE_STD_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp") - if(HAVE_STD_CXX11) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - set(HAVE_CXX11 ON) - endif() +if(CMAKE_CXX17_COMPILE_FEATURES OR ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)) + if((NOT DEFINED CMAKE_CXX_STANDARD) OR (CMAKE_CXX_STANDARD GREATER_EQUAL 17)) + set(HAVE_CXX17 ON) endif() endif() if(NOT HAVE_CXX11) - message(FATAL_ERROR "OpenCV 4.x requires C++11") + message(FATAL_ERROR "OpenCV 4.x requires C++11, but your compiler does not support it") endif() set(__OPENCV_ENABLE_ATOMIC_LONG_LONG OFF) diff --git a/cmake/checks/cxx11.cpp b/cmake/checks/cxx11.cpp deleted file mode 100644 index ea719d6248..0000000000 --- a/cmake/checks/cxx11.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600) -// OK -#else -#error "C++11 is not supported" -#endif - -static int test() { return 0; } - -int main() -{ - auto res = test(); - return res; -} diff --git a/doc/tutorials/introduction/config_reference/config_reference.markdown b/doc/tutorials/introduction/config_reference/config_reference.markdown index 91e1067ceb..67a2c60863 100644 --- a/doc/tutorials/introduction/config_reference/config_reference.markdown +++ b/doc/tutorials/introduction/config_reference/config_reference.markdown @@ -64,6 +64,21 @@ Only 0- and 1-level deep module locations are supported, following command will cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib ../opencv ``` +## Build with C++ Standard setting {#tutorial_config_reference_general_cxx_standard} + +`CMAKE_CXX_STANDARD` option can be used to set C++ standard settings for OpenCV building. + +```.sh +cmake -DCMAKE_CXX_STANDARD=17 ../opencv +cmake --build . +``` + +- C++11 is default/required/recommended for OpenCV 4.x. C++17 is default/required/recomended for OpenCV 5.x. +- If your compiler does not support required C++ Standard features, OpenCV configuration should be fail. +- If you set older C++ Standard than required, OpenCV configuration should be fail. + For workaround, `OPENCV_SKIP_CMAKE_CXX_STANDARD` option can be used to skip `CMAKE_CXX_STANDARD` version check. +- If you set newer C++ Standard than recomended, numerous warnings may appear or OpenCV build may fail. + ## Debug build {#tutorial_config_reference_general_debug}