Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nagisa opened this issue Oct 26, 2019 · 1 comment
Closed
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Oct 26, 2019

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.

@nagisa nagisa added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 26, 2019
@jonas-schievink jonas-schievink added the C-bug Category: This is a bug. label Oct 26, 2019
@nagisa nagisa changed the title Multiple #[link(name="X")] attributes combined with -lstatic=X result in duplicate link arguments Multiple #[link(name="X")] attributes combined in duplicate link arguments Oct 26, 2019
@nagisa nagisa changed the title Multiple #[link(name="X")] attributes combined in duplicate link arguments Multiple #[link(name="X")] attributes result in duplicate link arguments Oct 27, 2019
@nagisa
Copy link
Member Author

nagisa commented May 28, 2020

Duplicate of #38460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants