Skip to content

link-search-path doesn't have a way to specify priority / doesn't auto deprioritize OS link paths #15220

Closed
@vlovich

Description

@vlovich

Problem

I modified the llama.cpp repo that llama-cpp-2 has as a submodule to expose a new symbol & then added a method to call that new symbol in llama-cpp-2 via a Rust pub API. I then called the new Rust pub API in my code and added cpal as a dependency and the path to my local llama-cpp-2 as a dependency with the cuda feature on (cuda doesn't matter but right now that forces llama.cpp to be built as a shared library). I then tried to build my crate. I also installed libllama.so in my OS.

I expected to see this happen: Crate builds

Instead, this happened: Crate fails to link saying the new symbol I added to llama.cpp is missing.

The root cause is that cpal depends indirectly on alsa-sys which uses pkg-config which adds the location of the found library in /usr/lib to the search path. This happens before llama-cpp-2 runs and thus /usr/lib ends up ahead of the path that llama-cpp-2 prints causing it to try to link against the OS library that doesn't have the symbol.

While there's no way to specify priority and while valuable, that feels like it would be a much larger discussion. I was wondering if a more targeted fix would be to ensure that all paths under OUT_DIR are moved to the front (in an order-preserving way) while the remaining directories are moved later (in an order-preserving way). That way the linker ALWAYS looks for paths within OUT_DIR before looking in other locations.

I'm not sure if this bug is in rust itself or in Cargo, so I also filed to rust: rust-lang/rust#137404

Steps

  1. Install alsa and another dynamic library that conflicts with a crate that tries to build the same dynamic library (tested on Linux with libllama.so and llama-cpp-sys-2)
  2. Add alsa-sys as a dependency & use some symbols from it so that /usr/lib ends up getting added to the search path by pkg-config
  3. Modify the crate to build a new version of the conflicting dynamic library with new symbols & try to use those symbols

Expected: build should still work because the dynamic library should be used out of OUT_DIR not /usr/lib.

Possible Solution(s)

Bubble up link search paths under OUT_DIR to the front and bubble down search paths outside of it in a relative order preserving way.

Notes

No response

Version

cargo 1.84.0 (66221abde 2024-11-19)
release: 1.84.0
commit-hash: 66221abdeca2002d318fde6efff516aab091df0e
commit-date: 2024-11-19
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Arch Linux Rolling Release [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsA-linkageArea: linker issues, dylib, cdylib, shared libraries, soC-bugCategory: bugS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions