Skip to content

How to link dylib files? #114445

Closed
Closed

Description

@superchow

#[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?

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 4, 2023
  2. bjorn3 commented on Aug 4, 2023

    @bjorn3
    Member

    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.

  3. added
    A-linkageArea: linking into static, shared libraries and binaries
    O-macosOperating system: macOS
    on Aug 4, 2023
  4. inferiorhumanorgans commented on Aug 5, 2023

    @inferiorhumanorgans

    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.

  5. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 8, 2023
  6. RodBurman commented on Dec 26, 2024

    @RodBurman

    This looks to be a question about different OS's that has been satisfactorily answered, maybe we should close the issue?

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

    A-linkageArea: linking into static, shared libraries and binariesO-macosOperating system: macOST-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