How to use `RUSTFLAGS` environment variable to link to library?

Hey,

I'm trying to build a static binary on an Alpine docker container. I use the environment variable RUSTFLAGS to set -C target-feature=+crt-static and build by cargo build --release --no-default-features --features with-dns-sd --target x86_64-unknown-linux-musl but my build failed with

...

-Wl,--gc-sections" "-static-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
#9 1223.3   = note: /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libdns_sd.a(libdns_sd_la-compat.o): in function `query_resolver_callback':
#9 1223.3           compat.c:(.text+0x24c): undefined reference to `avahi_client_errno'
#9 1223.3           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libdns_sd.a(libdns_sd_la-compat.o): in function `reg_create_service':
#9 1223.3           compat.c:(.text+0x3f6): undefined reference to `avahi_entry_group_add_service_strlst'
#9 1223.3           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: compat.c:(.text+0x43e): undefined reference to `avahi_entry_group_add_service_subtype'
#9 1223.3           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: compat.c:(.text+0x450): undefined reference to `avahi_entry_group_commit'
#9 1223.3           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libdns_sd.a(libdns_sd_la-compat.o): in function `thread_func':
#9 1223.3           compat.c:(.text+0x575): undefined reference to `avahi_simple_poll_run'

...

#9 1223.3           collect2: error: ld returned 1 exit status
#9 1223.3           
#9 1223.3   = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
#9 1223.3   = note: use the `-l` flag to specify native libraries to link
#9 1223.3   = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

I understand that the linker failed to use/locate /usr/lib/libdns_sd.a. I tried various combinations of

"-C target-feature=+crt-static -C default-linker-libraries=yes"
"-C target-feature=+crt-static -l /usr/lib/libdns_sd.a"
"-C target-feature=+crt-static -L /usr/lib/"
"-C target-feature=+crt-static -C link-args=-Wl,-rpath,/usr/lib/

But none worked.

Within the source, an extern crate dns_sd is used which uses pkg_config::find_library("avahi-compat-libdns_sd").unwrap(); to locate the library.

Any ideas how to get the build working?
Thanks.

Alpine only ships libavahi as dynamic library, so there is nothing to statically link against.

Where did you get /usr/lib/libdns_sd.a from? The avahi-compat-libdns_sd package also only contains a dynamic library.

It's part of avahi-dev

https://pkgs.alpinelinux.org/contents?file=libdns_sd.a&path=&name=&branch=v3.19

I think libdns_sd.a depends on libavahi-common.a and libavahi-client.a: avahi/avahi-compat-libdns_sd/Makefile.am at 219a9c2459ac4e2aa29291ff6712248d48234b57 · avahi/avahi · GitHub Try linking against those too.

I tried to include it, but I think my syntax is wrong.

RUSTFLAGS="-C target-feature=+crt-static -l /usr/lib/libdns_sd.a -l /usr/lib/libavahi-client.a -l /usr/lib/libavahi-common.a"

produced

649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libdns_sd.a: No such file or directory
649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libavahi-client.a: No such file or directory
649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libavahi-common.a: No such file or directory
649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libdns_sd.a: No such file or directory
649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libavahi-client.a: No such file or directory
649.0           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -l/usr/lib/libavahi-common.a: No such file or directory
649.0           collect2: error: ld returned 1 exit status
649.0           
649.0 
649.1 error: could not compile `librespot` (bin "librespot") due to 1 previous error
------

I got it working with

RUSTFLAGS="-C target-feature=+crt-static -C link-arg=-L/usr/lib/ -C link-arg=-l:libdns_sd.a -C link-arg=-l:libavahi-client.a -C link-arg=-l:libavahi-common.a -C link-arg=-l:libdbus-1.a -C link-arg=-l:libintl.a"

It now compiles with out issue, but running the binary causes a segmentation fault....