Skip to content

Confusion about EM_JS functions and dead code elimination #18957

Closed
@hoodmane

Description

@hoodmane

How do I prevent dead code elimination from removing an EM_JS function I define in a different compilation unit? I have mostly been using -sMAIN_MODULE=1 which prevents these errors but now I am trying to use -sMAIN_MODULE=2 or no dynamic linking builds and I am running into trouble with missing EM_JS symbols. I assume I am messing up something simple. How do I prevent these missing symbol errors? (See full example below).

@antocuni

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.32 (eab98adf462c39f3c31d348331c4830bcaa36949)

Failing command line in full:

main.c

void say_hello(void);

int
main(void) {
    say_hello();
}

hello.c

#include <emscripten.h>

EM_JS(void, say_hello, (void), {
    console.log("Hello from JavaScript");
})

Compilation

$ emcc -c hello.c -o hello.o
$ emcc -c main.c -o main.o

Full link command and output with -v appended

$ emcc main.o hello.o -o main.js -v
 "/home/ubuntu/pyodide/emsdk/emsdk/upstream/bin/clang" --version
 "/home/ubuntu/pyodide/emsdk/emsdk/upstream/bin/wasm-ld" -o main.wasm main.o hello.o -L/home/ubuntu/pyodide/emsdk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/tmp/tmp6cceqbst.undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export-table -z stack-size=65536 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
wasm-ld: error: main.o: undefined symbol: say_hello
emcc: error: '/home/ubuntu/pyodide/emsdk/emsdk/upstream/bin/wasm-ld -o main.wasm main.o hello.o -L/home/ubuntu/pyodide/emsdk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/tmp/tmp6cceqbst.undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export-table -z stack-size=65536 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024' failed (returned 1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions