Can't create WebGL build with my c++ code

Hi

I am trying to create a Unity WebGL build with my c++ code. I have tried the following 2 methods to create Unity WebGL build.

  1. Create .bc file externally using emcc(emscripten) tool.

Even though I didn’t got any errors to compile my cpp files into one .bc file and even placing the .bc file in webgl folder in unity project Plugins folder. But when I run the build I get the following error.

Invoking error handler due to
Uncaught abort(“To use dlopen, you need to use Emscripten’s linking support, see Linking · emscripten-core/emscripten Wiki · GitHub”) at Error
at jsStackTrace (test-webgl.wasm.framework.unityweb:8:22295)
at stackTrace [Object.stackTrace] (test-webgl.wasm.framework.unityweb:8:22466)
at Object.onAbort (http://localhost/test-webgl/Build/UnityLoader.js:4:11047)
at abort (test-webgl.wasm.framework.unityweb:8:440553)
at _dlopen (test-webgl.wasm.framework.unityweb:8:153940)
at wasm-function[26622]:0x712d43
at wasm-function[27755]:0x7421f0
at wasm-function[27029]:0x725f91
at wasm-function[27028]:0x725f0c
at wasm-function[27031]:0x725ffd
at wasm-function[22381]:0x644125
at wasm-function[29361]:0x79913b
at dynCall_iiiii [Object.dynCall_iiiii] (test-webgl.wasm.framework.unityweb:8:421188)
at invoke_iiiii (test-webgl.wasm.framework.unityweb:8:310968)
at wasm-function[27081]:0x7271b9
at wasm-function[26547]:0x70fc34
at wasm-function[1742]:0x95e96
at wasm-function[1741]:0x95dc4
at wasm-function[5207]:0x1bdae4
at wasm-function[5204]:0x1bceb4
at wasm-function[7482]:0x2b5b02
at wasm-function[5628]:0x1efd40
at wasm-function[7908]:0x2e6d20
at wasm-function[7625]:0x2c442e
at wasm-function[7625]:0x2c4443
at wasm-function[7617]:0x2c3627
at wasm-function[7611]:0x2c1ca4
at wasm-function[29373]:0x7992b9
at dynCall_v [Object.dynCall_v] (test-webgl.wasm.framework.unityweb:8:428673)
at browserIterationFunc (test-webgl.wasm.framework.unityweb:8:158179)
at runIter [Object.runIter] (test-webgl.wasm.framework.unityweb:8:161240)
at Browser_mainLoop_runner (test-webgl.wasm.framework.unityweb:8:159702)

  1. Placing a single .cpp with all the includes of my cpp files.

In this case I further faced 2 problems.

a. If any of my cpp file which is further including third party library I get the following error. Even though I passed the include path of that third party library as emscripten argument using the Unity Editor API as following.

PlayerSettings.WebGL.emscriptenArgs = "-std=c++17 -ID:\\opencv-master\\opencv\\install\\include";

I get the following error.

stdout:
stderr:In file included from path_to_my_file_with_includes_in_unity_plugins_folder.cpp : 1 : path_to_my_cppfile_myfile.cpp:1:10: fatal error: ‘opencv2/opencv.hpp’ file not found#include <opencv2/opencv.hpp> ^~~~~~~~~~~~~~~~~~~~1 error generated.ERROR:root:compiler frontend failed to generate LLVM bitcode, halting
UnityEngine.GUIUtility : ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)

b. I removed all the third party library includes from my cpp file in Plugins folder and Unity generated WebGL build successfully. but I got the same error as above.

Invoking error handler due to
Uncaught abort(“To use dlopen, you need to use Emscripten’s linking support, see Linking · emscripten-core/emscripten Wiki · GitHub”) at Error
at jsStackTrace (test-webgl.wasm.framework.unityweb:8:22295)
at stackTrace [Object.stackTrace] (test-webgl.wasm.framework.unityweb:8:22466)
at Object.onAbort (http://localhost/test-webgl/Build/UnityLoader.js:4:11047)
at abort (test-webgl.wasm.framework.unityweb:8:440553)
at _dlopen (test-webgl.wasm.framework.unityweb:8:153940)
at wasm-function[26622]:0x712d43
at wasm-function[27755]:0x7421f0
at wasm-function[27029]:0x725f91
at wasm-function[27028]:0x725f0c
at wasm-function[27031]:0x725ffd
at wasm-function[22381]:0x644125
at wasm-function[29361]:0x79913b
at dynCall_iiiii [Object.dynCall_iiiii] (test-webgl.wasm.framework.unityweb:8:421188)
at invoke_iiiii (test-webgl.wasm.framework.unityweb:8:310968)
at wasm-function[27081]:0x7271b9
at wasm-function[26547]:0x70fc34
at wasm-function[1742]:0x95e96
at wasm-function[1741]:0x95dc4
at wasm-function[5207]:0x1bdae4
at wasm-function[5204]:0x1bceb4
at wasm-function[7482]:0x2b5b02
at wasm-function[5628]:0x1efd40
at wasm-function[7908]:0x2e6d20
at wasm-function[7625]:0x2c442e
at wasm-function[7625]:0x2c4443
at wasm-function[7617]:0x2c3627
at wasm-function[7611]:0x2c1ca4
at wasm-function[29373]:0x7992b9
at dynCall_v [Object.dynCall_v] (test-webgl.wasm.framework.unityweb:8:428673)
at browserIterationFunc (test-webgl.wasm.framework.unityweb:8:158179)
at runIter [Object.runIter] (test-webgl.wasm.framework.unityweb:8:161240)
at Browser_mainLoop_runner (test-webgl.wasm.framework.unityweb:8:159702)

I hope I can get some response from Unity guys here. Thanks

Ok. Turns out I had a type in the DllImport

I was doing this . [DllImport(“__internal”)]
rather [DllImport(“__Internal”)]

Yes only the I in internal was lower case. Anyway now I can run basic methods. But not those which are linked with some third party framework like opencv. Following is the command.

emcc -g dllmain.cpp -o webgl.js -std=c++17 -s LINKABLE=1 -s EXPORT_ALL=1 -s EXTRA_EXPORTED_RUNTIME_METHODS=‘[“ccall”, “cwrap”]’ -I /Users/ismaeel/Desktop/opencv-master/opencv/install/include/opencv

following is the stack trace

Invoking error handler due to
Uncaught abort(-1) at Error
at jsStackTrace (test_webgl2.wasm.framework.unityweb:8:22295)
at stackTrace [Object.stackTrace] (test_webgl2.wasm.framework.unityweb:8:22466)
at Object.onAbort (http://localhost/test_webgl2/Build/UnityLoader.js:4:11065)
at abort (test_webgl2.wasm.framework.unityweb:8:429902)
at __ZN2cv3Mat6createEiPKii (test_webgl2.wasm.framework.unityweb:8:60334)
at wasm-function[25486]:0x68fefd
at wasm-function[25480]:0x68fbfe
at wasm-function[25478]:0x68f8e4
at wasm-function[25461]:0x68f625
at wasm-function[21248]:0x5e8891
at wasm-function[25834]:0x6a498d
at dynCall_iiiii [Object.dynCall_iiiii] (test_webgl2.wasm.framework.unityweb:8:412942)
at invoke_iiiii (test_webgl2.wasm.framework.unityweb:8:310650)
at wasm-function[23926]:0x65094c
at wasm-function[23406]:0x639810
at wasm-function[1741]:0x8faa3
at wasm-function[1740]:0x8f9d1
at wasm-function[4485]:0x1620ad
at wasm-function[4482]:0x161467
at wasm-function[6934]:0x273284
at wasm-function[4882]:0x19447d
at wasm-function[7810]:0x2d1cc6
at wasm-function[7073]:0x2813dd
at wasm-function[7073]:0x2813f2
at wasm-function[7066]:0x2806c4
at wasm-function[7060]:0x27ed3c
at wasm-function[25845]:0x6a4ae8
at dynCall_v [Object.dynCall_v] (test_webgl2.wasm.framework.unityweb:8:419335)
at browserIterationFunc (test_webgl2.wasm.framework.unityweb:8:157976)
at runIter [Object.runIter] (test_webgl2.wasm.framework.unityweb:8:161037)
at Browser_mainLoop_runner (test_webgl2.wasm.framework.unityweb:8:159499)

even though I can see __ZN2cv3Mat6createEiPKii in my .bc file. :confused: