emscripten-core / emscripten-core/emscripten

--embind-emit-tsd fails when linking dynamic libraries

Open
#21,450 4 comments 0 reactions 1 assignee Claimed by @brendandahl View on GitHub
TypeScript
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

`emcc` fails when generating typescript definitions when a dynamic library is being linked.

Here's a zip file that contains some sample code that reproduces the issue. Run `build.sh` at the root of the zip file to get the error.

[ErrorWithDynamicLinkingAndEmbindAndTSDGen.zip](https://github.com/emscripten-core/emscripten/files/14437793/ErrorWithDynamicLinkingAndEmbindAndTSDGen.zip)

I think the problem is that the tsd generation requires the dynamic library(`.so`) file to be in the Emscripten temp directory in order to succeed. I discovered this by setting `EMCC_DEBUG=2`, which preserves the contents of the Emscripten temp directory. Then, I copy the `.so` file over to the temp directory and re-run the node command (eg: `node /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_u26tl33o/tsgen_a.out.js`). It properly emits the tsd after I do this.

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.54 (a95c44ee924d02fa1498f846595485d27c31daa8)
clang version 19.0.0git (https://github.com/llvm/llvm-project e769fb8699e3fa8e40623764f7713bfc783b0330)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/d.cheng/projects/emsdk/upstream/bin
```
**Failing command line in full:**
```
➜ ~/projects/DynamicLinkingAndEmbindWithTSDGeneration git:(main) ✗ emcc main.cpp MainLibrary.cpp SomeLibrary/SomeLibrary.so --bind --embind-emit-tsd=MainLibrary.d.ts -sMAIN_MODULE=2 -o main.html
/private/var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_f2ow26jt/tsgen_a.out.js:136
throw ex;
^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "undefined".] {
code: 'ERR_UNHANDLED_REJECTION'
}
emcc: error: '/Users/d.cheng/projects/emsdk/node/16.20.0_64bit/bin/node /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_f2ow26jt/tsgen_a.out.js' failed (returned 7)
```

**Full link command and output with `-v` appended:**
```
➜ ~/projects/DynamicLinkingAndEmbindWithTSDGeneration git:(main) ✗ emcc -v main.cpp MainLibrary.cpp SomeLibrary/SomeLibrary.so --bind --embind-emit-tsd=MainLibrary.d.ts -sMAIN_MODULE=2 -o main.html > output.txt
/Users/d.cheng/projects/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fPIC -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v main.cpp -c -o /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/main_0.o
clang version 19.0.0git (https://github.com/llvm/llvm-project e769fb8699e3fa8e40623764f7713bfc783b0330)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/d.cheng/projects/emsdk/upstream/bin
(in-process)
"/Users/d.cheng/projects/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +mutable-globals -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/Users/d.cheng/projects/DynamicLinkingAndEmbindWithTSDGeneration -v -fcoverage-compilation-dir=/Users/d.cheng/projects/DynamicLinkingAndEmbindWithTSDGeneration -resource-dir /Users/d.cheng/projects/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -isysroot /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /Users/d.cheng/projects/emsdk/upstream/lib/clang/19/include -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include -Werror=implicit-function-declaration -fdeprecated-macro -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/main_0.o -x c++ main.cpp
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-apple-darwin23.3.0
ignoring nonexistent directory "/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/compat
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
/Users/d.cheng/projects/emsdk/upstream/lib/clang/19/include
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/Users/d.cheng/projects/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fPIC -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v MainLibrary.cpp -c -o /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/MainLibrary_1.o
clang version 19.0.0git (https://github.com/llvm/llvm-project e769fb8699e3fa8e40623764f7713bfc783b0330)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/d.cheng/projects/emsdk/upstream/bin
(in-process)
"/Users/d.cheng/projects/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MainLibrary.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +mutable-globals -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/Users/d.cheng/projects/DynamicLinkingAndEmbindWithTSDGeneration -v -fcoverage-compilation-dir=/Users/d.cheng/projects/DynamicLinkingAndEmbindWithTSDGeneration -resource-dir /Users/d.cheng/projects/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -isysroot /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /Users/d.cheng/projects/emsdk/upstream/lib/clang/19/include -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include -Werror=implicit-function-declaration -fdeprecated-macro -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/MainLibrary_1.o -x c++ MainLibrary.cpp
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-apple-darwin23.3.0
ignoring nonexistent directory "/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/compat
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
/Users/d.cheng/projects/emsdk/upstream/lib/clang/19/include
/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/Users/d.cheng/projects/emsdk/upstream/bin/clang --version
/Users/d.cheng/projects/emsdk/upstream/bin/wasm-ld -o main.wasm /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/main_0.o /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/MainLibrary_1.o SomeLibrary/SomeLibrary.so -L/Users/d.cheng/projects/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/pic --whole-archive -lembind-rtti --no-whole-archive -lGL-getprocaddr -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 /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/tmpwtoak2nplibemscripten_js_symbols.so --import-memory --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_set_limits --export=stackAlloc --export=stackSave --export=stackRestore --export=__get_temp_ret --export=__set_temp_ret --export=__wasm_call_ctors --export=setThrew --export=malloc --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 --export-if-defined=__main_argc_argv --export-if-defined=__wasm_apply_data_relocs --export-if-defined=fflush --export-if-defined=_ZNKSt3__26locale9use_facetERNS0_2idE --export-if-defined=_ZNKSt3__28ios_base6getlocEv --export-if-defined=_ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEmc --export-if-defined=_ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE3putEc --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE5flushEv --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryC1ERS3_ --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryD1Ev --export-if-defined=_ZNSt3__217__compressed_pairINS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE5__repES5_EC2B8ue170004INS_18__default_init_tagESA_EEOT_OT0_ --export-if-defined=_ZNSt3__24coutE --export-if-defined=_ZNSt3__25ctypeIcE2idE --export-if-defined=_ZNSt3__26localeD1Ev --export-if-defined=_ZNSt3__28ios_base5clearEj --export-if-defined=__memory_base --export-if-defined=__stack_pointer --export-if-defined=__table_base --export-if-defined=strlen --experimental-pic --unresolved-symbols=import-dynamic -pie --no-export-dynamic -z stack-size=65536 --initial-memory=16777216 --max-memory=16777216 --no-entry --stack-first
/Users/d.cheng/projects/emsdk/upstream/bin/llvm-objcopy main.wasm main.wasm --remove-section=.debug* --remove-section=producers
/Users/d.cheng/projects/emsdk/upstream/bin/wasm-emscripten-finalize --dyncalls-i64 --pass-arg=legalize-js-interface-export-originals --pass-arg=legalize-js-interface-exported-helpers main.wasm -o main.wasm --detect-features
/Users/d.cheng/projects/emsdk/node/16.20.0_64bit/bin/node /Users/d.cheng/projects/emsdk/upstream/emscripten/src/compiler.mjs /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/tmpikf32zbf.json
/Users/d.cheng/projects/emsdk/node/16.20.0_64bit/bin/node /Users/d.cheng/projects/emsdk/upstream/emscripten/src/compiler.mjs /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/tmp77ofg5sv.json
/Users/d.cheng/projects/emsdk/node/16.20.0_64bit/bin/node /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/tsgen_a.out.js
/private/var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/tsgen_a.out.js:136
throw ex;
^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "undefined".] {
code: 'ERR_UNHANDLED_REJECTION'
}
emcc: error: '/Users/d.cheng/projects/emsdk/node/16.20.0_64bit/bin/node /var/folders/_6/dll3kgx93qz05hvnymcr5jzh0000gp/T/emscripten_temp_1ncybetu/tsgen_a.out.js' failed (returned 7)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.