Linker error cross-compilation armv7-unknown-linux-musleabihf

I am trying to cross-compile my project which links to a C library (libmpv). I have created a build.rs file which contains:

fn main() {
    println!("cargo:rustc-link-lib=mpv");
    println!("cargo:rustc-link-search=native=/mnt/data/libmpv_musl");
}

Cargo.toml:

[package]
(snip)
edition = "2018"
build = "build.rs"
links = "mpv"

.cargo/config:

[target.armv7-unknown-linux-musleabihf]
linker = "armv7-linux-musleabihf-gcc"

As you can see I added build = "build.rs" to Cargo.toml, specified the toolchain and linker in .cargo/config and I am building with cargo build --target armv7-unknown-linux-musleabihf. Unfortunately linking fails:

armv7-linux-musleabihf/bin/ld: cannot find -lmpv

The (huge) command emitted by Cargo -vv is:

"armv7-linux-musleabihf-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-nostdlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crt1.o" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crti.o" "-L" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.0.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.1.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.10.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.11.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.12.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.13.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.14.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.15.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.2.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.3.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.4.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.5.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.6.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.7.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.8.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.radio.9wm589jb-cgu.9.rcgu.o" "-o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-d1cd7e791a34b70b.46k6fpm679s3jibv.rcgu.o" "-Wl,--gc-sections" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps" "-L" "/home/roysten/rust/radio2/target/release/deps" "-L" "/mnt/data/libmpv_musl" "-L" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib" "-Wl,-Bstatic" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/liblibc-99f448d743c85844.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libstd-83a4f058944e6814.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libpanic_unwind-e11c7b3b3225afe2.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libbacktrace-13217ede3d276f16.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libbacktrace_sys-621a9ee22da6caa1.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_demangle-546c844e8071bbeb.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libhashbrown-be9569e4d599746f.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_std_workspace_alloc-47d8845cef2a3bc5.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libunwind-017511bce73a530c.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcfg_if-be7979c57a08057b.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/liblibc-d6459c4f0817c67c.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/liballoc-580035dd98451925.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_std_workspace_core-aee5c24fff305dea.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcore-8a55a4098920125a.rlib" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcompiler_builtins-9fc4b5be2ba5cc19.rlib" "-static" "-L/mnt/data/libmpv_musl" "-lmpv" "-Wl,-Bdynamic" "/home/roysten/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crtn.o"

What am I missing here? Of course the library is in the specified directory and compiling equivalent C code with the same toolchain does work.

If I manually put the -lmpv part at the end of the command it does succeed, how can I instruct Cargo to do this for me?

is it a static library ? my guess would be, because rust puts "-static" before the linkage ld will only see static libraries

if it's a dynamic library you could try

println!("cargo:rustc-link-lib=dylib=mpv");

No it is not, good call! Unfortunately this does not fix the issue, the errror remains the same. The command issued by Cargo/rustc is now:

"armv7-linux-musleabihf-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-nostdlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crt1.o" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crti.o" "-L" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.0.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.1.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.10.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.11.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.12.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.13.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.14.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.15.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.2.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.3.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.4.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.5.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.6.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.7.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.8.rcgu.o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.radio.7yvsmmtm-cgu.9.rcgu.o" "-o" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/radio-800c48f55af93eeb.3hcnsw9ffu2armrx.rcgu.o" "-Wl,--gc-sections" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps" "-L" "/home/roysten/rust/radio2/target/release/deps" "-L" "/mnt/data/libmpv_musl" "-L" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib" "-lmpv" "-Wl,-Bstatic" "/home/roysten/rust/radio2/target/armv7-unknown-linux-musleabihf/release/deps/liblibc-dc9740d38cee6736.rlib" "-Wl,--start-group" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libstd-f5d652664a0bf5ba.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libpanic_abort-87dfcb9451517be9.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libbacktrace-d8c262c23f2d9cad.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libbacktrace_sys-b9100f8c0b6a3e92.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_demangle-6291aeedda6b9a3c.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libhashbrown-f962bcf371dacf09.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_std_workspace_alloc-d22b03a4ce9ba2aa.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libunwind-936ce2afb484012d.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcfg_if-547424e2ee5c05ef.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/liblibc-d662aafb3ec805e0.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/liballoc-dc465a39519fee82.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/librustc_std_workspace_core-a56c595193523052.rlib" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcore-1f2211a880097e24.rlib" "-Wl,--end-group" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/libcompiler_builtins-7b7088043fccb32f.rlib" "-static" "-Wl,-Bdynamic" "/home/roysten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-musleabihf/lib/crtn.o"

I made some progress, using:

strace -f cargo build --target armv7-unknown-linux-musleabihf 2>&1 | grep mpv

I found out that Rust lets the linker search for a static library instead of a dynamic library, regardless of what type I provided in build.rs for the value cargo:rustc-link-lib. I read somewhere that the musl targets are commonly used for static linking, but I wouldn't mind dynamically linking to musl libc.

How can I force dynamic linking?

Edit: Found it! You need to add the following to your RUSTFLAGS:

-C target-feature=-crt-static

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.