Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the difference between clang and em++? #3277

Open
kamylee opened this issue Apr 3, 2024 · 4 comments
Open

What is the difference between clang and em++? #3277

kamylee opened this issue Apr 3, 2024 · 4 comments

Comments

@kamylee
Copy link

kamylee commented Apr 3, 2024

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!

@TianlongLiang
Copy link
Contributor

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

@kamylee
Copy link
Author

kamylee commented Apr 3, 2024

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

@TianlongLiang
Copy link
Contributor

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

@Rochet2
Copy link
Contributor

Rochet2 commented Apr 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants