WebAssembly / wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
Apache License 2.0
1.29k stars 192 forks source link

Fail compilation. #356

Closed orangeC23 closed 1 year ago

orangeC23 commented 1 year ago

The C program is :

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

int main() {
    const char *filename = "hello.txt";

    int fd = open(filename, O_RDWR);
    if (fd == -1) {
        perror("Failed to open file");
        return 1;
    }

    struct timeval times[2];
    gettimeofday(&times[0], NULL);
    times[1] = times[0];

    struct timespec timespec_times[2];
    timespec_times[0].tv_sec = times[0].tv_sec;
    timespec_times[0].tv_nsec = times[0].tv_usec * 1000;
    timespec_times[1].tv_sec = times[1].tv_sec;
    timespec_times[1].tv_nsec = times[1].tv_usec * 1000;

    if (futimens(fd, timespec_times) == -1) {
        perror("futimens");
        close(fd);
        return 1;
    }

    close(fd);

    return 0;
}

Use command ../../../../WASI/wasi-sdk-20.0/bin/clang --target=wasm32-unkown-wasi --sysroot=../../../../WASI/wasi-sysroot -nodefaultlibs -lc tmp.c -o tmp.wasm to compile it into WASM binaries.

It reports:

wasm-ld: error: ../../../../WASI/wasi-sysroot/lib/wasm32-wasi/libc.a(utimensat.o): undefined symbol: __muloti4
wasm-ld: error: ../../../../WASI/wasi-sysroot/lib/wasm32-wasi/libc.a(utimensat.o): undefined symbol: __muloti4
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)
sbc100 commented 1 year ago

The problem is that -nodefaultlibs .. that option tells the linker not to link libclang-rt which contains those math symbols.

I would try simply removing -nodefaultlibs -lc. If you really want to specify your own libc there is a separate -nolibc option.

orangeC23 commented 1 year ago

Thanks a lot! It works now !