Hello everyone, I'm having a compilation linking issue and I don't understand where the problem is. I hope someone can help me take a look.
It might sound a bit strange, but I want to build a Rust dynamic library (dylib, not cdylib) that depends on a static library, specifically raylib
on macOS arm. I've specified it like this:
#[link(name = "raylib", kind = "static")]
unsafe extern "C" {
// functions
}
However, this doesn't seem to work. The otool
tool indicates that my dynamic library is not actually depending on the static library, but instead wants to depend on the dynamic version of the library.
dlopen(/Users/kevinstephen/.local/share/ksl/lib/libksl_raylib.dylib, 0x0005): Library not loaded: @rpath/libraylib.550.dylib
Referenced from: <822DA870-8970-38FA-9772-20A658BA7C43> /Users/kevinstephen/projects/rswk/target/debug/libksl_raylib.dylib
Reason: no LC_RPATH's found
The outputs of cargo build --verbose
and otool -L <lib>
are as follows:
/Users/kevinstephen/.local/sdk/rustup/toolchains/nightly-aarch64-apple-darwin/bin/rustc --crate-name ksl_raylib --edition=2024 ksl_raylib/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=196 --crate-type lib --crate-type dylib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=0022d810958125c8 --out-dir /Users/kevinstephen/projects/rswk/target/debug/deps -C incremental=/Users/kevinstephen/projects/rswk/target/debug/incremental -L dependency=/Users/kevinstephen/projects/rswk/target/debug/deps --extern ksl=/Users/kevinstephen/projects/rswk/target/debug/deps/libksl.rlib --extern ksl=/Users/kevinstephen/projects/rswk/target/debug/deps/libksl.dylib -L /Users/kevinstephen/projects/rswk/ksl_raylib/extern/raylib-5.5_macos/lib -L native=/usr/local/lib -l static=raylib -l framework=OpenGL -l framework=Cocoa -l framework=IOKit -l framework=CoreFoundation -l framework=CoreVideo
../target/debug/libksl_raylib.dylib:
/Users/kevinstephen/projects/rswk/target/debug/deps/libksl_raylib.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libraylib.550.dylib (compatibility version 550.0.0, current version 5.5.0)
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 24.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 3423.0.0)
/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
The build.rs
:
fn main() {
let Ok(pkg_root) = std::env::var("CARGO_MANIFEST_DIR").map(|e| std::path::PathBuf::from(e)) else {
unreachable!()
};
println!(
"cargo::rustc-link-search={}/extern/raylib-5.5_{}/lib",
pkg_root.display(),
if cfg!(target_os = "windows") {
"win64_mingw-w64"
} else if cfg!(target_os = "macos") {
"macos"
} else {
"linux_amd64"
}
);
println!("cargo:rustc-link-lib=static=raylib");
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=dylib=winmm");
println!("cargo:rustc-link-lib=dylib=gdi32");
println!("cargo:rustc-link-lib=dylib=user32");
println!("cargo:rustc-link-lib=dylib=shell32");
} else if cfg!(target_os = "macos") {
println!("cargo:rustc-link-search=native=/usr/local/lib");
println!("cargo:rustc-link-lib=framework=OpenGL");
println!("cargo:rustc-link-lib=framework=Cocoa");
println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=CoreVideo");
} else {
println!("cargo:rustc-link-search=/usr/local/lib");
println!("cargo:rustc-link-lib=wayland-client");
println!("cargo:rustc-link-lib=glfw");
}
}
Can someone please tell me why this is happening and what the solution is?