Faster C++ Linking on Linux with mold: GCC, Clang, and CMake Setup

,
Faster C++ Linking on Linux with mold: GCC, Clang, and CMake Setup

Once ccache makes recompiles cheap, linking becomes the slowest part of a C++ edit-build-run loop. Every change to any object file means the linker has to rebuild the whole executable, and with GNU ld that can take seconds to minutes for a large binary with debug info. mold is a drop-in replacement linker designed to be much faster by using all CPU cores.

This guide shows how to switch GCC, Clang, and CMake to mold on Linux, how to prove it is actually being used, how to benchmark it against GNU ld and lld on your own project, and how to fix the “linker not found” errors that stop most first attempts.

Quick fix: Install mold so ld.mold is on your PATH, then link with -fuse-ld=mold (GCC 12.1+ or Clang). In CMake 3.29+, configure with -DCMAKE_LINKER_TYPE=MOLD. Verify with readelf -p .comment ./app | grep mold.

Why does the linker matter for build speed?

Compilation parallelizes naturally: Ninja runs one compiler per core, and ccache skips unchanged files. Linking a single executable is one step that has to finish before you can run anything. GNU ld (also called BFD ld) does most of its work on a single thread, so on a many-core machine it leaves most of the CPU idle.

mold and LLVM’s lld are both multithreaded. mold’s author designed it specifically to be as fast as possible on multicore machines, and it is broadly compatible with GNU ld’s command-line options.

LinkerDriver flagNotes
GNU ld (BFD)default, or -fuse-ld=bfdMost compatible, usually the slowest
gold-fuse-ld=goldOlder GNU linker; not installed on many current distros
lld-fuse-ld=lldLLVM’s linker, fast and widely packaged
mold-fuse-ld=moldDesigned for speed; GNU ld compatible options

What changed in mold 3.0?

mold 3.0.0, released on October 5, 2026, is the first version written in Rust; 2.42.1 was the last C++ release. The release notes describe it as a drop-in replacement with the same options, the same targets, and performance on par with 2.x. Bounds checking means corrupted input makes it stop with an error instead of crashing. Building from source now needs Cargo and a recent Rust toolchain. Most users install a binary package, so for them nothing changes.

Note: Distribution packages often lag behind upstream. Everything in this guide works with mold 2.x as well, so the version in your distro’s repository is fine.

How do you install mold?

  1. From your distribution:
    sudo apt install mold        # Debian, Ubuntu
    sudo dnf install mold        # Fedora
    mold --version

    The output is one line starting with mold followed by the version and compatible with GNU ld.

  2. Or use the official prebuilt tarball, which needs no root:
    curl -LO https://github.com/rui314/mold/releases/download/v3.0.0/mold-3.0.0-x86_64-linux.tar.gz
    tar xzf mold-3.0.0-x86_64-linux.tar.gz
    export PATH="$PWD/mold-3.0.0-x86_64-linux/bin:$PATH"
    mold --version
    mold 3.0.0 (8de38c35a2df16a25f7ff87ac3ad07156a925beb; compatible with GNU ld)

    That is the exact line the 3.0.0 tarball printed on Debian 13. The bin directory contains both mold and ld.mold, which is the name the compiler driver looks for.

How do you link with mold using GCC or Clang?

  1. Pass -fuse-ld=mold at link time:
    g++ -O2 main.o util.o -o app -fuse-ld=mold
    clang++ -O2 main.o util.o -o app -fuse-ld=mold

    A successful link prints nothing. GCC accepts mold here from version 12.1.0. For older GCC, the mold README recommends -B/usr/local/libexec/mold (or the matching libexec/mold directory under your install prefix), which contains an ld symlink to mold.

  2. Confirm which linker built the binary:
    readelf -p .comment app
    String dump of section '.comment':
      [     0]  mold 3.0.0 (8de38c35a2df16a25f7ff87ac3ad07156a925beb; compatible with GNU ld)
      [    4e]  GCC: (Debian 14.2.0-19) 14.2.0

    Look for the mold line. A binary linked by GNU ld shows only the compiler line.

Clang also accepts an absolute path, -fuse-ld=/opt/mold/bin/mold, if mold is not on PATH. GCC does not accept a path there.

How do you use mold with CMake?

CMake 3.29 added the CMAKE_LINKER_TYPE variable and the LINKER_TYPE target property. They translate a linker name into the right driver flag, so you do not hard-code -fuse-ld.

  1. Choose mold for the whole build:
    cmake -S . -B build -G Ninja -DCMAKE_LINKER_TYPE=MOLD
    cmake --build build -v | grep fuse-ld
    [2/2] : && /usr/bin/cc  -fuse-ld=mold -Wl,--dependency-file=CMakeFiles/app.dir/link.d CMakeFiles/app.dir/main.c.o -o app   && :

    You should see -fuse-ld=mold on the link line.

  2. Or set it per target in CMakeLists.txt:
    cmake_minimum_required(VERSION 3.29)
    project(demo CXX)
    add_executable(app main.cpp)
    set_property(TARGET app PROPERTY LINKER_TYPE MOLD)

    You should see the same flag only for app.

The CMake documentation lists MOLD as supported on Linux with GNU, Clang, LLVMFlang, and NVIDIA compilers. Other values include LLD, GOLD, BFD, and SYSTEM. CMake does not translate linker-specific options, so any -Wl,... flags you add must be understood by the linker you pick.

Tip: Prefer setting CMAKE_LINKER_TYPE from a preset or the command line rather than forcing it in CMakeLists.txt. Developers without mold can still build, and CI can opt in explicitly.

How do you benchmark mold against GNU ld and lld?

Measure on your own project; linker speedups depend on binary size, debug info, LTO, and core count. Relink only, without recompiling, so the numbers isolate the linker:

for ld in bfd lld mold; do
  for run in 1 2 3; do
    /usr/bin/time -f "$ld %e s" g++ $(cat objs.txt) -o app_$ld -fuse-ld=$ld
  done
done

The loop prints three timings per linker. Compare the median, and skip the first run if your files were not in the page cache.

Benchmark run: GNU ld 2.44 vs mold on 508 MB of objects

I ran that loop on a C++ test project of 120 translation units that use <regex>, <map>, and <format>, compiled in Debug mode. The objects added up to 508 MB, mostly debug info. The machine had 8 cores and ran Debian 13 with g++ 14.2.0, GNU ld 2.44, the distro’s mold 2.37.1, and the official mold 3.0.0 tarball from the install step above.

LinkerRun 1Run 2Run 3Output size
GNU ld 2.44 (-fuse-ld=bfd)2.49 s2.58 s2.53 s99,250,112 bytes
mold 2.37.1 (-fuse-ld=mold)0.16 s0.15 s0.16 s106,228,800 bytes
mold 3.0.0 (official tarball on PATH)0.16 s0.11 s0.11 s106,228,696 bytes
gold (-fuse-ld=gold)failedfailedfailednot installed

That is about 16x on this workload: 2.53 s against 0.16 s at the median, and the Rust-based 3.0.0 was at least as fast as 2.37.1. On a 120-file project, that saves about two seconds per edit-compile-run cycle. The savings grow with binary size and debug info.

Terminal output linking 508 MB of C++ Debug objects: GNU ld 2.44 takes about 2.5 s per link, mold 2.37.1 takes 0.15 to 0.16 s, gold is not found, and readelf confirms the mold string in .comment
Real run on Debian 13, 8 cores: the same object files relinked three times each with GNU ld, gold, and Debian’s mold 2.37.1.

What I ran into

Three things were not what I expected. First, the gold row failed outright with collect2: fatal error: cannot find ‘ld’. Debian 13 does not install ld.gold by default, and the error names ld rather than gold, which is misleading. Second, the mold binary was about 7% larger than the BFD one. Do not use file size as a signal that something went wrong; check readelf -p .comment, which printed mold 2.37.1 (compatible with GNU ld). Third, configuring with -DCMAKE_LINKER_TYPE=MOLD on CMake 4.4.4 worked without any extra flags. So did wrapping an unmodified build in mold -run cmake --build build-run: the resulting binary carried the mold string in .comment, next to GCC: (Debian 14.2.0-19) 14.2.0.

How do you fix “linker not found” errors?

The compiler driver does not take a path; it searches for a program named ld.mold. If that search fails, the error depends on the compiler.

Flowchart showing how GCC or Clang with -fuse-ld=mold searches for ld.mold and the GCC and Clang errors when it is missing
How the compiler driver finds ld.mold, and the errors GCC and Clang print when it cannot.

GCC reports a confusing message that mentions ld, not mold:

$ gcc -fuse-ld=mold main.c -o app
collect2: fatal error: cannot find ‘ld’
compilation terminated.

Clang is clearer:

clang++: error: invalid linker name in argument '-fuse-ld=mold'

In CMake, the same failure appears during configure, inside the compiler check, because CMake links a test program with the requested linker:

CMake Error at .../CMakeTestCCompiler.cmake:65 (message):
  The C compiler "/usr/bin/cc" is not able to compile a simple test program.
  ...
    collect2: fatal error: cannot find ‘ld’
CauseFix
mold not installedInstall the package or tarball
Installed, but ld.mold not on PATHAdd its bin directory to PATH, or use Clang with an absolute path
GCC older than 12.1Use -B<prefix>/libexec/mold or upgrade GCC
CMake cache from a failed configureDelete the build directory and reconfigure
CMake older than 3.29Upgrade CMake, or set CMAKE_EXE_LINKER_FLAGS=-fuse-ld=mold

Warning: CMake records compiler check results in the cache. After fixing PATH, reconfiguring an existing build directory may keep the old failure. Start from a clean directory.

What if you cannot change the build flags?

Some build systems make it hard to pass linker flags. mold -run starts a command and redirects its calls to ld, ld.bfd, ld.lld, or ld.gold to mold:

mold -run make -j"$(nproc)"
mold -run cmake --build build

The build runs unchanged. Check the result with readelf -p .comment.

Are there cases where mold is the wrong choice?

  • Complex linker scripts: embedded and kernel-style builds often depend on GNU ld script features. mold’s stated goal is GNU ld compatibility, but test these builds carefully.
  • Release reproducibility: some teams keep the distro default linker for release artifacts and use mold for development builds.
  • Non-Linux targets: this guide covers Linux ELF; check the mold README for other platforms.

FAQ

Is mold safe for production binaries?

Many projects use it, and its output is standard ELF. Still, run your full test suite after switching, as with any toolchain change.

Does mold help with LTO builds?

With LTO, much of the link time is spent in the compiler’s optimization step, not the linker itself, so the improvement is usually smaller.

Can I combine mold with ccache and Ninja?

Yes. They address different steps: Ninja schedules, ccache skips compiles, mold speeds up the link.

Bottom line

Switching to mold is usually one flag: -fuse-ld=mold, or -DCMAKE_LINKER_TYPE=MOLD in CMake 3.29+. Verify with readelf, benchmark relinks on your own code, and if you see cannot find ‘ld’, put ld.mold on PATH and reconfigure from a clean directory.

Related guides

Official references

Leave a Reply