7

So I'm trying to port with emscripten to WebAssembly (wasm) a program that has the following includes at the beginning:

#include <Eigen/Geometry>
#include <boost/filesystem.hpp>
#include <dvo/core/intrinsic_matrix.h>
#include <dvo/core/surface_pyramid.h>
#include <dvo/dense_tracking.h>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>

To start easily, I tried to compile a minimal hello world OpenCV program:

#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
int main(int argc, char **argv) {
  Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
  std::cout << "M = " << std::endl << " " << M << std::endl;
  return 0;
}

I have the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( hello hello.cpp )
target_link_libraries( hello ${OpenCV_LIBS} )

Where third-party/opencv-4.1.0/build_wasm is the folder in which I built OpenCV to wasm (cf doc) with:

python ./platforms/js/build_js.py build_wasm --build_wasm

Now, when I'm running emconfigure cmake .. on my project, it does not find OpenCV ("Could not find a package configuration ..."). I'm doing something wrong, but I don't know what. Docs on using emscripten with cmake and dependencies are not detailed unfortunately. Or maybe this issue is specific to OpenCV, I've no idea.

EDIT

Complete error:

 ~/t/w/w/build  emconfigure cmake ..                                                  ven. 26 avril 2019 16:01:00 CEST
CMake Error at CMakeLists.txt:5 (find_package):
  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!
See also "/home/matthieu/temp/wasm/wasm-opencv/build/CMakeFiles/CMakeOutput.log".
shared:ERROR: Configure step failed with non-zero return code: 1.  Command line: cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/home/matthieu/programs/emsdk/node/8.9.1_64bit/bin/node" .. -DCMAKE_TOOLCHAIN_FILE=/home/matthieu/programs/emsdk/emscripten/1.38.30/cmake/Modules/Platform/Emscripten.cmake at /home/matthieu/temp/wasm/wasm-opencv/build

Content of CMakeFiles/CMakeOutput.log:

The target system is: Emscripten - 1 - x86
The host system is: Linux - 4.19.36-1-lts - x86_64
2
  • Could you upload the full error log?
    – Bumsik Kim
    Apr 26, 2019 at 14:34
  • Yes, I've added the error to the question Apr 26, 2019 at 14:54

2 Answers 2

3

So I've finally managed to compile the example with cmake. It is quite ugly though, since I manually add the includes and libraries with some GLOB. I wished OpenCV, when building the library to wasm, would provide an OpenCVConfig.cmake file with all the dependencies setting done. It would allow using the classic ${OpenCV_INCLUDE_DIRS} and ${OpenCV_LIBS} when targetting wasm.

cmake_minimum_required( VERSION 3.1 )
set( CMAKE_CXX_STANDARD 11 )
project( HelloCV )

# Does not work
# find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)

# Needed for opencv2/opencv.hpp
include_directories( third-party/opencv-4.1.0/include )

# Needed by opencv.hpp for opencv2/opencv_modules.hpp
include_directories( third-party/opencv-4.1.0/build_wasm )

# Needed by opencv_modules.hpp for every module
file( GLOB opencv_include_modules "third-party/opencv-4.1.0/modules/*/include" )
include_directories( ${opencv_include_modules} )

# Our hello world executable
add_executable( hello hello.cpp )

# Link to opencv.js precompiled libraries
file( GLOB opencv_js "third-party/opencv-4.1.0/build_wasm/lib/*.a" )
target_link_libraries( hello ${opencv_js} )

This works well when compiling to WebAssembly with emscripten:

~/t/e/o/build  emconfigure cmake -DCMAKE_BUILD_TYPE=Release ..
...
~/t/e/o/build  emmake make
...
~/t/e/o/build  node hello.js
M =
 [  0,   0, 255,   0,   0, 255;
   0,   0, 255,   0,   0, 255]

An annoying issue with my current CMakeLists.txt is that it does not compile successfully in native, only wasm. If you have a better cmake configuration than this, that would allow easy change of target (x86 or emscripten) please share your answer.

0

If your project is not too large in terms of number of source files, you might be better off using emscripten without cmake. Here is an example of command that worked for me:

emcc latuile.cpp binpack.cpp compact_frame.cpp compact_rectangles.cpp fit_together.cpp KMeansRexCore.cpp MPD_Arc.cpp MyRect.cpp optimize_rectangle_positions.cpp permutation.cpp stair_steps.cpp swap_rectangles.cpp WidgetContext.cpp FunctionTimer.cpp -o latuile.js -I/usr/include/eigen3 -Wno-c++11-narrowing -s EXPORTED_FUNCTIONS='["_latuile"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s ALLOW_MEMORY_GROWTH=1

Using cmake, I was having the same errors as you, so I took this route and stopped wasting too much time.

I just copied the list of cpp files from inside my cmake. Notice the EXPORTED_FUNCTIONS='["_latuile"]' And the EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' And the -s ALLOW_MEMORY_GROWTH=1 option. I don't know how you would feed all that into cmake ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.