Extern static in dll has incorrect address

⚓ rust    📅 2025-06-08    👤 surdeus    👁️ 3      

surdeus

I have two rust crates, one of which is a cdylib crate and the other has a load-time dependency on the cdylib. I've defined a #[no_mangle] static in the cdylib, and when i try to access it from the main crate, the address of the static is incorrect, so i can't use the static at all.

Main crate

unsafe extern "C" {
	pub safe fn print_static();
	pub safe static VALUE: i32;
}

fn main() {
	print_static();
	println!("{:?}", &VALUE as *const _);
	println!("{:?}", VALUE);
}

dylib crate

#[unsafe(no_mangle)]
static VALUE : i32 = 25;

#[unsafe(no_mangle)]
pub extern "C" fn print_static() {
	println!("{:?}", &VALUE as *const _);
	println!("{:?}", VALUE);
}

when i print the address and value of the static in both the main and dll crate, i get an output like this

0x7ffe64a892a0
25
0x7ff601477646
745809407

Why is the address of the static in the main crate incorrect? Is this even something I'm allowed to do with a dll?

2 posts - 2 participants

Read full topic

🏷️ rust_feed