Issue
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
Solution
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.
Answered By - Matthieu Pizenberg Answer Checked By - Dawn Plyler (WPSolving Volunteer)