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

How to link dylib files? #114445

Open
superchow opened this issue Aug 4, 2023 · 2 comments
Open

How to link dylib files? #114445

superchow opened this issue Aug 4, 2023 · 2 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries O-macos Operating system: macOS T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@superchow
Copy link

#[link(name = "Mylib" )] works on the Windows, but Mac failed🚬

I try code in build.rs

let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", manifest_dir.display());
println!("cargo:rustc-link-lib=Mylib");
but also not work.

Stack trace

dyld[29363]: Library not loaded: @rpath/libMylib.dylib
  Referenced from: <987AFB97-031F-3387-A7C3-047E9989602C> /Users/admir/Desktop/Code/rust-desktop-demo/src-tauri/target/debug/tauri-ts-demo
  Reason: tried: '/System/Volumes/Preboot/Cryptexes/OS@rpath/libSampleBlueTooth.dylib' (no such file), '/usr/local/lib/libMylib.dylib' (no such file), '/usr/lib/libMylib.dylib' (no such file, not in dyld cache)

How Can i link libMylib.dylib success?

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 4, 2023
@bjorn3
Copy link
Member

bjorn3 commented Aug 4, 2023

It looks like it successfully linked it, but you need to tell the dynamic linker where to find it using eg DYLD_LIBRARY_PATH when running.

@bjorn3 bjorn3 added A-linkage Area: linking into static, shared libraries and binaries O-macos Operating system: macOS labels Aug 4, 2023
@inferiorhumanorgans
Copy link

At first blush this looks like everything's working as intended if the library is at the project root (CARGO_MANIFEST_DIR) and you're launching the program from there.

I'm not at all familiar with Windows, but I believe it will pretty much always check the directory where the executable is and the current directory for libraries. This would explain things working if you're launching the app via cargo.

https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

By default macOS will check the current directory as well. However in this case the native library was built in a way that tells the runtime linker to only look in the paths specified by the rpath. You can verify this with otool -D libMylib.dylib and see that the name listed begins with @rpath.

There are a few things you can try:

You can install the library into one of the rpath directories. otool -l target/… will list your application's rpath (look for LC_LOAD_DYLIB). rust-lang/cargo#5077 discusses how to change the rpath at build time.

Before you build your application you can change how the library identifies itself to the linker with install_name_tool.

At run-time you can include the directory where the library is in DYLD_LIBRARY_PATH.

You can change whatever's being used to create the native library to not prepend @rpath.

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 8, 2023
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 O-macos Operating system: macOS 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

5 participants