Skip to content

fix link flags of suffix versioned libs#3908

Open
xiaopeng-tranxmart wants to merge 2 commits intobazelbuild:mainfrom
xiaopeng-tranxmart:fix_suffix_versioned_libs_link_opts
Open

fix link flags of suffix versioned libs#3908
xiaopeng-tranxmart wants to merge 2 commits intobazelbuild:mainfrom
xiaopeng-tranxmart:fix_suffix_versioned_libs_link_opts

Conversation

@xiaopeng-tranxmart
Copy link

Problem

When a versioned shared library (e.g. libcheryl.so.3.8) is used, the current behavior generates linker flags such as:

  • rustc -dylib=cheryl
  • gcc -lcheryl

These flags assume the presence of an unversioned shared library (libcheryl.so). As a result, linking fails when only versioned libraries (e.g., libcheryl.so.3.8) are available:

ld: cannot find -lcheryl

The correct linker options in this case should explicitly reference the versioned .so:

  • rustc -Clink-arg=-l:libcheryl.so.3.8
  • gcc -l:libcheryl.so.3.8

Solution

IMO, the current implementation assumes that users always provide unversioned shared libraries (libxxx.so). This assumption is too restrictive:

  1. Some libraries do not provide an unversioned .so symlink (e.g., only libxxx.so.X.Y exists)
  2. Library naming may not follow the libxxx.so convention (e.g., xxx.so.1)
  3. Users may require fine-grained control over linker arguments, especially for ABI-specific or version-pinned dependencies

This PR updates the linking behavior as follows:

  • If the library file name matches the conventional form lib<name>.so, generate -ldylib=<name>
  • Otherwise, pass the exact file name to the linker via -Clink-arg=-l:<filename>

"-ldylib=%s" % get_lib_name(artifact),
]
lib_file_name = artifact.basename
if lib_file_name.startswith("lib") and lib_file_name.endswith(".so"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check how this interacts with get_lib_name_for_windows? (I'm a bit worried that this check is overly specific to linux and may regress windows.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. It appears that the library search behavior on Windows and Darwin differs from Linux. Since I’m not familiar with those platforms, this change is currently limited to Linux only.

@krasimirgg krasimirgg added awaiting-response Maintainers have responded to the pull-request or thread and now await contributor responses. bug labels Mar 19, 2026
Copy link
Collaborator

@krasimirgg krasimirgg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@krasimirgg krasimirgg enabled auto-merge March 19, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-response Maintainers have responded to the pull-request or thread and now await contributor responses. bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants