rust-ctor: ctor not running for statically linked libraries
Hi,
I have an issue with the following setup:
- app crate defines the binary to produce
- lib crate defines a rust standard library
When using #[ctor] attribute inside the crate lib, it is not called when running the binary built with app.
Using a rust library dependency statically link it so it should also include the ctor function, but it does not seems to be the case.
Am I missing something or is this an unsupported use case?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 15 (8 by maintainers)
https://github.com/rust-lang/rust/issues/47384
I can reproduce this on macOS Mojave and Rust 1.39.0 as well. However, it seems that I am able to get the constructor to run at least with the following minimal application:
The output produced by this application is:
If I comment out the
unused()call inmain.rs, the application now produces the following output instead:I was unable to get the destructor working with the
#[dtor]macro, but if you replace the definition of thefoo::on_shutdown()function with this instead:And then add the following call to
libc::atexit()to thefoo::on_startup()constructor:The application now works as expected:
In short, it seems that a few tweaks to the way your application is written will get the constructor and destructor to run:
main.rsmust call at least one function or inherent struct method fromlib.rsfor#[ctor]to register properly. Importing static and const values in themain.rsdoesn’t seem to help.#[dtor]doesn’t seem to work at all. Register it manually withlibc::atexit()in your#[ctor]function.I’m not sure what is going on, but I am also leaning towards the possibility of an issue with Rust or LLVM.