Comiple Emscripten LLVM bitcode to use in Unity

I’ve built a simple static lib from very simple .c source file into a .bc file but I can’t never call the function from c#.
My .c file is:

#include <stdio.h>

int AddNumbers (int x, int y)
{
    return x + y;
}

and then on c#

    [DllImport("__Internal")]
    private static extern int AddNumbers(int x, int y);

To compile the .c file I’ve created a MakeFile with c flags set to ‘-emit-llvm’ using CMake and compiled it with Emscripten

./emmake make

It generates a .a lib file that I’ve renamed to .bc and moved into Unity Assets and marked as a WebGL plugin.
When i call the AddNumber method I always get this error:

An error occurred running the Unity content on this page. See your browser's JavaScript console for more info. The error was:
abort(-1) at jsStackTrace (WebGLPlugin.js:1:19708)
stackTrace (WebGLPlugin.js:1:19882)
abort (WebGLPlugin.js:3:32695)
_AddNumbers (WebGLPlugin.js:1:186759)
Pwo (WebGLPlugin.asm.js:10:844115)
N_l (WebGLPlugin.asm.js:4:87306)

If I just drop the .c file inside Unity Assets it runs without a problem.
The reason I want to create the .bc file is because I want to compile a much larger code base.

I’ve followed from this thread

Thanks

emscripte version is same or not?

Unity 5.5.0f3 is use emscripten 1.36.7

I’m using Unity 5.5.2f1 and i was now able to compile it.
I had to set CMake compiler and linker executables to use the ones from Emscripten.

I’m now getting this error from the .bc file.

    abort(-1) at jsStackTrace (WebGLPlugin.js:1:19708)
    stackTrace (WebGLPlugin.js:1:19882)
    abort (WebGLPlugin.js:3:32695)
    _llvm_objectsize_i64_p0i8 (WebGLPlugin.js:1:299381)
    t3e (WebGLPlugin.asm.js:11:461661)
    a4e (WebGLPlugin.asm.js:11:498065)
    ...

maybe you can try build .bc use shared lib ( not static lib )

in my case.
I’m install emscripten from Official Website and use CMake + VisualStudio to build .bc (shared lib) file.

it’s work for me.
FYR:
// CMake File

// Build batch
https://github.com/effekseer/EffekseerForUnity/blob/master/Dev/Cpp/webgl/Build.bat

Thank you. I didn’t have time to go back into this project again, but i will inform about my success or not.

1 Like