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 2^3=8.

All source code has been uploaded to GitHub: exportFunc

Categories: Web

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

X
0
Would love your thoughts, please comment.x
()
x