-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Linking a static C library does not include all symbols #78827
Comments
@Firstyear can you confirm whether this worked in Rust 1.0 (or any later release)? 1ae1461 is present in every stable release, it would be helpful to know when this regressed. |
I can probably make a smaller reproducer and do a rough bisect of some older releases, but I honestly only just found this problem the other day :( |
@jyn514 I've just checked back through to 1.34.0-x86_64-unknown-linux-gnu, where releases prior can't be used without lowering the version of cc. Every version from 1.34 to nightly exhibits this issue with the example repository. I have pushed an update that simplifies the example code so that anyone should be able to reproduce it. |
@Firstyear I don't understand how to run your example. Do I need R installed or something? |
Sorry to make it a bit clearer.
The function wrapper_add as defined https://github.com/Firstyear/demo_r_link/blob/main/src/wrapper.c which is built with https://github.com/Firstyear/demo_r_link/blob/main/build.rs should be present in the resulting .so, but it is not. |
You are not linking your C library anywhere so you cannot expect it be included. Re-exporting C symbols in cdylibs currently doesn't work: rust-lang/rfcs#2771. If that's what you are trying to achieve this issue can be closed as a duplicate. |
The cc crate emits the metadata to link the C lib into the output. This is not a duplicate as it is the C symbols that are not available after compilation. |
Rust won't link that C library if it doesn't find anything useful there and your code doesn't pull any symbol. Re-exporting them is more complicated though, you'd have to do something like this: #62287 |
This is still not sufficient. I have updated the example such that rust has a call into the C component now. From the symbol outputs:
You can find the updates in https://github.com/Firstyear/demo_r_link/tree/reverse-call . In this lib.rs calls from f64mul to reverse_call in wrapper.c, which does correctly compile in. However, the symble for "wrapper_add" that calls back to f64sum is still not present. This indicates that wrapper.c is compiled and linked, but not all symbols are included. |
It does import the symbol but it's still not exported:
AFAIK this is expected because you do not import |
What would be the way to fix the provided example code so that it does work in the way that I expect it to then? |
Assigning |
For re-exporting somebody has to fix rust-lang/rfcs#2771. |
I think it's that a symbol defined in my code's .c files is not exported which seems like a surprising behaviour. Especially since calling wrapper_add would be recursive, and the point is that wrapped_add should be a landing point for incoming calls from C applications. The fact that a linked library in my own source tree has it's symbols not exported is a very surprising (and incorrect) behaviour IMO. |
This relates to #15460. On 1.47 stable and 1.48 nightly, when using cc/cbindgen, if a symbol in the C file is not used by the .rs file, then it is not in the resulting cdylib. It is possible to force the symbol to be included by "calling" it as show in #19321 .
The linked repository is able to reproduce this. Testing on OpenSUSE tumbleweed (fully patched with gcc10). After
cargo build
then the resulting libdemo.so does not contain the symbolwrapper_add
.This appears to be a regression since #15460 which was intended to resolve this behaviour. I appreciate your advice on this, thanks,
https://github.com/Firstyear/demo_r_link
EDIT: Worth noting in wrapper.c that it does not need to link to any R libs, and that SEXP is a typdef to
typedef struct SEXPREC *SEXP;
The text was updated successfully, but these errors were encountered: