Hi!
I've got a linker related problem. I'd like to create Rust bindings for a C program so people can write modules in Rust for it. A module is just an .so file. I need to link against a static library. It declares the function rust_parser_proxy_new()
and my crate defines it. The problem is, that after the .so
file is produced by cargo I see the rust_parser_proxy_new()
function both as undefined and in the .text section. I'd like to see it only in the .text section.
I reproduced this problem with a very simple library which can be found here: https://github.com/ihrwein/link-repro
Here, the liba.a
static library defines the foo()
function and declares the bar()
function. I link against this library and my crate defines the bar()
function. If I run cargo build
and check the .so
file I see the bar
symbol both as undefined and in the .text
section. I think this is because the -l
parameters are after the object file produced by rustc. This is good for almost every case but not for my one. Do you know a solution for this problem?
$ rustc src/lib.rs --crate-name link_repro --crate-type dylib -g --out-dir /home/tibi/workspace/link-repro/target/debug --emit=dep-info,link -L dependency=/home/tibi/workspace/link-repro/target/debug -L dependency=/home/tibi/workspace/link-repro/target/debug/deps -L native=/home/tibi/workspace/link-repro -l static=a -Z print-link-args
src/lib.rs:2:1: 5:2 warning: function bar is marked #[no_mangle], but not exported, #[warn(private_no_mangle_fns)] on by default
src/lib.rs:2 extern fn bar() -> i32 {
src/lib.rs:3 println!("bar");
src/lib.rs:4 1 as i32
src/lib.rs:5 }
src/lib.rs:2:1: 5:2 warning: function is never used: `bar`, #[warn(dead_code)] on by default
src/lib.rs:2 extern fn bar() -> i32 {
src/lib.rs:3 println!("bar");
src/lib.rs:4 1 as i32
src/lib.rs:5 }
"cc" "-Wl,--as-needed" "-m64" "-L" "/home/tibi/.multirust/toolchains/stable/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/tibi/workspace/link-repro/target/debug/link_repro.0.o" "-o" "/home/tibi/workspace/link-repro/target/debug/liblink_repro.so" "-Wl,--whole-archive" "-l" "morestack" "-Wl,--no-whole-archive" "/home/tibi/workspace/link-repro/target/debug/link_repro.metadata.o" "-nodefaultlibs" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/libstd-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/libcollections-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/librustc_unicode-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/librand-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/liballoc-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/liblibc-198068b3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustc.Kar3vNl88nIY/libcore-198068b3.rlib" "-Wl,--no-whole-archive" "-L" "/home/tibi/workspace/link-repro/target/debug" "-L" "/home/tibi/workspace/link-repro/target/debug/deps" "-L" "/home/tibi/workspace/link-repro" "-L" "/home/tibi/.multirust/toolchains/stable/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "/home/tibi/workspace/link-repro/.rust/lib/x86_64-unknown-linux-gnu" "-L" "/home/tibi/workspace/link-repro/lib/x86_64-unknown-linux-gnu" "-Wl,-Bstatic" "-Wl,--whole-archive" "-l" "a" "-Wl,--no-whole-archive" "-Wl,-Bdynamic" "-l" "dl" "-l" "pthread" "-l" "rt" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-shared" "-Wl,-rpath,$ORIGIN/../../../../.multirust/toolchains/stable/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-rpath,/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-l" "compiler-rt"