Why the #[link] attribute cannot be applied to `extern "Rust"`?

When I try to do this, the compiler displays a warning

warning: attribute should be applied to an `extern` block with non-Rust ABI
 --> src\main.rs:3:1
  |
3 | #[link(name="b")]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: `#[warn(unused_attributes)]` on by default

and ignores this attribute thereby causing an error, I have to use the syntax "-l" and it works. What is the meaning of this restriction?

You have to apply it to an extern block, not an extern function. For example #[link(name = "b")] extern "C" {}.

the example of use in which I get this warn looks like this`

#[link(name="b")]
extern "Rust" {
fn say_b_addr()->usize;
fn get_api()->Fns;
}      

Guess that isn't allowed then. Note that C libraries can't use the Rust abi and if you are using a rust library, you almost certainly shouldn't use #[link] either. Only if you can ensure that the exact same rustc version is used is it maybe allowed. And even then anything type that containes allocated memory is not allowed either.

1 Like

Then why does the -l flag work and is not ignored? After all, the -l flag and #[link] perform the same logic,or I'm probably wrong.

If you use -l with a rust dynamic library, the assumption is that the exported interface uses the C abi, not the Rust abi. Rustc has no way to check if this is true or not.

2 Likes

Thank you for the explanation, it all explains

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.