I have been following the guidelines provided in the Emscripten documentation here, and I am interested in experimenting with basic examples. My ultimate objective is to develop a C++ library, compile it into a single wasm binary file, and then utilize these compiled functions in a frontend web application.
I am puzzled about the need for calling C++ methods with "ccall" when it seems like they can be accessed directly from "Module". For instance, instead of using
Module.ccall('doubleNumber','number', ['number'], [9]);
, I could just use Module._doubleNumber(9);
, which appears to be simpler.
Can someone explain the distinction between these two approaches?
Below is the complete code snippet:
extern "C" {
int doubleNumber( int x ) {
int res = 2*x;
return res;
}
}
Compiled command:
emcc main.cpp -o main.js -sEXPORTED_FUNCTIONS=_doubleNumber -sEXPORTED_RUNTIME_METHODS=ccall
I am utilizing a straightforward HTML document to evaluate the outcomes:
<html>
<head>
<meta charset="utf-8">
<title>WASM Demo</title>
</head>
<body>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript">
Module.ccall('doubleNumber','number', ['number'], [9]); // 18
Module._doubleNumber(9); // 18
</script>
</body>
</html>