hyperlinkemscriptenlibm

How can I link libm to my emscripten port


I am trying to use emscripten to port some C code that requires libm. Usually, it uses my system's version, and I don't need to worry about linking to it, but I need to manually link it with emscripten.

How can I link libm?

I have tried using openlibm, but when I make it with emcc (the emscripten compiler) it cannot find all dependencies, as openlibm still depends on system headers.

I have also tried using the GNU libc source, but cannot navigate those Makefiles.

What I need is the bitcode, as generated by emcc, to link to when compiling to JavaScript, using the -lm flag in the compiler.


Solution

  • libm is linked by default.

    The emcc compiler will not be able to link it if it is specified (as -lm), as this requires an explicit search path (with -L/path/). It has an internal implementation which should not be overwritten, unless you can supply your own emcc-compiled bitcode.

    The compiler will throw warnings for the unresolved library until you compile to JavaScript, as libm is not included until the final compile. These errors on the intermediate targets can and should be ignored - it is a known issue.

    The available libraries through the compiler can be found at https://github.com/kripken/emscripten/tree/master/system/include. Specifically, libm is included in the musl headers. It is not explicit, but that is covered in the musl FAQs.