wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

What is the difference between clang and em++?

Open kamylee opened this issue 1 year ago • 4 comments

I compiled the libjsoncpp.a library using Emscripten. When I tried to compile wasm using clang and linked this library, it resulted in errors at runtime. cmd: clang -O3 -nostdlib \ -pthread \ -Wl,--shared-memory,--max-memory=131072 \ -o test.wasm test.cpp ./lib/libjsoncpp.a \ -Wl,--export=init -Wl,--export=onMessage \ -Wl,--export=__heap_base -Wl,--export=__data_end \ -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined err: warning: failed to link import function (env, _Znwm) warning: failed to link import function (env, _ZdlPv) warning: failed to link import function (env, _ZNSt12length_errorD1Ev) warning: failed to link import function (env, _ZNSt11logic_errorC2EPKc) warning: failed to link import function (env, _ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE9__grow_byEmmmmmm)

However, when I compiled the wasm using em++, it could parse JSON correctly at runtime. cmd: em++ -g -O3 test.cpp lib/libjsoncpp.a -o test.wasm --no-entry -s WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s EXPORTED_FUNCTIONS=_init,_onMessage result: image

Is it because clang uses wasi-sdk, while em++ uses Emscripten? Does wasi-sdk not support jsoncpp? How to solve? Thank you!

kamylee avatar Apr 03 '24 07:04 kamylee

Hi, you are using clang rather than clang++ to compile a cpp program. To use clang++ instead should be able to fix the compilation error

TianlongLiang avatar Apr 03 '24 07:04 TianlongLiang

Thank you for your reply! I tried using clang++,But it ditn't work still. still the same error.

kamylee avatar Apr 03 '24 09:04 kamylee

What if you try to remove -nostdlib? If that doesn't work, then I think maybe you are right at first, it could be wasi-sdk problem

TianlongLiang avatar Apr 03 '24 09:04 TianlongLiang

The errors you get seem to be from exception related imports that cannot be resolvedas the runtime does not provide the required imports for the module.

If you look at the readme on wasi-sdk main page you can see that it does not support exceptions. Also note that since the exceptions come from std, using nostdlib will possibly make the missing classes into imports that then fail as you try to run the program.

This repository does not yet support C++ exceptions. C++ code is supported only with -fno-exceptions for now.

So you could try including the -fno-exceptions flag for wasi-sdk/clang. I believe this should result in an abort of the program when exception is thrown.

Emscripten by default does not support exceptions either. However, it will allow the code to compile and run. It will abort the entire program when an exception occurs by default. The docs say that there is some kind of support possible, but I never got it to work (tried for few hours to get lua to compile to wasm with exceptions or longjmp). See emscripten docs on exceptions.

Rochet2 avatar Apr 03 '24 13:04 Rochet2