# Use OpenCV in C++ WASM

**URL:** <https://forum.opencv.org/t/use-opencv-in-c-wasm/18630>\
**Category:** C++\
**Tags:** wasm\
**Created:** [August 31, 2024, 10:05am UTC](https://forum.opencv.org/t/use-opencv-in-c-wasm/18630 "2024-08-31T10:05:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GundulaGaukeley](https://avatars.discourse-cdn.com/v4/letter/g/cc9497/32.png) [@GundulaGaukeley](https://forum.opencv.org/u/GundulaGaukeley)\
**Post date:** [August 31, 2024, 10:05am UTC](https://forum.opencv.org/t/use-opencv-in-c-wasm/18630/1 "2024-08-31T10:05:25Z")

</div>

Dear colleagues,

I am trying to port a Linux console application to WASM. It is important to mention that no JS is used. JS is only used to call functions of the WASM file. But before I continue, here is the basic information:

```auto
emscripten version 3.1.51 (c0c2ca1314672a25699846b4663701bcb6f69cca)
openCV version 4.7.0

Compile openCV for WASM:

Change build_js.py:
-------------------

if "EMSCRIPTEN" in os.environ:
   emscripten_dir = os.environ["EMSCRIPTEN"]
else:
   log.warning("EMSCRIPTEN environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher")

->

if "EMSDK" in os.environ:
   emscripten_dir = os.environ["EMSDK"]
else:
   log.warning("EMSDK environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher")

Set Toolchain manual
--------------------
emcmake python3 /home/gundula/Workspace/opencv/platforms/js/build_js.py build_wasm --build_wasm --emscripten_dir="/usr/emsdk/upstream/emscripten" --cmake_option="-DCMAKE_TOOLCHAIN_FILE=/usr/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" --cmake_option="-DCMAKE_CROSSCOMPILING_EMULATOR=/usr/emsdk/node/16.20.0_64bit/bin/node"

This generates:
- libopencv_calib3d.a
- libopencv_core.a
- libopencv_dnn.a
- libopencv_features2d.a
- libopencv_flann.a
- libopencv_imgproc.a
- libopencv_objdetect.a
- libopencv_photo.a
- libopencv_video.a

```

I write a Makefile with a Variable:

`STATLIBS += libopencv_calib3d.a libopencv_core.a libopencv_dnn.a libopencv_features2d.a libopencv_flann.a libopencv_imgproc.a libopencv_objdetect.a libopencv_photo.a libopencv_video.a`

and build it with:

```auto
$(EXECUTABLE) : $(OBJECTS)
	$(CC) $(OBJECTS) $(STATLIBS) $(LIBS) -o $@ -s EXPORTED_FUNCTIONS=...

```

Then I get the following Linker error’s:

```auto
wasm-ld: error: build/convertPictures.o: undefined symbol: cv::imread(...
wasm-ld: error: build/convertPictures.o: undefined symbol: cv::imwrite(...

```

There is no doubt that Make works correctly. The libs are also included, because classes like cv::Mat are included. And yes, we need imread and imwrite for the local file system of emscripten. We have verified that we can read and write image data to this file system.

One thing first. I do NOT want to port to JS. I want to call OpenCV functions from the C++ code. I already had the idea that this might not be possible with the WASM code and therefore tried to compile OpenCV the “normal” way and specified the em++ compiler as native compiler in “cmake config”. Configure successful but this causes massive problems with make, because the include chain is lost. See “[⚙ D131441 [libc++] Diagnose when header search paths are set up incorrectly](https://reviews.llvm.org/D131441)”

What am I looking for. I need to compile openCV so that I can call all openCV functions from the C++ (WASM) Code. If this is not possible with the existing makes, I need a file list for the different modules to create a makefile myself.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 31, 2024, 11:25am UTC](https://forum.opencv.org/t/use-opencv-in-c-wasm/18630/2 "2024-08-31T11:25:34Z")

</div>

explicit js or not, you still need to use the js generators / toolchain,  
and the imagecodecs module is not available there  
(too many 3rd party code & good js alternatives in that world, i guess)

> [@GundulaGaukeley](#):
>
> so that I can call all openCV functions

basically, you’re restricted to the [whitelist here](https://github.com/opencv/opencv/blob/4.x/platforms/js/opencv_js.config.py)

---

<div class="post-metadata">

**Author:** ![GundulaGaukeley](https://avatars.discourse-cdn.com/v4/letter/g/cc9497/32.png) [@GundulaGaukeley](https://forum.opencv.org/u/GundulaGaukeley)\
**Post date:** [September 1, 2024, 11:35am UTC](https://forum.opencv.org/t/use-opencv-in-c-wasm/18630/3 "2024-09-01T11:35:39Z")

</div>

It’s time to change the Computer Vision Library. Tomorrow I check CImg, boost::GIl and OpenImageIO. Thanks
