From 9c48c69c8a1877242738816482b8cedd3f455c21 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Tue, 16 Dec 2025 16:41:43 +0900 Subject: [PATCH] Merge pull request #28179 from Kumataro:fix28178 js: add C++17 requirement check for Emscripten 4.0.20+ #28179 Close https://github.com/opencv/opencv/issues/28178 ### 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 --- .../js_setup/js_setup/js_setup.markdown | 7 ++++++- modules/js/CMakeLists.txt | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/doc/js_tutorials/js_setup/js_setup/js_setup.markdown b/doc/js_tutorials/js_setup/js_setup/js_setup.markdown index 198cc74a24..3628285c41 100644 --- a/doc/js_tutorials/js_setup/js_setup/js_setup.markdown +++ b/doc/js_tutorials/js_setup/js_setup/js_setup.markdown @@ -84,7 +84,12 @@ Building OpenCV.js from Source @endcode @note - It requires `python` and `cmake` installed in your development environment. + - It requires `python` and `cmake` installed in your development environment. + - To build with Emscripten 4.0.20 or later, append --cmake_option="-DCMAKE_CXX_STANDARD=17" . + Embind requires C++17 or later since Emscripten 4.0.20. + @code{.bash} + emcmake python ./opencv/platforms/js/build_js.py build_js --cmake_option="-DCMAKE_CXX_STANDARD=17" + @endcode -# [Optional] To build the OpenCV.js loader, append `--build_loader`. diff --git a/modules/js/CMakeLists.txt b/modules/js/CMakeLists.txt index ca27e3df60..526cb3d585 100644 --- a/modules/js/CMakeLists.txt +++ b/modules/js/CMakeLists.txt @@ -74,13 +74,23 @@ else() endif() endif() +# Embind requires C++17 or newer from Emscripten 4.0.20. +# See https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md#4020---111825 +if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.20") + if(NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98" + OR CMAKE_CXX_STANDARD STREQUAL "11" OR CMAKE_CXX_STANDARD STREQUAL "14") + + message(FATAL_ERROR "[OpenCV.js Build Error] " + "Emscripten ${EMSCRIPTEN_VERSION} requires C++17 or newer for Embind, " + "but CMAKE_CXX_STANDARD is set to '${CMAKE_CXX_STANDARD}'.\n" + "Please re-run emcmake with --cmake_option=\"-DCMAKE_CXX_STANDARD=17\"\n") + endif() +endif() ocv_add_module(js BINDINGS PRIVATE_REQUIRED opencv_js_bindings_generator) ocv_module_include_directories(${EMSCRIPTEN_INCLUDE_DIR}) -add_definitions("-std=c++11") - set(deps ${OPENCV_MODULE_${the_module}_DEPS}) list(REMOVE_ITEM deps opencv_js_bindings_generator) # don't add dummy module link_libraries(${deps})