Reputation: 365
I use Clang to compile the following C file,
#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;
}
My compile command is as follows,
clang --sysroot home/user/wasi-sdk-12.0/share/wasi-sysroot/ \
-Wl,--export-all \
-o matrix.wasm matrix.c
And I use wasm2wat to translate wasm file into wat format. The file content the following import,
(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)))
I run the webassembly file with wasmer,
wasmer run matrix.wasm --invoke matrix 10 12
Then the error appears,
error: failed to run `matrix.wasm`
╰─> 1: Error while importing "wasi_snapshot_preview1"."proc_exit": unknown import. Expected Function(FunctionType { params: [I32], results: [] })
I can run it successfully with
wasmer matrix.wasm
I don't know how to invoke a specific exported function correctly with these import lines. When I delete them, the program goes on well. However, because I delete the fd_write line, it does not print anything. How can I succefully execute this program with
wasmer matrix.wasm --invoke matrix 10 12
Upvotes: 2
Views: 2331
Reputation: 113
Maybe you can add --target=wasm32-wasi
to your compile command.
clang --target=wasm32-wasi --sysroot home/user/wasi-sdk-12.0/share/wasi-sysroot/ \
-Wl,--export-all \
-o matrix.wasm matrix.c
I tried this, everything works fine. If not this problem, maybe the version of clang
and wasi-sysroot
on your computer doesn't match.
Upvotes: 0