Skip to content

Fix linker selection: avoid LLD when using GCC with LTO#3222

Open
plop-user wants to merge 2 commits intogoogle-deepmind:mainfrom
plop-user:fix/cmake-change
Open

Fix linker selection: avoid LLD when using GCC with LTO#3222
plop-user wants to merge 2 commits intogoogle-deepmind:mainfrom
plop-user:fix/cmake-change

Conversation

@plop-user
Copy link
Copy Markdown

@plop-user plop-user commented Apr 14, 2026

Fix linker selection: avoid LLD when using GCC with LTO

Summary

MuJoCo's CMake configuration currently prioritizes linkers in the following order: LLDgoldbfd.
This causes build failures when using GCC with LTO enabled, because LLD is selected even though it is incompatible with GCC's LTO object format.

This PR updates the linker selection logic to:

  • Prefer LLD only when using Clang.
  • Fall back to gold/bfd when using GCC.
  • change 'char* c' to 'const char* c' as strchr() returns a const and is an error in GCC 15

Problem

MujocoLinkOptions.cmake detects linker support using:

check_c_source_compiles("int main() {}" SUPPORTS_LLD)

This test passes for LLD because:

  1. It does not use LTO.
  2. It only checks basic compilation and linking.

As a result, LLD is selected and -fuse-ld=lld is added to EXTRA_LINK_OPTIONS.

Later in the build process, MujocoOptions.cmake enables LTO:

CMAKE_INTERPROCEDURAL_OPTIMIZATION=ON

This causes GCC to compile with -flto. LLD does not support GCC's GIMPLE IR or liblto_plugin.so. Therefore, when LLD encounters these object files, it only sees an empty .text section. Symbols such as main appear undefined, and linking fails.


Fix

Restrict usage of LLD to Clang builds only.

Key changes:

  • Only probe and enable lld (Linux/macOS) and lld-link (Windows) when CMAKE_C_COMPILER_ID MATCHES "Clang".
  • For GCC, skip LLD entirely and fall back to gold or bfd.

This ensures compatibility with GCC LTO while preserving LLD usage for Clang.


Changes

Updated MujocoLinkOptions.cmake:

  • Added compiler checks before enabling LLD.
  • Prevented LLD selection when using GCC.
  • Maintained existing fallback behavior (goldbfd).

Testing

Tested under the following configurations:

  • GCC + LTO (before fix): Linking fails with undefined symbols when LLD is selected.
  • GCC + LTO (after fix): Successfully links using gold/bfd.
  • Clang + LLD: Works as expected (no regression).
  • Python bindings: Import succeeds after build (python -c "import mujoco").

Environment

  • OS: Linux 6.19.11-arch1-1
  • Compiler: GCC 15
  • Linker: LLD 22.1

@Yashu-Maurya
Copy link
Copy Markdown

Must needed change. Similar error in Macos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants