You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Linking a static native library to a dynamic rust library doesn't necessarily actually link #15460
$ gcc -c foo.c
$ ar crus libfoo.a foo.o
$ rustc foo.rs -L.
$ rustc bar.rs -L.
error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-o' 'bar' 'bar.o' '-Wl,-force_load,/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a' '-nodefaultlibs' '-Wl,-dead_strip' '-L' '/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-lnative-1fb5e2c0-0.11.0-pre' '-L' '/Users/acrichton/tmp' '-lfoo-aab0fa9e-0.0' '-L' '/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-lstd-59beb4f7-0.11.0-pre' '-L' '/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-lsync-305341d2-0.11.0-pre' '-L' '/Users/acrichton/code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-lrustrt-d8560cb2-0.11.0-pre' '-L' '.' '-L' '/Users/acrichton/tmp/.rust' '-L' '/Users/acrichton/tmp' '-L' '/Users/acrichton/.rust' '-lSystem' '-lpthread' '-lc' '-lm' '-Wl,-rpath,@loader_path/../code/rust/lib/rustlib/x86_64-apple-darwin/lib' '-Wl,-rpath,@loader_path/.' '-Wl,-rpath,/usr/local/lib/rustlib/x86_64-apple-darwin/lib' '-lcompiler-rt'
note: ld: warning: directory not found for option '-L/Users/acrichton/tmp/.rust'
Undefined symbols for architecture x86_64:
"_foo", referenced from:
main::hdcb342faf114af9efaa::v0.0 in bar.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: aborting due to previous error
The problem is that if the dynamic library doesn't actually reference any symbols from the native library then the native library is stripped from the command line.
Possible solutions
Stop using -Wl,--as-needed
Pass -Wl,--whole-archive for immediately dependent static libraries
For all extern blocks with a #[link(kind = "static")], generate a reference to all symbols in the extern block to force the library to be linked. (in some sort of 0-cost manner)
changed the title [-]Linking a static native library to a dynamic rust library doesn't necessary actually link[/-][+]Linking a static native library to a dynamic rust library doesn't necessarily actually link[/+]on Jul 5, 2014
Using cargo build then inspecting the resulting .so, "wrapper_add" is not present. Using the linking trick from @steveklabnik's closed issue can force symbols to be included from the static archive. This is using cc and cbindgen, so this "should work". Any advice would be most appreciated.
@Firstyear in general, especially with a bug this old, it's better to open a new issue, and mention this one in it. A lot has changed since 2014 and most of the details here won't be relevant.
Activity
[-]Linking a static native library to a dynamic rust library doesn't necessary actually link[/-][+]Linking a static native library to a dynamic rust library doesn't necessarily actually link[/+]o11c commentedon Jul 8, 2014
Should retitle to indicate that it's not just about static native libraries.
alexcrichton commentedon Jul 8, 2014
In your test case you are using
#[link_args]which is a feature-gated and unsupported way to link to dynamic libraries.This is not a problem using dynamic libraries through the
#[link]attribute.o11c commentedon Jul 8, 2014
Given that
#[link]is fundamentally wrong in nearly all cases,#[link_args]will have to be blessed sooner or later.rustc: Link entire archives of native libraries
Firstyear commentedon Nov 7, 2020
Hi there, sorry to re-open this many years after, but I can reproduce this on 1.47 stable and 1.48 nightly with the repository found here: https://github.com/Firstyear/demo_r_link
Using cargo build then inspecting the resulting .so, "wrapper_add" is not present. Using the linking trick from @steveklabnik's closed issue can force symbols to be included from the static archive. This is using cc and cbindgen, so this "should work". Any advice would be most appreciated.
steveklabnik commentedon Nov 7, 2020
@Firstyear in general, especially with a bug this old, it's better to open a new issue, and mention this one in it. A lot has changed since 2014 and most of the details here won't be relevant.