-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
link: failed to static link to c++ library when global variable is used #36710
Comments
You are required to define two functions and one symbol in order to use global objects in your C++ files: As an aside, that C++ snippet probably does not do what you expect it to do. |
I don't get it, does it mean I can't static link to libraries that is written in C++ and uses global variables? Or is there an extra library that I should link to? This is just a simple example to reproduce the failure. And it actually works. |
Not without extra effort on your part, no. Remember that you’re mixing the GNU C runtime (which defines the __dso_handle in an object file somewhere at
Anything related to linking more libraries would likely not work as it would introduce symbol conflict between runtimes. Instead, either build your C++ object targetting MUSL, or use the |
Ah yes indeed, if |
From what I can tell, this isn't a bug in rustc. Closing. |
After keeping my CPU warm for a while by compiling:
I now have in my hands a musl-compatible
I am able to create a static binary containing Rust and C++ code (in my case I was trying to compile the example code from https://github.com/tupshin/cassandra-rs which uses a C++ driver). So, this is possible 🎉 🎈! Now, not everyone is going to go to the trouble I did above, but there are good use cases for static binaries (for one, they actually run everywhere, unlike "mostly static" binaries that are still glibc version dependent), and it would be nice to be able to modify the "early objects" and "late objects" lists that get passed to the linker.
|
@bossmc is it possible you could share this modified rustc? I am currently working on a cross compiling toolchain and I have the crt object files you mention... but of course rust fails on a C++ library. |
Not from this PC, but the only change I made (I think 🤞) was to modify https://github.com/rust-lang/rust/blob/master/src/librustc_back/target/linux_musl_base.rs#L59-L61 to add the There are a few alternatives I've considered since:
@alexcrichton - I'd quite like to help resolve this, who should I be working with/talking to? Do we need an RFC? |
@bossmc er sorry this is pretty out of cache for me, but if you've got some linking questions or whatnot feel free to hit me up on IRC! |
Fair enough, you were the last name I recognised as a core dev to comment. I'll shout in IRC at some point when I've got some time. |
For whom it may concern, I used the last suggestion @bossmc of creating a custom linker script. After some troubleshooting, this is what I came up with that works: #!/bin/bash
args=()
for arg in "$@"; do
if [[ $arg = *"Bdynamic"* ]]; then
args+=() # we do not want this arg
elif [[ $arg = *"crti.o"* ]]; then
args+=("$arg" "/usr/arm-unknown-linux-musleabi/lib/gcc/arm-unknown-linux-musleabi/6.3.0/crtbeginT.o" "-Bstatic")
elif [[ $arg = *"crtn.o"* ]]; then
args+=("-lgcc" "-lgcc_eh" "-lc" "/usr/arm-unknown-linux-musleabi/lib/gcc/arm-unknown-linux-musleabi/6.3.0/crtend.o" "$arg")
else
args+=("$arg")
fi
done
#echo "RUNNING WITH ARGS: ${args[@]}"
arm-unknown-linux-musleabi-real-g++ "${args[@]}" The first if removes Bdynamic args, which is for some reason still included in static linking. Then, it adds It might be a bit sloppy, but that is what worked for me. |
Wild speculation, but were the missing symbols related to You'll need libc since libc++ depends on it, but you shouldn't (need to) be dependent on any gcc libraries. You could also link directly to |
Actually, the missing symbols were like |
Did you add Yeah, I know the feeling about the multiple hour wait for bootstrap compilations 😀... |
hmm... that the library you proposed does look like a good alternative especially considering that that was basically all I was missing after fixing crtbegin/crtend issues. I think I will give it a try within the next weeks--its a great suggestion. |
This fixes rust-lang#36710 with -crt-static.
Reopening as per #36710 (comment) (feel free to ping me or another team member if bugs need to be reopened!) |
This seems to work fine now, as long as |
@jonas-schievink I think that means that a self-contained link will still not work for a C++ binary (even if the C++ bit is built with musl libc)? Is that just inevitable? |
For now, yes, that still won't work. |
CRT objects are not guaranteed to be compatible between different compilers, so the answer is yes. |
…, r=Mark-Simulacrum Unignore test for rust-lang#36710 on MUSL This now works fine thanks to autodetected `-C link-self-contained`. Closes rust-lang#36710
…, r=Mark-Simulacrum Unignore test for rust-lang#36710 on MUSL This now works fine thanks to autodetected `-C link-self-contained`. Closes rust-lang#36710
…, r=Mark-Simulacrum Unignore test for rust-lang#36710 on MUSL This now works fine thanks to autodetected `-C link-self-contained`. Closes rust-lang#36710
C++ requires additional runtime to be linked by rust linker. Reference: rust-lang/rust#36710 (comment)
C++ requires additional runtime to be linked by rust linker. Reference: rust-lang/rust#36710 (comment)
Hi, I am trying to build a binary written in rust static linked with a c++ library. I follow the instructions in official documentation, but end up with an error:
Following is a simple example:
I build the static library using following command:
And build the binary using following command:
It failed with error:
I can build the binary successfully when linking dynamically. Am I missing any thing here? Any help is appreciated.
The text was updated successfully, but these errors were encountered: