emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.63k stars 3.29k forks source link

`error: undefined symbol: main/__main_argc_argv `when building standalone WebAssembly #18645

Closed ritch2022 closed 1 year ago

ritch2022 commented 1 year ago

I'm new to emscripten, and try to work with standalone WebAssembly. Followed by the blog: https://v8.dev/blog/emscripten-standalone-wasm, but failed. Error msg is error: undefined symbol: main/__main_argc_argv (referenced by top-level compiled C/C++ code) .

Version of emscripten/emsdk:

> emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.24-git
clang version 14.0.6
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/bin

Full link command and output with -v appended:

❯ emcc hello.cpp -O3 -o hello.wasm -v
 "/nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -I/nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include/SDL --sysroot=/nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot -resource-dir=/nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/lib/clang/14.0.6/ -idirafter/nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include -iwithsysroot/include/c++/v1 -Xclang -iwithsysroot/include/compat -O3 -v hello.cpp -c -o /tmp/emscripten_temp_h68ea28m/hello_0.o
clang version 14.0.6
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/bin
 (in-process)
 "/nix/store/73hm4skd1d7a8gr2gx3g0r8m8y0a5f1m-clang-14.0.6/bin/clang-14" -cc1 -triple wasm32-unknown-emscripten -emit-obj --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name hello.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/data/workspace/wasm/demo -nostdsysteminc -resource-dir /nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/lib/clang/14.0.6/ -idirafter /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include -iwithsysroot /include/c++/v1 -D EMSCRIPTEN -I /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include/SDL -isysroot /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot -internal-isystem /nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/lib/clang/14.0.6/include -O3 -fdeprecated-macro -fdebug-compilation-dir=/data/workspace/wasm/demo -ferror-limit 19 -fvisibility default -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_h68ea28m/hello_0.o -x c++ hello.cpp
clang -cc1 version 14.0.6 based upon LLVM 14.0.6 default target x86_64-unknown-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
 /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include/SDL
 /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include/c++/v1
 /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include/compat
 /nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/lib/clang/14.0.6/include
 /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/include
End of search list.
 "/nix/store/lavg09sr4ldwkvga5hj2d8r35j03qmka-emscripten-llvm-3.1.24/bin/wasm-ld" -o hello.wasm /tmp/emscripten_temp_h68ea28m/hello_0.o -L/nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/lib/wasm32-emscripten /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/cache/sysroot/lib/wasm32-emscripten/crt1.o -lGL -lal -lhtml5 -lstandalonewasm -lstubs -lc -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --strip-debug --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=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export-table -z stack-size=5242880 --initial-memory=16777216 --max-memory=16777216 --global-base=1024
 "/nix/store/153vbyz0j1g06bb3x2bggh5k0hya1ijy-binaryen-111/bin/wasm-emscripten-finalize" --dyncalls-i64 --no-legalize-javascript-ffi --standalone-wasm hello.wasm -o hello.wasm --detect-features
 "/nix/store/7y1qagav6xv4k3w0df5d7z91y2d3w4z7-nodejs-18.13.0/bin/node" /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/src/compiler.js /tmp/tmpjkb0c2ae.json
error: undefined symbol: main/__main_argc_argv (referenced by top-level compiled C/C++ code)
warning: Link with `-sLLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _main may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
warning: To build in STANDALONE_WASM mode without a main(), use emcc --no-entry
Error: Aborting compilation due to previous errors
emcc: error: '/nix/store/7y1qagav6xv4k3w0df5d7z91y2d3w4z7-nodejs-18.13.0/bin/node /nix/store/zn6474w5vclmg91vh9gihs33ik0kq09y-emscripten-3.1.24/share/emscripten/src/compiler.js /tmp/tmpjkb0c2ae.json' failed (returned 1)
sbc100 commented 1 year ago

What is in hello.cpp? Do you have main entry point?

If not, the error message is designed to point you to the solution: To build in STANDALONE_WASM mode without a main(), use emcc --no-entry

ritch2022 commented 1 year ago

Here is the hello.cpp

// hello.cpp
#include <stdio.h>

int main() {
  printf("hello, world!\n");
  return 0;
}

I just followed the post section: https://v8.dev/blog/emscripten-standalone-wasm#running-in-wasm-runtimes

sbc100 commented 1 year ago

That is very strange.. that above works fine for me:

$ cat hello.cpp 
// hello.cpp
#include <stdio.h>

int main() {
  printf("hello, world!\n");
  returclang version 14.0.6n 0;
}
$ emcc hello.cpp -O3 -o hello.wasm 
$ node a.out.js 

I notice that you are using some kind of distro package for emscripten and that seems like it might be the most likely reason for you failure. In fact, I can see now that your clang version looks completely wrong. It seem to be clang version 14.0.6 but emscripten 3.1.24 expects clang 16.

Can you try installing emscripten via emsdk to see if that fixes your issue. You might also want to file a bug with the package maintainer. When you use the wrong llvm version emscripten should also be warning you about. You should see something like this:

emcc: warning: LLVM version for clang executable "/usr/bin/clang" appears incorrect (seeing "14.0", expected "16.0") [-Wversion-check]

I'm not sure how you could not be seeing that warning.

sbc100 commented 1 year ago

cc @Ericson2314 who I think was involved with the NixOS package that it looks like you are using?

ritch2022 commented 1 year ago

update:

I found that I used an unstable version of the Nixpkgs repository, So I set it to version 22.11(commit: 285b3ff0660640575186a4086e1f8dc0df2874b5) and re-installed emscripten.

Now emcc version is 3.1.17

❯ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.17-git
clang version 14.0.6
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /nix/store/vml9va213nbs46wikw85sm94mv3bbw1x-emscripten-llvm-3.1.17/bin 

but get the same error

❯ emcc hello.cpp -O3 -o hello.wasm
error: undefined symbol: main/__main_argc_argv (referenced by top-level compiled C/C++ code)
warning: Link with `-sLLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _main may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
warning: To build in STANDALONE_WASM mode without a main(), use emcc --no-entry
Error: Aborting compilation due to previous errors
emcc: error: '/nix/store/mldqcwj80jlcpi8g7bicknq7pxw4sqz9-nodejs-18.12.1/bin/node /nix/store/8da379da6xjlh7w65zbyrhsvks00585p-emscripten-3.1.17/share/emscripten/src/compiler.js /tmp/tmp1ipo2b8c.json' failed (returned 1)
sbc100 commented 1 year ago

That still looks like likely llvm version mismatch. Emscripten 3.1.17 is supposed to be used with llvm 15:

$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.17 (fbc532773d84d2bd7da876275671970e792ad1cd)
clang version 15.0.0 (https://github.com/llvm/llvm-project 17e4c217b66305e60657a48f10fe3c428c2fe4d2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/google/home/sbc/dev/wasm/emsdk/upstream/bin

See the clang version 15.0.0 line above. (Also note that emscripten always depends on a specific version of llvm, not an official release).

Please try using emsdk, at least just temporarily. If that fixes your issue then this is likely bug in the NixOS packaging and not an emscripten issue.

ritch2022 commented 1 year ago

Using emsdk, it works. It should be issue of Nixpkgs, I will try feedback this.

sbc100 commented 1 year ago

Great news! Should we close this issue now?

ritch2022 commented 1 year ago

ok I will close it.