FLAGS = -O0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s TOTAL_MEMORY=134217728 \
-s NO_EXIT_RUNTIME=1 \
-s FORCE_FILESYSTEM=1 \
--memory-init-file 0 \
-s MODULARIZE=1 \
-s WASM=1 \
-s EXPORT_ES6=1 \
-s EXPORTED_FUNCTIONS="['_main']" \
-s EXPORTED_RUNTIME_METHODS=intArrayFromString,allocate,ALLOC_NORMAL \
-DNODEPS=1
That also generates the .wasm file and a .js file just fine, however when I include it in my regular code the "main" function is always called automatically without any arguments. Also the output is correct and just lists the supported parameters - so everything fine and as expected.
HOWEVER:
As it's a C-program I need the "main" function so it needs to be listed in "EXPORTED_FUNCTIONS" as far as I understood, but I want to call it in my code manually at a later point in time with some arguments. So the initial, automatic call is just not necessary.
What I tried so far:
I found some hints to add flags like --no-entry, INVOKE_MAIN=0, NO_INITIAL_RUN=1
and even tried to add Module.noInitialRun = true
or Module["noInitialRun"] = true
in my JavaScript code before calling Module().then(...
But none of it works, nor is there any documentation about some of the flags, nor that they have been there but removed as they are deprecated (which is also strange)
emcc -v
returns:
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 6865cff8ea8b07d9f2385fd92cecb422404f0f35)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/emscripten/3.1.35/libexec/llvm/bin
Question:
What do I have to configure/set to avoid that the 'main' function of the C program is automatically called when just loading the generated .js and .wasm?
Original comment by Ruud Helderman:
Try -Dmain=SomeOtherName; Emscripten documentation claims it is possible to "use emcc as a drop-in replacement for gcc", which suggests you can use -D to rename main without changing the source code.
As proposed by @ruud-helderman I tried to set -Dmain=runMain
and also added the "virtual" function runMain
to -s EXPORTED_FUNCTIONS="['_runMain']
and this worked for me and "main" is not run twice anymore perfect
I was able to call it now like the following in my JavaScript code:
import Module from './bin/test.js';
class TestWasm {
_testModule = null;
async init() {
this._testModule = await Module();
}
runProgram(args) {
this._testModule['_runMain'](argc, argv, 0);
}
}