zig
zig copied to clipboard
Cross compiling a library from Rust fails
Zig Version
0.9.0-dev.1920+de81c504b
Steps to Reproduce
When trying to cross compile the Wasmer C API to different systems, the linker outputs a unexpected error.
git clone [email protected]:wasmerio/wasmer.git
cd wasmer
rustup target add x86_64-pc-windows-gnu
cargo build --manifest-path lib/c-api/Cargo.toml --release --no-default-features --features staticlib,wasi --target=x86_64-pc-windows-gnu
Note: you will need to modify the .cargo/config.toml
file with:
[target.x86_64-pc-windows-gnu]
linker = "THE_PATH_TO/zig-cc"
With zig-cc
being:
#!/bin/sh
zig cc $@
Expected Behavior
That is able to compile
Actual Behavior
It fails with:
= note: "/Users/syrusakbary/Downloads/zig-macos-aarch64-0.9.0-dev.1920+de81c504b/zig-cc" "-Wl,/var/folders/f8/jp6yy7c10d1bjlvlstsznqlh0000gn/T/rustcAnuQ55/list.def" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/rsbegin.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.0.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.1.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.10.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.11.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.12.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.13.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.14.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.15.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.2.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.3.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.4.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.5.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.6.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.7.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.8.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.wasmer_wasi.25dcb9ee-cgu.9.rcgu.o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.14gh0jf4galpv8rr.rcgu.o" "-L" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps" "-L" "/Users/syrusakbary/Development/wasmer/target/release/deps" "-L" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/build/wasmer-vm-2a3effa2416b5660/out" "-L" "/Users/syrusakbary/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-x86_64-pc-windows-gnu-0.4.0/lib" "-L" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib" "-Wl,-Bstatic" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_wasi_types-4a8aafb3f53294df.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libtime-a7c985db0087b404.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libtime_macros-b3301e3381a5a462.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libstandback-73a1520d1e16cf33.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libgenerational_arena-7a86b382f767ba95.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libcfg_if-2e375d076677be01.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_vfs-fe12d11410227612.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libtracing-07e4b9dd0bce484e.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libpin_project_lite-d6ee2fb4613cbcb0.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libtracing_core-27aa365f746ff4ec.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer-11898c582d79d0cc.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_engine-cf35994a2ed6c30b.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/liblazy_static-034e555b14873731.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libmemmap2-947a97e9e9cd69d6.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_compiler-1c86864dd37c7df7.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libserde_bytes-83e27193d8fd0f0e.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libsmallvec-e717e71bf849c9a4.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libtarget_lexicon-143a929cad875e4c.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_vm-4c8c65b894b0b5f3.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libregion-e7b77779eab701fb.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libbitflags-77deefdcac6b6671.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libbacktrace-156c958c49a497bc.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libobject-b74caf5bc7d63d39.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libcrc32fast-8d7a00289111346d.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libmemchr-310902874e86d301.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/liblibc-f538534eaaf32b8f.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libaddr2line-c2b0032bd60c4da4.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libgimli-e57b8184cca14a95.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/librustc_demangle-a14a091d666fa24f.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwinapi-003de942151e676b.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libmore_asserts-cebb09e43905ce88.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libmemoffset-3af371cad7067e32.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmparser-73facc5e40c58339.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_types-af5a163de6c7424c.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/librkyv-50a17642798e3fb5.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/librend-86806a178e3c7ab3.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libseahash-b0de8297b669fbb2.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libptr_meta-ae45499a374c2db9.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libenumset-ffd59a1ac611be25.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libloupe-2ab66e8b5b4ac581.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libindexmap-769422b836a73e15.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libhashbrown-5770a982e6580bbf.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libahash-35b9e99bfeba3396.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libgetrandom-bb197c97e4ee2f72.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libcfg_if-e8dedcad5699af89.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libonce_cell-ae038c2366c0bee0.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libserde-2c3e3e9f24f79067.rlib" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libthiserror-302b498505c6377d.rlib" "-Wl,--start-group" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libstd-c949a5d9f8ea17eb.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libpanic_unwind-4efbd26dc95f473b.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libobject-11df657f271ec0b5.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libmemchr-ed6e817a89285a21.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libaddr2line-a16c4de5bb48a9a6.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libgimli-b5d4a31ea91f9e86.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libstd_detect-01e22831e506e0c6.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/librustc_demangle-85865d75ad690c5e.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libhashbrown-91e2696c8ccb4461.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/librustc_std_workspace_alloc-186512436a4043bc.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libunwind-d0f4aee65f54d86f.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libcfg_if-42335e6cb2553e87.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/liblibc-662baf24e2595e01.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/liballoc-131219d6deabaad8.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/librustc_std_workspace_core-6530d4af942dc9d4.rlib" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libcore-d1ec034e2d5fe483.rlib" "-Wl,--end-group" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libcompiler_builtins-885492184211f20a.rlib" "-Wl,-Bdynamic" "-lwinapi_advapi32" "-lwinapi_cfgmgr32" "-lwinapi_kernel32" "-lwinapi_user32" "-lbcrypt" "-lkernel32" "-lws2_32" "-ladvapi32" "-luserenv" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib" "-o" "/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/wasmer_wasi.dll" "-Wl,--gc-sections" "-shared" "-Wl,--out-implib=/Users/syrusakbary/Development/wasmer/target/x86_64-pc-windows-gnu/release/deps/libwasmer_wasi.dll.a" "-Wl,-O1" "-nodefaultlibs" "/Users/syrusakbary/.rustup/toolchains/1.56-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/rsend.o"
= note: warning: unsupported linker arg: /var/folders/f8/jp6yy7c10d1bjlvlstsznqlh0000gn/T/rustcAnuQ55/list.def
warning: unsupported linker arg: --disable-auto-image-base
error: the argument -femit-implib is allowed only when building a Windows DLL
error: could not compile `wasmer-wasi` due to previous error
Can you please try to specify the target also inside the zig-cc
script? The error seems to imply that zig cc
is not aware that you're targeting windows.
Also if I understand correctly the first line of your error seems to imply that zig cc
doesn't receive any target information from Rust. I'm not an expert when it comes to Rust specifically, but I know for a fact that you do need to specify in the shell script the target when trying to do the same with Go.
Also make sure to use triplets that Zig can understand:
#!/bin/sh
zig cc $@ -target x86_64-windows-gnu
Edit: also check out this link for a Rust example https://actually.fyi/posts/zig-makes-rust-cross-compilation-just-work/
I'm getting a ~~same~~ different error with -target x86_64-windows-gnu
.
= note: warning: unsupported linker arg: /var/folders/0r/4fqyt_zs2tj4zvglsb7f4zjw0000gn/T/rustc48TRw1/list.def
warning: unsupported linker arg: --disable-auto-image-base
error(link): DLL import library for -lgcc_eh not found
error: DllImportLibraryNotFound
Hey @messense, great work on cargo zigbuild
... I think it's awesome. I'm pending to try it to see if that will solve my original issue (at least it will make things much more ergonomic regarding cross-compilation!)
https://github.com/messense/cargo-zigbuild
For anyone who is tracking this it still happens with version 0.10.0-dev.3880+e2bb92b2e
. Dependencies build fine then during the last stage I get
= note: warning: unsupported linker arg: /tmp/rustco8YO4X/list.def
warning: unsupported linker arg: --disable-auto-image-base
error: library 'libpthread.a' not found
using a cc
script that looks like:
#!/bin/sh
exec zig cc -target x86_64-windows-gnu $@
For anyone who is tracking this it still happens with version
0.10.0-dev.3880+e2bb92b2e
. Dependencies build fine then during the last stage I get= note: warning: unsupported linker arg: /tmp/rustco8YO4X/list.def warning: unsupported linker arg: --disable-auto-image-base error: library 'libpthread.a' not found
using a
cc
script that looks like:#!/bin/sh exec zig cc -target x86_64-windows-gnu $@
@Jeffrey-P-McAteer ,
Zig does not include winpthreads
.
See: #10989