首页 > 解决方案 > Windows 上的 opencv wasm 构建失败

问题描述

我正在尝试在 Windows 10 上构建 OpenCV 4.0.0 的 wasm 版本,但我无法成功。我在这里和关于 opencv 和 emscripten 的 github 问题页面上阅读了很多关于类似问题的帖子,尝试了建议的解决方案,但似乎没有一个对我有用。

我的设置是:
操作系统 - Windows 10 64 位
OpenCV - v4.0.0(稳定) - 我尝试从github克隆并下载预构建的二进制文件,还有 v3.4.5
emscripten - v1.38.22
python - 与 emscripten 捆绑在一起(2.7.13.1_64bit) ,还尝试了 3.6.4
C++ 编译器 - minGW64
cmake - 3.13.0
openCL 标头 - 来自github binaryen - 来自github
的 version_63 - 我还尝试使用 emsdk install 命令 git 安装它 - GitBash windows 版本

可行的事情:
- cmake(在 GUI 和命令行中,正如一些帖子所建议的那样)
- minGW64 g++ 可以为 win64 生成正确的可执行文件
- opencv 与 g++ 一起工作
- emscripten 可以编译 [hello world].cpp

不起作用的事情:
- emscripten 无法从 opencv 库生成 LLVM 位码,但适用于 zlib - 在这里我遵循了emscripten 文档
的说明 - 它也无法编译使用诸如 imread 和 imshow 之类的 opencv 函数的简单 .cpp 文件:

error: undefined symbol: _ZN2cv17destroyAllWindowsEv
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
error: undefined symbol: _ZN2cv3Mat10deallocateEv
error: undefined symbol: _ZN2cv6imreadERKNSt3__212basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEi
error: undefined symbol: _ZN2cv6imshowERKNSt3__212basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEERKNS_11_InputArrayE
error: undefined symbol: _ZN2cv7waitKeyEi
error: undefined symbol: _ZN2cv8fastFreeEPv
error: undefined symbol: _ZN2cv9thresholdERKNS_11_InputArrayERKNS_12_OutputArrayEddi
Error: Aborting compilation due to previous errors

在这里,我尝试了-s ERROR_ON_UNDEFINED_SYMBOLS=0github 问题中建议的标志,它确实将错误变成了警告,但生成的 .js 仍然不起作用,并将这些确切的错误发现为未定义的函数。

当我尝试使用或不使用选项构建 opencv.js 时,--build_wasm我得到:

C:\opencv>python platforms\js\build_js.py build_wasm --build_wasm --emscripten_dir d:\Download\gitEMSDK\emsdk\emscripten\1.38.22
Args: Namespace(build_dir='build_wasm', build_doc=False, build_test=False, build_wasm=True, clean_build_dir=False, config_only=False, disable_wasm=False, emscripten_dir='d:\\Download\\gitEMSDK\\emsdk\\emscripten\\1.38.22', enable_exception=False, opencv_dir='C:\\opencv', skip_config=False)
Check dir C:\opencv\build_wasm (create: True, clean: False)
Check dir C:\opencv (create: False, clean: False)
Check dir d:\Download\gitEMSDK\emsdk\emscripten\1.38.22 (create: False, clean: False)
=====
===== Config OpenCV.js build for wasm
=====
Executing: ['cmake', '-DCMAKE_BUILD_TYPE=Release', "-DCMAKE_TOOLCHAIN_FILE='d:\\Download\\gitEMSDK\\emsdk\\emscripten\\1.38.22\\cmake\\Modules\\Platform\\Emscripten.cmake'", "-DCPU_BASELINE=''", "-DCPU_DISPATCH=''", '-DCV_TRACE=OFF', '-DBUILD_SHARED_LIBS=OFF', '-DWITH_1394=OFF', '-DWITH_ADE=OFF', '-DWITH_VTK=OFF', '-DWITH_EIGEN=OFF', '-DWITH_FFMPEG=OFF', '-DWITH_GSTREAMER=OFF', '-DWITH_GTK=OFF', '-DWITH_GTK_2_X=OFF', '-DWITH_IPP=OFF', '-DWITH_JASPER=OFF', '-DWITH_JPEG=OFF', '-DWITH_WEBP=OFF', '-DWITH_OPENEXR=OFF', '-DWITH_OPENGL=OFF', '-DWITH_OPENVX=OFF', '-DWITH_OPENNI=OFF', '-DWITH_OPENNI2=OFF', '-DWITH_PNG=OFF', '-DWITH_TBB=OFF', '-DWITH_PTHREADS_PF=OFF', '-DWITH_TIFF=OFF', '-DWITH_V4L=OFF', '-DWITH_OPENCL=OFF', '-DWITH_OPENCL_SVM=OFF', '-DWITH_OPENCLAMDFFT=OFF', '-DWITH_OPENCLAMDBLAS=OFF', '-DWITH_GPHOTO2=OFF', '-DWITH_LAPACK=OFF', '-DWITH_ITT=OFF', '-DBUILD_ZLIB=ON', '-DBUILD_opencv_apps=OFF', '-DBUILD_opencv_calib3d=ON', '-DBUILD_opencv_dnn=ON', '-DBUILD_opencv_features2d=ON', '-DBUILD_opencv_flann=ON', '-DBUILD_opencv_gapi=OFF', '-DBUILD_opencv_ml=OFF', '-DBUILD_opencv_photo=ON', '-DBUILD_opencv_imgcodecs=OFF', '-DBUILD_opencv_shape=OFF', '-DBUILD_opencv_videoio=OFF', '-DBUILD_opencv_videostab=OFF', '-DBUILD_opencv_highgui=OFF', '-DBUILD_opencv_superres=OFF', '-DBUILD_opencv_stitching=OFF', '-DBUILD_opencv_java=OFF', '-DBUILD_opencv_js=ON', '-DBUILD_opencv_python2=OFF', '-DBUILD_opencv_python3=OFF', '-DBUILD_EXAMPLES=OFF', '-DBUILD_PACKAGE=OFF', '-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF', '-DBUILD_DOCS=OFF', "-DCMAKE_C_FLAGS='-s WASM=1 '", "-DCMAKE_CXX_FLAGS='-s WASM=1 '", 'C:\\opencv']
Re-run cmake no build system arguments
-- Building for: Visual Studio 15 2017
-- Found PythonInterp: D:/Download/gitEMSDK/emsdk/python/2.7.13.1_64bit/python-2.7.13.amd64/python.exe (found suitable version "2.7.13", minimum required is "2.7")
-- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "2.7")
-- Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV)
-- If you want to enable Python/Numpy support, set the following variables:
--   PYTHON2_INCLUDE_PATH
--   PYTHON2_LIBRARIES (optional on Unix-like systems)
--   PYTHON2_NUMPY_INCLUDE_DIRS
--   PYTHON3_INCLUDE_PATH
--   PYTHON3_LIBRARIES (optional on Unix-like systems)
--   PYTHON3_NUMPY_INCLUDE_DIRS
-- Could NOT find PythonInterp: Found unsuitable version "2.7.13", but required is at least "3.2" (found D:/Download/gitEMSDK/emsdk/python/2.7.13.1_64bit/python-2.7.13.amd64/python.exe)
-- Performing Test HAVE_CXX_FSIGNED_CHAR
-- Performing Test HAVE_CXX_FSIGNED_CHAR - Failed
-- Performing Test HAVE_C_FSIGNED_CHAR
-- Performing Test HAVE_C_FSIGNED_CHAR - Failed
-- Performing Test HAVE_CXX_W
-- Performing Test HAVE_CXX_W - Failed
-- Performing Test HAVE_C_W
-- Performing Test HAVE_C_W - Failed
-- Performing Test HAVE_CXX_WALL
-- Performing Test HAVE_CXX_WALL - Failed
-- Performing Test HAVE_C_WALL
-- Performing Test HAVE_C_WALL - Failed
-- Performing Test HAVE_CXX_WERROR_RETURN_TYPE
-- Performing Test HAVE_CXX_WERROR_RETURN_TYPE - Failed
-- Performing Test HAVE_C_WERROR_RETURN_TYPE
-- Performing Test HAVE_C_WERROR_RETURN_TYPE - Failed
-- Performing Test HAVE_CXX_WERROR_NON_VIRTUAL_DTOR
-- Performing Test HAVE_CXX_WERROR_NON_VIRTUAL_DTOR - Failed
-- Performing Test HAVE_C_WERROR_NON_VIRTUAL_DTOR
-- Performing Test HAVE_C_WERROR_NON_VIRTUAL_DTOR - Failed
-- Performing Test HAVE_CXX_WERROR_ADDRESS
-- Performing Test HAVE_CXX_WERROR_ADDRESS - Failed
-- Performing Test HAVE_C_WERROR_ADDRESS
-- Performing Test HAVE_C_WERROR_ADDRESS - Failed
-- Performing Test HAVE_CXX_WERROR_SEQUENCE_POINT
-- Performing Test HAVE_CXX_WERROR_SEQUENCE_POINT - Failed
-- Performing Test HAVE_C_WERROR_SEQUENCE_POINT
-- Performing Test HAVE_C_WERROR_SEQUENCE_POINT - Failed
-- Performing Test HAVE_CXX_WFORMAT
-- Performing Test HAVE_CXX_WFORMAT - Failed
-- Performing Test HAVE_C_WFORMAT
-- Performing Test HAVE_C_WFORMAT - Failed
-- Performing Test HAVE_CXX_WERROR_FORMAT_SECURITY
-- Performing Test HAVE_CXX_WERROR_FORMAT_SECURITY - Failed
-- Performing Test HAVE_C_WERROR_FORMAT_SECURITY
-- Performing Test HAVE_C_WERROR_FORMAT_SECURITY - Failed
-- Performing Test HAVE_CXX_WMISSING_DECLARATIONS
-- Performing Test HAVE_CXX_WMISSING_DECLARATIONS - Failed
-- Performing Test HAVE_C_WMISSING_DECLARATIONS
-- Performing Test HAVE_C_WMISSING_DECLARATIONS - Failed
-- Performing Test HAVE_CXX_WMISSING_PROTOTYPES
-- Performing Test HAVE_CXX_WMISSING_PROTOTYPES - Failed
-- Performing Test HAVE_C_WMISSING_PROTOTYPES
-- Performing Test HAVE_C_WMISSING_PROTOTYPES - Failed
-- Performing Test HAVE_CXX_WSTRICT_PROTOTYPES
-- Performing Test HAVE_CXX_WSTRICT_PROTOTYPES - Failed
-- Performing Test HAVE_C_WSTRICT_PROTOTYPES
-- Performing Test HAVE_C_WSTRICT_PROTOTYPES - Failed
-- Performing Test HAVE_CXX_WUNDEF
-- Performing Test HAVE_CXX_WUNDEF - Failed
-- Performing Test HAVE_C_WUNDEF
-- Performing Test HAVE_C_WUNDEF - Failed
-- Performing Test HAVE_CXX_WINIT_SELF
-- Performing Test HAVE_CXX_WINIT_SELF - Failed
-- Performing Test HAVE_C_WINIT_SELF
-- Performing Test HAVE_C_WINIT_SELF - Failed
-- Performing Test HAVE_CXX_WPOINTER_ARITH
-- Performing Test HAVE_CXX_WPOINTER_ARITH - Failed
-- Performing Test HAVE_C_WPOINTER_ARITH
-- Performing Test HAVE_C_WPOINTER_ARITH - Failed
-- Performing Test HAVE_CXX_WSHADOW
-- Performing Test HAVE_CXX_WSHADOW - Failed
-- Performing Test HAVE_C_WSHADOW
-- Performing Test HAVE_C_WSHADOW - Failed
-- Performing Test HAVE_CXX_WSIGN_PROMO
-- Performing Test HAVE_CXX_WSIGN_PROMO - Failed
-- Performing Test HAVE_C_WSIGN_PROMO
-- Performing Test HAVE_C_WSIGN_PROMO - Failed
-- Performing Test HAVE_CXX_WUNINITIALIZED
-- Performing Test HAVE_CXX_WUNINITIALIZED - Failed
-- Performing Test HAVE_C_WUNINITIALIZED
-- Performing Test HAVE_C_WUNINITIALIZED - Failed
-- Performing Test HAVE_CXX_WINCONSISTENT_MISSING_OVERRIDE
-- Performing Test HAVE_CXX_WINCONSISTENT_MISSING_OVERRIDE - Failed
-- Performing Test HAVE_C_WINCONSISTENT_MISSING_OVERRIDE
-- Performing Test HAVE_C_WINCONSISTENT_MISSING_OVERRIDE - Failed
-- Performing Test HAVE_CXX_WNO_NARROWING
-- Performing Test HAVE_CXX_WNO_NARROWING - Failed
-- Performing Test HAVE_C_WNO_NARROWING
-- Performing Test HAVE_C_WNO_NARROWING - Failed
-- Performing Test HAVE_CXX_WNO_DELETE_NON_VIRTUAL_DTOR
-- Performing Test HAVE_CXX_WNO_DELETE_NON_VIRTUAL_DTOR - Failed
-- Performing Test HAVE_C_WNO_DELETE_NON_VIRTUAL_DTOR
-- Performing Test HAVE_C_WNO_DELETE_NON_VIRTUAL_DTOR - Failed
-- Performing Test HAVE_CXX_WNO_UNNAMED_TYPE_TEMPLATE_ARGS
-- Performing Test HAVE_CXX_WNO_UNNAMED_TYPE_TEMPLATE_ARGS - Failed
-- Performing Test HAVE_C_WNO_UNNAMED_TYPE_TEMPLATE_ARGS
-- Performing Test HAVE_C_WNO_UNNAMED_TYPE_TEMPLATE_ARGS - Failed
-- Performing Test HAVE_CXX_WNO_COMMENT
-- Performing Test HAVE_CXX_WNO_COMMENT - Failed
-- Performing Test HAVE_C_WNO_COMMENT
-- Performing Test HAVE_C_WNO_COMMENT - Failed
-- Performing Test HAVE_CXX_FDIAGNOSTICS_SHOW_OPTION
-- Performing Test HAVE_CXX_FDIAGNOSTICS_SHOW_OPTION - Failed
-- Performing Test HAVE_C_FDIAGNOSTICS_SHOW_OPTION
-- Performing Test HAVE_C_FDIAGNOSTICS_SHOW_OPTION - Failed
-- Performing Test HAVE_CXX_PTHREAD
-- Performing Test HAVE_CXX_PTHREAD - Failed
-- Performing Test HAVE_C_PTHREAD
-- Performing Test HAVE_C_PTHREAD - Failed
-- Performing Test HAVE_CXX_QUNUSED_ARGUMENTS
-- Performing Test HAVE_CXX_QUNUSED_ARGUMENTS - Failed
-- Performing Test HAVE_C_QUNUSED_ARGUMENTS
-- Performing Test HAVE_C_QUNUSED_ARGUMENTS - Failed
-- Performing Test HAVE_CXX_FFUNCTION_SECTIONS
-- Performing Test HAVE_CXX_FFUNCTION_SECTIONS - Failed
-- Performing Test HAVE_C_FFUNCTION_SECTIONS
-- Performing Test HAVE_C_FFUNCTION_SECTIONS - Failed
-- Performing Test HAVE_CXX_FDATA_SECTIONS
-- Performing Test HAVE_CXX_FDATA_SECTIONS - Failed
-- Performing Test HAVE_C_FDATA_SECTIONS
-- Performing Test HAVE_C_FDATA_SECTIONS - Failed
-- Performing Test HAVE_CPU_BASELINE_FLAGS
-- Performing Test HAVE_CPU_BASELINE_FLAGS - Failed
CMake Error at cmake/OpenCVCompilerOptimizations.cmake:524 (message):
  Compiler doesn't support baseline optimization flags:
Call Stack (most recent call first):
  cmake/OpenCVCompilerOptions.cmake:270 (ocv_compiler_optimization_options)
  CMakeLists.txt:682 (include)


-- Configuring incomplete, errors occurred!
See also "C:/opencv/build_wasm/CMakeFiles/CMakeOutput.log".
See also "C:/opencv/build_wasm/CMakeFiles/CMakeError.log".
Traceback (most recent call last):
  File "platforms\js\build_js.py", line 224, in <module>
    builder.config()
  File "platforms\js\build_js.py", line 164, in config
    execute(cmd)
  File "platforms\js\build_js.py", line 23, in execute
    raise Fail("Child returned: %s" % retcode)
__main__.Fail: Child returned: 1

尽管我关注了这篇文章并使用了带有 opencl、binaryen、cmake、emscripten_dir 的 python2

我还尝试了带有选项直接 cmake 命令,例如:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE='d:/microSoft/emsdk/emscripten/1.38.21/cmake/Modules/Platform/Emscripten.cmake' -DCPU_BASELINE='' -DCPU_DISPATCH='' -DCV_TRACE=OFF -DBUILD_SHARED_LIBS=OFF -DWITH_1394=OFF -DWITH_ADE=OFF -DWITH_VTK=OFF -DWITH_EIGEN=OFF -DWITH_FFMPEG=OFF -DWITH_GSTREAMER=OFF -DWITH_GTK=OFF -DWITH_GTK_2_X=OFF -DWITH_IPP=OFF -DWITH_JASPER=OFF -DWITH_JPEG=OFF -DWITH_WEBP=OFF -DWITH_OPENEXR=OFF -DWITH_OPENGL=OFF -DWITH_OPENVX=OFF -DWITH_OPENNI=OFF -DWITH_OPENNI2=OFF -DWITH_PNG=OFF -DWITH_TBB=OFF -DWITH_PTHREADS_PF=OFF -DWITH_TIFF=OFF -DWITH_V4L=OFF -DWITH_OPENCL=OFF -DWITH_OPENCL_SVM=OFF -DWITH_OPENCLAMDFFT=OFF -DWITH_OPENCLAMDBLAS=OFF -DWITH_GPHOTO2=OFF -DWITH_LAPACK=OFF -DWITH_ITT=OFF -DBUILD_ZLIB=ON -DBUILD_opencv_apps=OFF -DBUILD_opencv_calib3d=ON -DBUILD_opencv_dnn=ON -DBUILD_opencv_features2d=ON -DBUILD_opencv_flann=ON -DBUILD_opencv_gapi=OFF -DBUILD_opencv_ml=OFF -DBUILD_opencv_photo=ON -DBUILD_opencv_imgcodecs=OFF -DBUILD_opencv_shape=OFF -DBUILD_opencv_videoio=OFF -DBUILD_opencv_videostab=OFF -DBUILD_opencv_highgui=OFF -DBUILD_opencv_superres=OFF -DBUILD_opencv_stitching=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_js=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF -DBUILD_EXAMPLES=OFF -DBUILD_PACKAGE=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DPYTHON_EXECUTABLE=d:/microSoft/emsdk/python/2.7.13.1_64bit/python-2.7.13.amd64/python.exe -DPYTHON_INCLUDE_DIRS=d:/microSoft/emsdk/python/2.7.13.1_64bit/python-2.7.13.amd64/include -DPYTHON_LIBRARY=d:/microSoft/emsdk/python/2.7.13.1_64bit/python-2.7.13.amd64/libs/libpython27.a -S . -B build_js

这完成了,但 mingw32-make 失败了

如果您在我的设置或我尝试的命令中发现任何奇怪的地方,请告诉我。欢迎任何建议。非常感谢。

标签: c++windowsopencvwebassembly

解决方案


看起来没有人在 Windows 下构建它。

只需启用适用于 Linux 的 Windows 子系统,从应用商店安装 Ubuntu 1804 LTS。然后安装 emsdk (WebAssembly) 并在其下构建。

显然,OpenCV 4 .js 构建在“opencv.js”文件( https://github.com/opencv/opencv/issues/13356 )中包含 WASM 代码作为变量(“wasmBinaryFile” )。

https://docs.microsoft.com/en-us/windows/wsl/install-win10

https://webassembly.org/getting-started/developers-guide/

https://docs.opencv.org/trunk/d4/da1/tutorial_js_setup.html


推荐阅读