Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV compilation errors #490

Open
Brennanzuz opened this issue Sep 24, 2024 · 8 comments
Open

OpenCV compilation errors #490

Brennanzuz opened this issue Sep 24, 2024 · 8 comments

Comments

@Brennanzuz
Copy link

Hi I am trying to compile the OpenCV source code (with the contrib modules) using WASI-SDK, as I was told by this issue that emscripten could not handle wasmtime (???).

So after following all the instructions of:

git clone --recursive https://github.com/WebAssembly/wasi-sdk.git
cd wasi-sdk/
git submodule update --init
cmake -G Ninja -B build/toolchain -S . -DWASI_SDK_BUILD_TOOLCHAIN=ON -DCMAKE_INSTALL_PREFIX=build/install
cmake --build build/toolchain --target install
cmake -G Ninja -B build/sysroot -S . -DCMAKE_INSTALL_PREFIX=build/install -DCMAKE_TOOLCHAIN_FILE=build/install/share/cmake/wasi-sdk.cmake -DCMAKE_C_COMPILER_WORKS=ON -DCMAKE_CXX_COMPILER_WORKS=ON
cmake --build build/sysroot --target install

to install this SDK, and then going to my ~ folder where my opencv and opencv_contrib repos are at and doing:

cmake -DCMAKE_TOOLCHAIN_FILE=~/wasi-sdk/build/install/share/cmake/wasi-sdk.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv

It gave me the following error:

CMake Error at /home/neonu/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "/home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-99YUeW'
    
    Run Build Command(s): /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_df402/fast
    /usr/bin/gmake  -f CMakeFiles/cmTC_df402.dir/build.make CMakeFiles/cmTC_df402.dir/build
    gmake[1]: Entering directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-99YUeW'
    Building CXX object CMakeFiles/cmTC_df402.dir/testCXXCompiler.cxx.obj
    /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads   -pthread  -fPIE -MD -MT CMakeFiles/cmTC_df402.dir/testCXXCompiler.cxx.obj -MF CMakeFiles/cmTC_df402.dir/testCXXCompiler.cxx.obj.d -o CMakeFiles/cmTC_df402.dir/testCXXCompiler.cxx.obj -c /home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-99YUeW/testCXXCompiler.cxx
    Linking CXX executable cmTC_df402
    /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_df402.dir/link.txt --verbose=1
    /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads  -pthread   -Wl,--import-memory -Wl,--export-memory  CMakeFiles/cmTC_df402.dir/testCXXCompiler.cxx.obj -o cmTC_df402 
    wasm-ld: error: cannot open /home/neonu/wasi-sdk/build/install/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a: No such file or directory
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    gmake[1]: *** [CMakeFiles/cmTC_df402.dir/build.make:100: cmTC_df402] Error 1
    gmake[1]: Leaving directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-99YUeW'
    gmake: *** [Makefile:127: cmTC_df402/fast] Error 2
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:130 (project)


-- Configuring incomplete, errors occurred!

I was hoping that integrating this SDK with a tried and true repository like OpenCV would be exemplary to see how this works.

I am on an Ubuntu 22.04 system. Ask me if you need any other information.

What should I do?

@sbc100
Copy link
Member

sbc100 commented Sep 24, 2024

If you just want to use the SDK then I suggest you down the prebuilt binaries from https://github.com/WebAssembly/wasi-sdk/releases rather than trying to build it yourself from source.

@Brennanzuz
Copy link
Author

Aight, I did just that:

WASI_OS=linux
WASI_ARCH=x86_64
WASI_VERSION=24
WASI_VERSION_FULL=${WASI_VERSION}.0
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz
tar xvf wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz

and:

WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv

Same error:

Details

-- The CXX compiler identification is Clang 18.1.2
-- The C compiler identification is Clang 18.1.2
System is unknown to cmake, create:
Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
Your CMakeCache.txt file was copied to CopyOfCMakeCache.txt. Please post that file on discourse.cmake.org.
-- Detecting CXX compiler ABI info
System is unknown to cmake, create:
Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++
System is unknown to cmake, create:
Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Check for working CXX compiler: /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ - broken
CMake Error at /home/neonu/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "/home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E'
    
    Run Build Command(s): /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_7f876/fast
    /usr/bin/gmake  -f CMakeFiles/cmTC_7f876.dir/build.make CMakeFiles/cmTC_7f876.dir/build
    gmake[1]: Entering directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E'
    Building CXX object CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj
    /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads   -pthread  -fPIE -MD -MT CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -MF CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj.d -o CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -c /home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E/testCXXCompiler.cxx
    Linking CXX executable cmTC_7f876
    /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7f876.dir/link.txt --verbose=1
    /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads  -pthread   -Wl,--import-memory -Wl,--export-memory  CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -o cmTC_7f876 
    wasm-ld: error: cannot open /home/neonu/wasi-sdk/build/install/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a: No such file or directory
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    gmake[1]: *** [CMakeFiles/cmTC_7f876.dir/build.make:100: cmTC_7f876] Error 1
    gmake[1]: Leaving directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E'
    gmake: *** [Makefile:127: cmTC_7f876/fast] Error 2
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:130 (project)

