October 22, 2024
Chicago 12, Melborne City, USA
C#

How to avoid that emscripten calls “main” function automatically

I want to generate a WebAssembly from a C-program using Emscripten.
I’m using the following flags for emcc:

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?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video