WASI: wasm-ld symbol not found

637 views Asked by At

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

  1. clang -> LLVM IR (.ll) -> LLVM native object files (.o) -> convert to wasm
  2. 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

1

There are 1 answers

0
Dbettkk On

You need to link the standard C library like this:

wasm-ld example.o -o example.wasm  --no-entry --export-all \
  -lc -L${WASI_SYSROOT}/lib/wasm32-wasi/ 

Replace ${WASI_SYSROOT} with your own wasi-sysroot path.

For your first approach, you can add -v to see what actually happened. You can see wasm-ld xxx -lc -L${WASI_SYSROOT}/lib/wasm32-wasi/ as well.