Wasmer 导入“wasi_snapshot_preview1”时出错。“proc_exit”:未知导入。预期功能

问题描述 投票:0回答:1

我使用Clang编译以下C文件,

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int length(char* s) {
    return strlen(s);
}
int matrix(int row, int col) {
    printf("this is matrix\n");
    int a[row][col];
    int b[col][row];
    int r[row][row];
    for(int i = 0; i<row; i++) {
        for(int j = 0; j<col; j++) {
            a[i][j] = rand()%1000+1;
            b[j][i] = rand()%1000+1;
        }
    }
    for(int i = 0; i<row; i++) {
        for(int j = 0; j<row; j++) {
            r[i][j] = 0;
            for(int k = 0; k<col; k++) {
                r[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    
    return r[row-1][row-1];
}
int main(){
    int a = matrix(10, 12);
    printf("a: %d\n", a);
    printf("length: %d\n", length("abcd"));
    return 0;
}

我的编译命令如下,

clang --sysroot home/user/wasi-sdk-12.0/share/wasi-sysroot/ \
-Wl,--export-all \
-o matrix.wasm matrix.c

我使用 wasm2wat 将 wasm 文件转换为 wat 格式。文件内容如下导入,

  (import "wasi_snapshot_preview1" "proc_exit" (func $__wasi_proc_exit (type 2))) 
  (import "wasi_snapshot_preview1" "fd_seek" (func $__wasi_fd_seek (type 3)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__wasi_fd_write (type 4)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__wasi_fd_close (type 5))) 
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__wasi_fd_fdstat_get (type 6)))

我使用 wasmer 运行 WebAssembly 文件,

wasmer run matrix.wasm --invoke matrix 10 12

然后出现错误,

error: failed to run `matrix.wasm`
╰─> 1: Error while importing "wasi_snapshot_preview1"."proc_exit": unknown import. Expected Function(FunctionType { params: [I32], results: [] })

我可以成功运行它

wasmer matrix.wasm

我不知道如何使用这些导入行正确调用特定的导出函数。当我删除它们时,程序运行良好。但是,因为我删除了 fd_write 行,所以它不会打印任何内容。我怎样才能成功执行这个程序

wasmer matrix.wasm --invoke matrix 10 12
c compiler-errors clang linker-errors webassembly
1个回答
0
投票

也许你可以将

--target=wasm32-wasi
添加到你的编译命令中。

clang --target=wasm32-wasi --sysroot home/user/wasi-sdk-12.0/share/wasi-sysroot/ \
-Wl,--export-all \
-o matrix.wasm matrix.c

我试过了,一切正常。如果不是这个问题,可能是你电脑上的

clang
wasi-sysroot
版本不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.