Building shared libraries with upstream backend

85 views
Skip to first unread message

Dan C

unread,
May 4, 2021, 7:32:36 PM5/4/21
to emscripten-discuss
Hi All,

We are currently building a library with 50 other libraries, and need expose over a thousand functions. With fastcomp backend, we currently create a shared lib with all these libs, and another shared lib worker.so which was linked against the previous shared lib. We then create WebAssembly binaries with these two shared libraries.

When trying to use the latest upstream backend, we encountered this issue https://github.com/emscripten-core/emscripten/issues/9726 for creating the shared libs. We then tried to create WebAssembly binaries with static libs. First difficulty is that we need to list all the 50 static lib names in the cmake file, and we also need list all the 2000 functions for EXPORTED_FUNCTIONS because EMSCRIPTEN_KEEPALIVE doesn't keep the functions alive.

Is there an equivalent of EMSCRIPTEN_KEEPALIVE for static libraries or a way to make it work with them? Is there any alternative approach to group the libraries, so that we don't need to list all the libraries and functions?

Thanks in advance for any suggestions,
Dan

Sam Clegg

unread,
May 4, 2021, 9:23:14 PM5/4/21
to emscripte...@googlegroups.com
EMSCRIPTEN_KEEPALIVE does work on static libraries, but you need to understand how object files in static libraries are included by the linker.   By default only object files that are needed via some symbol dependency will be included.   You can override this behavior using `-Wl,-whole-archive -lmylib `-Wl,-no-whole-archive` which causes the linker to include all the object files as if you have listed them on the command line individually (which in turn will honor all of the `EMSCRIPTEN_KEEPALIVE` statements in those objects).

If you want to continue using shared libraries can also do that instead.   https://github.com/emscripten-core/emscripten/issues/9726 is now fixed and we released a new version of emsdk today which should include that fix (2.0.19).

Bear in mind that the "shared library" support you are referring to is kind of fake.   What emscripten does today when you pass `-shared` is create an intermediate object file (basically combines the input objects into one big object).   The result works like shared libraries but actually you are still statically linking.    If this works for you and you prefer it to using more traditional static libraries you should be able to continue to do that.

cheers,
sam

Thanks in advance for any suggestions,
Dan

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/eccc3847-0ac2-4a88-89a7-51a89e63ea61n%40googlegroups.com.

Dan C

unread,
Jun 18, 2021, 9:19:34 PM6/18/21
to emscripten-discuss

With the latest release, shared libraries work, but not with -flto. I managed to make static libraries work for me anyways.

Thanks Sam!
Dan
Reply all
Reply to author
Forward
0 new messages