Create a file GetPrimers.c which has a simple math function.
#include <stdio.h>
// export the function.
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
#endif
int compute( int a, int b )
{
int ans = 1;
for( int i = 0; i < b; ++i )
{
ans = ans * a;
}
return ans;
}
The declaration EMSCRIPTEN_KEEPALIVE
helps us to export the function compute
.
Compile the C code to generate an Emscripten plumbing js file and wasm file. EXPORTED_RUNTIME_METHODS
tells the compiler that we want to use the runtime functions ccall
and cwrap
.
emcc GetPrimers.c -o GetPrimers.js -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]'
Then add javascript code to use C function compute
to get .
All source code has been uploaded to GitHub: exportFunc