The following short c example uses the standard c library and therefore requires the wasi sdk:
#include <stdio.h>
int main(void)
{
puts("Hello");
return 0;
}
When compiling the code directly with clang to wasm it works without problem:
clang --target=wasm32-unknown-wasi -s -o example.wasm example.c
My understanding of the LLVM tool chain is that I could achieve the same result with either
- clang -> LLVM IR (.ll) -> LLVM native object files (.o) -> convert to wasm
- clang -> LLVM native object files (.o) -> convert to wasm
I am able to use the second approach with a simple C program which does not use standard lib calls, when trying with the example above I receive a undefined symbol error:
clang --target=wasm32-unknown-wasi -c example.c
wasm-ld example.o -o example.wasm --no-entry --export-all
wasm-ld: error: example.o: undefined symbol: puts
I do not know if my problem is that I use the wrong clang parameters and therefore not export enough information or that the error is in the wasm-ld
command.
Would be happy if someone could give me more insight into tool chain, thanks
You need to link the standard C library like this:
Replace
${WASI_SYSROOT}
with your own wasi-sysroot path.For your first approach, you can add
-v
to see what actually happened. You can seewasm-ld xxx -lc -L${WASI_SYSROOT}/lib/wasm32-wasi/
as well.