使用 Emscripten 从 C 编译成 WebAssembly
Compiling from C to WebAssembly using Emscripten
我想将程序从 C 编译为 WebAssembly。该程序应取一个数字,计算给定数字以内的素数,然后计算 return 经过的时间(以微秒为单位)。为了编译我使用了 Emscripten 并得到了这样的错误:
“C:\Users\Pawel\Desktop\Wasm\main.c:20:1: 错误:函数 'calcPrimes' 的隐式声明在 C99
中无效
[-Werror,-Wimplicit-function-declaration]
calcPrimes(数字);
^
产生 1 个错误。
emcc: 错误: 'C:/Users/Pawel/Desktop/emsdk/emsdk/upstream/bin\clang.exe -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=1 -D__EMSCRIPTEN_minor__=39 -D__EMSCRIPTEN_tiny__=15 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -Werror=implicit-function-declaration -Xclang -nostdsysteminc -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\compat -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\libc -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\lib\libc\musl\arch\emscripten -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\local\include -Xclang -isystemC:\Users\Pawel.emscripten_cache\wasm\include -DEMSCRIPTEN -fignore-exceptions C:\Users\Pawel\Desktop\Wasm\main.c -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\SDL -c -o C:\Users\Pawel\AppData\Local\Temp\emscripten_temp_nqmlki_g\main_0.o -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr' 失败 (1)"
在此先感谢您的帮助!
我的代码:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
long getDuration(int number){
struct timespec ts;
struct timespec {
time_t tv_sec;
long tv_nsec;
};
timespec_get(&ts, TIME_UTC);
long startsc = ts.tv_sec;
long startns = ts.tv_nsec;
calcPrimes(number);
timespec_get(&ts, TIME_UTC);
long endsc = ts.tv_sec;
long endns = ts.tv_nsec;
long resus = (endsc - startsc) * 1000000 + (endns/1000) - (startns/1000);
return resus;
}
int isPrime(int num) {
int i;
if(num == 2) return 1;
if(num % 2 == 0) return 0;
int sq = (int) sqrt(num) + 1;
for(i = 3; i < sq; i = i + 2) if(num % i == 0) return 0;
return 1;
}
int calcPrimes(int n) {
int i, count = 0;
for(i = 2; i <= n; i++) if(isPrime(i)) count++;
return count;
}
您缺少 calcPrimes
的前向声明。您要么需要添加一个,要么将文件中的 getDuration
移动到 calcPrimes
下方。
我想将程序从 C 编译为 WebAssembly。该程序应取一个数字,计算给定数字以内的素数,然后计算 return 经过的时间(以微秒为单位)。为了编译我使用了 Emscripten 并得到了这样的错误:
“C:\Users\Pawel\Desktop\Wasm\main.c:20:1: 错误:函数 'calcPrimes' 的隐式声明在 C99
中无效
[-Werror,-Wimplicit-function-declaration]
calcPrimes(数字);
^
产生 1 个错误。
emcc: 错误: 'C:/Users/Pawel/Desktop/emsdk/emsdk/upstream/bin\clang.exe -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=1 -D__EMSCRIPTEN_minor__=39 -D__EMSCRIPTEN_tiny__=15 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -Werror=implicit-function-declaration -Xclang -nostdsysteminc -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\compat -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\libc -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\lib\libc\musl\arch\emscripten -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\local\include -Xclang -isystemC:\Users\Pawel.emscripten_cache\wasm\include -DEMSCRIPTEN -fignore-exceptions C:\Users\Pawel\Desktop\Wasm\main.c -Xclang -isystemC:\Users\Pawel\Desktop\emsdk\emsdk\upstream\emscripten\system\include\SDL -c -o C:\Users\Pawel\AppData\Local\Temp\emscripten_temp_nqmlki_g\main_0.o -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr' 失败 (1)"
在此先感谢您的帮助!
我的代码:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
long getDuration(int number){
struct timespec ts;
struct timespec {
time_t tv_sec;
long tv_nsec;
};
timespec_get(&ts, TIME_UTC);
long startsc = ts.tv_sec;
long startns = ts.tv_nsec;
calcPrimes(number);
timespec_get(&ts, TIME_UTC);
long endsc = ts.tv_sec;
long endns = ts.tv_nsec;
long resus = (endsc - startsc) * 1000000 + (endns/1000) - (startns/1000);
return resus;
}
int isPrime(int num) {
int i;
if(num == 2) return 1;
if(num % 2 == 0) return 0;
int sq = (int) sqrt(num) + 1;
for(i = 3; i < sq; i = i + 2) if(num % i == 0) return 0;
return 1;
}
int calcPrimes(int n) {
int i, count = 0;
for(i = 2; i <= n; i++) if(isPrime(i)) count++;
return count;
}
您缺少 calcPrimes
的前向声明。您要么需要添加一个,要么将文件中的 getDuration
移动到 calcPrimes
下方。