#[link] attribute on extern "Rust" block

⚓ rust    📅 2025-05-29    👤 surdeus    👁️ 3      

surdeus

I'm trying to figure out how to add load time dll dependency to a rust binary. I managed to get it working with the "C" ABI as expected by just linking against the import library and defining the symbols in an extern block like this.

#[link(name = "plugin.dll", kind="dylib")]
unsafe extern "C" {
	pub fn print_test();
}

I wanted to try doing the same with the extern "Rust" abi (the dll is from a rust crate). But when i tried to use the "Rust" ABI , the link attribute is ignored which causes a missing symbol error from the linker.

#[link(name = "plugin.dll", kind="dylib")]
unsafe extern "Rust" {
	pub fn print_test();
}

Warning:

warning: attribute should be applied to an `extern` block with non-Rust ABI
  --> base\src\main.rs:17:1
   |
17 | #[link(name = "plugin.dll", kind="dylib")]

Error:

error LNK2019: unresolved external symbol print_test referenced in function _ZN4base4main17h83576916a4b36923E

Why can't I link against an import library with extern "Rust". Is there any other way around this that still preserves plugin.dll as a load time dependency (i.e. not using LoadLibrary and GetProcAddress)?

Note

I know extern "Rust" is unstable and i shouldn't rely on it for anything important. I'm just doing this to learn.
I've tried using extern "Rust" with runtime loading before and it worked ok enough for me. I just wanted to try the same with a load time dll because it requires less boilerplate from the main crate.

2 posts - 2 participants

Read full topic

🏷️ rust_feed