Build --target x86_64-unknown-linux-musl / undefined reference to symbol 'shmget@@GLIBC_2.2.5'

I am trying to build a self contained app that has dependencies on crates/libxml and crate/xmlsec. These libraries are not included in the base docker image ekidd/rust-musl-builder, so I built them from source following the suggestions in the documentation. I have included my new docker file in my example repo: https://github.com/JoshuaNitschke/rust-musl-builder_example

When I try to build, even though I compiled from source, it fails to finish building with the below error. I'm not sure what to try next? Anyone have any tips? I have tried searching the forums and seen similar topics but haven't found anything that seemed useful.

rust@74c30da02b56:~/src$ cargo build --release --target x86_64-unknown-linux-musl
   Compiling rust-musl-builder v0.1.0 (/home/rust/src)
WARN rustc_codegen_ssa::back::link Linker does not support -static-pie command line option. Retrying with -static instead.
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crt1.o" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/opt/rust/rustup/toolchains/stable-x8
6_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbegin.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.0.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0a
bdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.1.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.10.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0f
a1f4e9-cgu.11.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.12.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.2.rcgu.o" "/home/rust/s
rc/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.3.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.4.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/
release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.5.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.6.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abda
b2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.7.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4e9-cgu.8.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.rust_musl_builder.0fa1f4
e9-cgu.9.rcgu.o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2.21tfntb70993l72k.rcgu.o" "-Wl,--as-needed" "-L" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps" "-L" "/home/rust/src/target/release/deps" "-L" "/usr/local/musl/lib" "-L" "/usr/local/m
usl/lib" "-L" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/libxmlsec-bd1b363e63662ad8.rlib" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/liblazy_static-c03c33d
691abf67a.rlib" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/liblibxml-e0f9293446cc9aa8.rlib" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/liblibc-70ba889effc8716d.rlib" "-Wl,--start-group" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-
linux-musl/lib/libstd-bd288633937fc261.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-d5ca8f542ed71803.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-3f3a3c0
6ab303b52.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-34b784c556a5ac17.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-30156cf7870ed168.rlib" "/opt/rust/rustup/toolcha
ins/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libmemchr-42ddd1c995a4526e.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-27e425dff83cc7fe.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/l
ib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-27953ea6f34f06aa.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-8140e89a3473e3c6.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/
lib/librustc_demangle-dc264158c76b38a0.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-6499d2e635982e2a.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_al
loc-8221281351bf4b54.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-55df4fa4e20c6efc.rlib" "-lunwind" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-9ce06dc879286971.rlib" "/
opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-c097b71320db773c.rlib" "-lc" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-37d12500bef99331.rlib" "/opt/rust/rustup/toolchains/stable-x86
_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-63d6ff8598946320.rlib" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-972b93109ce1fa5e.rlib" "-Wl,--end-group" "/opt/rust/rustup/toolchains/stable-x86_64-un
known-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-71972cde1db06d51.rlib" "-Wl,-Bdynamic" "-lssl" "-lxmlsec1-openssl" "-lxmlsec1" "-lcrypto" "-ldl" "-lxml2" "-lm" "-lxml2" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-nostartfiles" "-L" "/opt/rust/rustup/toolchains/stable-x86_64-unknown
-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "-o" "/home/rust/src/target/x86_64-unknown-linux-musl/release/deps/rust_musl_builder-0abdab2629dfeaa2" "-Wl,--gc-sections" "-static" "
-Wl,-zrelro,-znow" "-Wl,-O1" "-nodefaultlibs" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtend.o" "/opt/rust/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: /usr/bin/ld: /usr/local/musl/lib/libcrypto.a(rand_unix.o): undefined reference to symbol 'shmget@@GLIBC_2.2.5'
          //lib/x86_64-linux-gnu/libc.so.6: error adding symbols: DSO missing from command line
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = 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#cargorustc-link-libkindname)

error: could not compile `rust-musl-builder` due to previous error

The C dependencies have used GNU-specific C stdlib headers from your host operating system. You need to ensure that your C compiler is also a cross-compiler that won't look at your /usr/include or its own GNU headers.

It's PITA. It's probably easiest to avoid cross-compilation, and compile on a MUSL-based Linux distro.

After encountering cross-compilation problems many times, I use docker to build multiple compilation environments locally, which is very fast and convenient.

Thanks for the tips. After I posted this, I started trying to build it in alpine, so this sounds like I'm on the right track. I had tried to build on the target OS (CentOS7) but was having trouble with headers lining up for xmlsec

I got it to build using alpine! Thanks for the tip.