Compiling to wasm32 with emscripten an app that uses a C library

#1

I’m trying to compile a Rust app that uses a C library (libtcod) but I’m having an issue when compiling it with cargo build --target wasm32-unknown-emscripten

The actual error is:

error: linking with `emcc` failed: exit code: 1
  |
  = note: "emcc" "-s" "DISABLE_EXCEPTION_CATCHING=0" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.158wz1xuz2s5vbhj.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.16i0u6jlhoj1fwbo.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.16tydtzzxpnaflgw.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.16u6js6g0l3k1ic6.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.181cuta0v63atwcm.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1ca4zz4e4zskwx3q.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1im38lueib99jsk0.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1jnlhyxj59jycbjv.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1kduva7sc7em934m.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1mvmz58owquyropc.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1o6b1la7g4jnv39b.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1tjp4sat87ohv0s4.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1y16o1qfye96o7m0.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1zeawhkbeobww1zn.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.1zwd8n7bcl3vhvvh.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.23tqyymcb18u96mb.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2a701tzw2p0lt28v.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2is0uik3u3ki3kai.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2jqywn86b2gsqohu.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2lyh15q6cjwzy18c.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2o249ai2g7bpvy5h.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2qhkzqx5zqexj20y.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2wixplf1qzszxq14.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.2xnvmuhjbhd7vxcm.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3171x0bwu82dptu7.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.35dif2yfqvun9jag.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.360n40d30atuqugt.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.37w14dd6qvzg70fz.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.38ps4pa181wsnsy9.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3ayaeypdcro9d6yk.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3kfx4ynvkmi2y9i5.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3ldk0i2zxftngav8.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3nplons1c017dtrc.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3oqznv5oym7l3m4e.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3rngp6bm2u2q5z0y.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3wn85ajn7ufppyxt.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.3wta9ctgdrpkmlpr.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.436dotimmrgzkwfa.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.43v6g0y2xsxoggnt.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.44fwlp5wvpf2x6hd.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.45sud1ub1th7k9to.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.48721dc4k5qxei0u.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.49a7n47po4ttqjl7.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.49lx1q7cxvpykyv0.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4ezmh1vbs95c5ack.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4g8y2se10t8swbo4.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4gbgp5a7xd4lch9n.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4gz8df9p8esl5anp.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4hvwaj80f504uofg.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4hyqbavotgq89ckf.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4mz25kegc2cy2nje.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4oc10dk278mpk1vy.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4oxej6yqxc1k2a95.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4xq48u46a1pwiqn7.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4yh8x2b62dcih00t.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.4ypvbwho0bu5tnww.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.56dly8q07ws8ucdq.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.571cm6ai9wkynned.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.5by9xggibmun5kn9.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.5fyogldvlpmb3ne2.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.5gf6du7k58s78kob.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.5ja6ksszgmblgzy.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.7k6tdnj1zc9vuhv.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.81jpvh8cn5k8ng8.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.8xzrsc1ux72v29j.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.98g0d9x8aw3akpe.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.9elsx31vb4it187.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.bxp0med4do8g2u9.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.c6lbtaiefvx3wya.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.ddmt0h0tjfpebjj.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.jtxtkd93cyq9dz.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.kclyx7of0fal6jf.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.kkh23rkehqzaj9d.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.mz7vgmcf23rofcc.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.oa3rad818d8sgn4.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.v6ozwtpojmqfurc.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.wleeoia16qcv6b6.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.y08g5q2x813c4wx.rcgu.o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.z9ox7biyn1otfln.rcgu.o" "-o" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.js" "-s" "EXPORTED_FUNCTIONS=[\"_main\",\"_rust_eh_personality\"]" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/roguelikers.crate.allocator.rcgu.o" "-O0" "--memory-init-file" "0" "-g4" "-s" "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]" "-L" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps" "-L" "/Users/tanis/Documents/roguelikers/target/debug/deps" "-L" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/build/tcod-sys-04a11e1e2538a818/out" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libserde_json-f23faf1e93e85e76.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libitoa-4979e99f1cfeaae3.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libryu-0b51450a86a9087b.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/librand-18a3c73795845ee4.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/librand-c46dbcb2ba8e018b.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libtcod-e9c601f4a37ea109.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libtcod_sys-229209c0cafc1991.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/liblibc-01fc46e4f35f1521.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libserde-2e5208293274b70f.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/liblazy_static-8b5ea448172b99de.rlib" "/Users/tanis/Documents/roguelikers/target/wasm32-unknown-emscripten/debug/deps/libbitflags-6c9329b96559b1f8.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/libstd-9a440ef90899818f.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/libpanic_unwind-7bed87070cafeede.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/libunwind-8cd3b0417a81fb26.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/liballoc_system-387bd949d1b36a91.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/liblibc-453d825a151d7dec.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/liballoc-5235bf36189564a3.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/libcore-5725e7f9b84bd931.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-emscripten/lib/libcompiler_builtins-874d313336916306.rlib" "-l" "tcod" "-l" "tcod" "-l" "c" "-s" "BINARYEN=1" "-s" "ERROR_ON_UNDEFINED_SYMBOLS=1"
  = note: WARNING:root:emcc: cannot find library "tcod"
          WARNING:root:emcc: cannot find library "tcod"
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/rust.metadata.bin is not LLVM bitcode, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json0.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json1.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json10.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json11.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json12.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json13.rcgu.bc.z is not valid according to llvm-nm, cannot link
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_yslnXO_archive_contents/serde_json-f23faf1e93e85e76.serde_json14.rcgu.bc.z is not valid according to llvm-nm, cannot link
...
          WARNING:root:object /private/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/emscripten_temp_6OxUvZ_archive_contents/compiler_builtins-874d313336916306.compiler_builtins9-efdbf1bd2f3b1e3c9741f8dcfa1f7012.rs.rcgu.bc.z is not valid according to llvm-nm, cannot link
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          warning: unexpected number of arguments 2 in call to 'rust_oom', should be 1
          Traceback (most recent call last):
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emcc.py", line 3042, in <module>
              sys.exit(run())
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emcc.py", line 1759, in run
              final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args)
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/tools/shared.py", line 2274, in emscripten
              emscripten._main(cmdline)
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emscripten.py", line 2222, in _main
              return temp_files.run_and_clean(lambda: main(
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/tools/tempfiles.py", line 93, in run_and_clean
              return func()
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emscripten.py", line 2227, in <lambda>
              DEBUG=DEBUG,
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emscripten.py", line 2153, in main
              temp_files=temp_files, DEBUG=DEBUG)
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emscripten.py", line 81, in emscript
              backend_output = compile_js(infile, temp_files, DEBUG)
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/emscripten.py", line 110, in compile_js
              jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE, universal_newlines=True), note_args=backend_args)
            File "/usr/local/Cellar/emscripten/1.38.11/libexec/tools/jsrun.py", line 20, in timeout_run
              raise Exception('Subprocess "' + ' '.join(note_args) + '" failed with exit code ' + str(proc.returncode) + '!')
          Exception: Subprocess "/usr/local/opt/emscripten/libexec/llvm/bin/llc /tmp/tmpXhVI8L/roguelikers.bc -march=js -filetype=asm -o /tmp/tmpGyjkRG.4.js -emscripten-stack-size=5242880 -O0 -emscripten-precise-f32 -emscripten-assertions=1 -emscripten-no-aliasing-function-pointers -emscripten-global-base=1024 -enable-emscripten-cpp-exceptions -emscripten-no-exit-runtime -emscripten-wasm -emscripten-only-wasm" failed with exit code -11!


error: aborting due to previous error

There are a lot of warnings and it looks like it cannot find the tcod library. Is there anything I should do before running cargo build?
Cheers!

0 Likes

#2

Hi, did you find a solution ? :smiley:

0 Likes

#3

Actually no. I suspect I need to find a way to tell emcc that it’s got to take the C sources of libtcod and do something with them but I have no idea how to fit that into Rust support for emscripten :frowning:

0 Likes

#4

It is kinda odd, because I have a similar problem with a different cause that is actually using C libs.
I use https://docs.rs/ntru/0.5.6/ntru/ that wraps a c-lib. I use that crate in my own lib that I can get to build to a .wasm file. However if I want to use my combination lib in another project that references it I get the exact same errors as you do.

0 Likes

#5

We probably need help from someone who knows how the inners of the emscripten integration with Rust work. I even tried compiling libtcod with emscripten so that I could get a .bc file that I could copy somewhere in my Rust project, but it fails finding zlib for some reason that I can’t understand.

0 Likes

#6

I’m digging this post as I did some further tests to find out what’s happening and I believe that emcc is expecting to find a library called tcod somewhere in its ports or already compiled to wasm by something else, while cargo is actually just telling emcc that tcod is already there.
Does this make sense? Is there any way to instruct cargo to compile all of the external C libraries (included by other crates) to wasm before compiling the project itself?

0 Likes