Skip to content

Incorrect information about link attribute on extern blocks #1986

Open

Description

@wmmc88

[items.extern.attributes.link.intro] states:

The link attribute specifies the name of a native library that the compiler should link with for the items within an extern block.

The incorrect part of this statement is for the items within an extern block. The link attribute seems to behave exactly like rustc-link-lib which just adds a linker argument to link against another library. The current wording in the reference implies that it will only try to link the items within that specific extern block to the library, but any external symbol could be resolved by that library.

Example

/// Empty extern block
#[link(name = "foo")]
unsafe extern "C" {}

/// Extern block with no `link`
unsafe extern "C" {
    /// This is a function in `libfoo`
    fn bar();
}

bar will be resolved with its implementation in the foo library, despite the link attribute being on a different extern block

Activity

  1. chorman0773 commented on Aug 30, 2025

    @chorman0773
    Contributor

    Also, conversely, I believe that (unless you have an explicit link_ordinal attribute on windows), symbols in a #[link] extern block can be resolved by any library, and not just the linked-in one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions