Skip to content

Multiple #[link(name="X")] attributes result in duplicate link arguments #65847

Closed
@nagisa

Description

@nagisa

Given static libraries, having duplicate -lfoo is more likely to result in an error than not. Having multiple #[link] blocks with kind="static" specified or inferred in any way will cause the linking to fail. Consider for example this:

extern "C" {
    #![link(name="zstd", kind="static")]
    fn ZSTD_freeCCtx();
}

extern "C" {
    #![link(name="zstd", kind="static")]
    fn ZSTD_createCCtx();
}

fn main() {
    unsafe {
        ZSTD_createCCtx();
        ZSTD_freeCCtx();
    }
}

will pass to the linker

"-Wl,--whole-archive" "-lzstd" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "-lzstd" "-Wl,--no-whole-archive"

which will fail due to duplicate symbols. This is somewhat surprising because rustc will avoid passing through to the linker the -lfoo from -l static=foo if there’s a #[link] attribute in the crate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions