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

Unable to statically link a native library if there a dynamic library in the same directory. #124462

Open
devxu opened this issue Apr 28, 2024 · 7 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@devxu
Copy link

devxu commented Apr 28, 2024

Code

rustc -L native='/usr/local/Cellar/libplist/2.4.0/lib' -l static=plist-2.0 main.rs

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (aed2187d5 2024-04-27)
binary: rustc
commit-hash: aed2187d53b8789e3a37f50ae36f894a2a679077
commit-date: 2024-04-27
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.4

Error output

There were both of a dynamic library file and a static library file in the lib directory.
The program was built successfully, but it was linked with a dynamic library rather than the static library.
@devxu devxu added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 28, 2024
@TinusgragLin
Copy link

I can confirm that this does not happen in my Linux with rustc 1.77.2. @devxu Could you try this in a different MacOS environment other than yours?

(BTW, I don't think the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ tag is accurate here as the compiler does not actually panic with an internal error.)

@TinusgragLin
Copy link

@rustbot label -I-ICE

@rustbot rustbot removed the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Apr 29, 2024
@jieyouxu jieyouxu added A-linkage Area: linking into static, shared libraries and binaries S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
@bjorn3
Copy link
Member

bjorn3 commented May 13, 2024

On Linux it is possible to use -Bdynamic/-Bstatic to request the linker to link dynamically/statically. macOS doesn't support this however, so rustc doesn't have a way to make the linker pick whether to statically or dynamically link.

fn takes_hints(&self) -> bool {
// Really this function only returns true if the underlying linker
// configured for a compiler is binutils `ld.bfd` and `ld.gold`. We
// don't really have a foolproof way to detect that, so rule out some
// platforms where currently this is guaranteed to *not* be the case:
//
// * On OSX they have their own linker, not binutils'
// * For WebAssembly the only functional linker is LLD, which doesn't
// support hint flags
!self.sess.target.is_like_osx && !self.sess.target.is_like_wasm
}

@kekeimiku
Copy link

I had the same problem with macos m3

@kekeimiku
Copy link

Is there any way to temporarily resolve this?

@kekeimiku
Copy link

Very bad temporary solution, just in case anyone needs it, I'm putting it here, feel free to ignore my code. Hope this issue will be resolved soon.

// build.rs
    const APPLE: [&str; 2] = ["aarch64-apple-darwin", "aarch64-apple-ios"];
    if APPLE.contains(&target.as_str()) {
        fs::rename(
            path.join("my.dylib"),
            path.join("this.not.static.my.dylib"),
        )
        .unwrap();

        println!("cargo:rustc-link-search={}", path.display());
    }

@devxu
Copy link
Author

devxu commented May 15, 2024

@kekeimiku In build.rs do these:
The pkg-config crate is recommended, which is easy to get the dependent static libraries.
Copy the all of dependent static library files to the $OUT_DIR/libs, which can be specified as rustc-link-search directory.

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. S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. 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

6 participants