@sbc100
Copy link
Member

sbc100 commented Sep 24, 2024

The clang that cmake is using is still the one you built yourself (/home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++). Perhaps make is caching that? Try removing your build directory, or if you are doing an in-tree build when removing the cmake cache file.

@Brennanzuz
Copy link
Author

Thanks, that fixed it, but I did a cmake --build . (as per the OpenCV docs) and got this error:

In file included from /home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:38:
/home/neonu/wasi-sdk-24.0-x86_64-linux/bin/../share/wasi-sysroot/include/wasm32-wasi/sys/resource.h:2:2: error: WASI lacks process-associated clocks; to enable emulation of the `getrusage` function using the wall clock, which isn't sensitive to whether the program is running or suspended, compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
    2 | #error WASI lacks process-associated clocks; to enable emulation of the `getrusage` function using \
      |  ^
In file included from /home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:39:
/home/neonu/wasi-sdk-24.0-x86_64-linux/bin/../share/wasi-sysroot/include/wasm32-wasi/sys/times.h:2:2: error: WASI lacks process-associated clocks; to enable emulation of the `times` function using the wall clock, which isn't sensitive to whether the program is running or suspended, compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
    2 | #error WASI lacks process-associated clocks; to enable emulation of the `times` function using \
      |  ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:55:19: error: variable has incomplete type 'struct rusage'
   55 |     struct rusage t;
      |                   ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:55:12: note: forward declaration of 'struct rusage'
   55 |     struct rusage t;
      |            ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:58:5: error: call to undeclared function 'getrusage'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   58 |     getrusage(0, &t);
      |     ^
4 errors generated.
gmake[2]: *** [3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/build.make:258: 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/opj_clock.c.obj] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:3678: 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/all] Error 2
gmake: *** [Makefile:166: all] Error 2

I'm not at all familiar with CMake, but where would the suggested -D_WASI_EMULATED_PROCESS_CLOCKS arguments and "link with -lwasi-emulated-process-clocks" go? Or is there another way?

@sbc100
Copy link
Member

sbc100 commented Sep 24, 2024

The -D flag would go in compile flags (CFLAGS) and the -l flag would go in your linker flags (LDFLAGS).

@Brennanzuz
Copy link
Author

Brennanzuz commented Sep 24, 2024

It seems like I can't put any flags when doing --build because:

CMake Error: Unknown argument --build
CMake Error: Run 'cmake --help' for all supported options.

And even if I put the -D flag to recompile the thing with cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D_WASI_EMULATED_PROCESS_CLOCKS ../opencv, it'll say that:

CMake Error: Parse error in command line argument: _WASI_EMULATED_PROCESS_CLOCKS
 Should be: VAR:type=value

CMake Error: Run 'cmake --help' for all supported options.

And do the linker flags go in the same compile command?

@sbc100
Copy link
Member

sbc100 commented Sep 24, 2024

cmake ... -DCMAKE_C_FLAGS=-D_WASI_EMULATED_PROCESS_CLOCKS.

@Brennanzuz
Copy link
Author

Brennanzuz commented Sep 25, 2024

Thanks, so that's solved, but here's another error:

[  5%] Linking C static library ../lib/libzlib.a
[  5%] Built target zlib
[  5%] Processing OpenCL kernels (core)
[  5%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj
In file included from /home/neonu/opencv/modules/core/src/algorithm.cpp:43:
In file included from /home/neonu/opencv/modules/core/src/precomp.hpp:53:
/home/neonu/opencv/modules/core/include/opencv2/core/utility.hpp:730:14: error: no type named 'recursive_mutex' in namespace 'std'
  730 | typedef std::recursive_mutex Mutex;
      |         ~~~~~^
/home/neonu/opencv/modules/core/include/opencv2/core/utility.hpp:731:14: error: no template named 'lock_guard' in namespace 'std'
  731 | typedef std::lock_guard<cv::Mutex> AutoLock;
      |         ~~~~~^
2 errors generated.
gmake[2]: *** [modules/core/CMakeFiles/opencv_core.dir/build.make:103: modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:3808: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
gmake: *** [Makefile:166: all] Error 2

It couldn't have been an OpenCV error could it? It builds just fine otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants