I'm having problems creating a rust library that can be called from static musl-gcc. Any help would be appreciated!
Cargo.toml
[package]
name = "c-example"
version = "0.1.0"
edition = "2024"
[dependencies]
[profile.release]
panic = "abort"
lto = true
[lib]
crate-type = ["staticlib"]
src/lib.rs
#[repr(C)]
pub struct Example {
pub id: u32,
pub value: u64,
}
#[unsafe(no_mangle)]
pub extern "C" fn example_new(id: u32, value: u64) -> *mut Example {
let obj = Example { id, value };
Box::into_raw(Box::new(obj))
}
test.c
#include <stdio.h>
#include <stdlib.h>
extern void *example_new(unsigned int id, unsigned long value);
int main() {
void *obj = example_new(42, 123456789);
if (!obj) {
printf("Failed to create struct\n");
return 1;
}
}
.cargo/config.toml
cat .cargo/config.toml
[build]
rustflags = ["-C", "target-feature=+crt-static"]
target = "x86_64-unknown-linux-musl"
$ cargo build --release --target x86_64-unknown-linux-musl
$ musl-gcc -static -o test test.c target/x86_64-unknown-linux-musl/release/c_example.a
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x2941): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status
For information:
$ rustc -V
rustc 1.87.0-nightly (e16a049ad 2025-03-03)
$ musl-gcc -v
Using built-in specs.
Reading specs from /usr/lib/musl/lib/musl-gcc.specs
rename spec cpp_options to old_cpp_options
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.1 20250207 (GCC)