0
votes

I want to compile a program from C to WebAssembly. The program should take a number, calculate prime numbers up to the given number and then return elapsed time (in microseconds). For compiling I used Emscripten and got an error like that:

"C:\Users\Pawel\Desktop\Wasm\main.c:20:1: error: implicit declaration of function 'calcPrimes' is invalid in C99
[-Werror,-Wimplicit-function-declaration]
calcPrimes(number);
^
1 error generated.
emcc: error: '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' failed (1)"

Thanks in advance for any help!
My code:

#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;
}
1

1 Answers

1
votes

You are missing a forward declaration of calcPrimes. You either need to add one, or move the getDuration below calcPrimes in your file.