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
This commit is contained in:
Kumataro 2025-03-31 17:31:14 +09:00 committed by GitHub
parent 1f468dd586
commit 09c71aed14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 26 deletions

View File

@ -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)
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)
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()
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)

View File

@ -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;
}

View File

@ -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}