I'm curious if it's possible to have emscripten build a binary without libc.
If I have simple.c:
int add1(int x) {
return x + 1;
}
And I don't want to include any of libc, is that possible?
My best attempt so far has been:
emcc simple.c -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=1 -s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE='[]' -s LIBRARY_DEPS_TO_AUTOEXPORT='[]' -s EXPORTED_FUNCTIONS='["add1"]' -s USE_SDL=0 -o simple.js -nostdlib
But the generated output still includes symbols for malloc, string conversion routines, and so on.
I'm also interested in the same process for generating webassembly; that is, my real goal is generating a webassembly module that contains only a single function. Is this possible with emscripten?
Emscripten is not currently well-suited for this use case. Object files are bitcode, and anytime linking is used, it wants to include some libraries and JS module code.
For WebAssembly, it is possible to use clang from upstream LLVM trunk along with the s2wasm
tool from Binaryen. Clang (optionally in conjunction with LLVM's llc
tool) can generate a .s
assembly file which s2wasm
can convert into wast
(WebAssembly's text format). This file can be converted to wasm binary using Binaryen's wasm-as
tool or the wast2wasm
tool from WABT. This pipeline will turn any undefined references to functions from the C file into wasm imports, which you'll need to manually hook up to JavaScript functions.
There are a few examples of this floating around on the Internet (I found some by googling "webassembly clang s2wasm"). Note also that s2wasm
does the linker's job of laying out the wasm linear memory, and it has several options that you'll want to be aware of; see its help output and its sources, e.g. the tool and linker code.