Skip to content

fix: link the static C++ stdlib with -bundle, once per Build - #1955

Merged
NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:fix/cxx-stdlib-static-once-per-build
Sep 25, 2026
Merged

NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:fix/cxx-stdlib-static-once-per-build

Conversation

@waltuov

@waltuov waltuov commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

With cpp_link_stdlib_static(true), cc emits cargo:rustc-link-lib=static=stdc++. rustc then looks up libstdc++.a in its own search paths and bundles it into the rlib. That fails when the archive only lives in the toolchain's directories, as with MinGW. It is also why cc-test needed a libstdc++.a symlink in OUT_DIR. A CXXSTDLIB value that already names a kind, such as static:-bundle=stdc++, got a second static= prefix.

Change

  • The static C++ stdlib is emitted as static:-bundle=<stdlib>, so the linker of the final binary finds it.
  • rustc rejects a library passed twice when either mention has link modifiers. As discussed in the issue, the line is emitted once per Build. The state lives in the Build's cache, which its clones share, so several compile() calls on one Build or its clones emit it once. Separate Builds don't share it.
  • wasm32, pauthtest and Apple targets keep static=<stdlib>. On wasm32 and pauthtest cc may also pass -lstatic=c++ or -lc++ through rustc-flags, and a second mention with modifiers would fail. On Apple targets rustc passes no static hint and ld64 prefers the dylib, so -bundle would silently link it dynamically.
  • A stdlib value that contains = is emitted unchanged, once per Build.
  • The docs of cpp_link_stdlib_static describe this and what to do when two Builds link the same stdlib.
  • cc-test built baz twice on Linux, linking stdc++ once dynamically and once statically, which now fails in rustc. It now builds baz once, with the static stdlib on *-linux-gnu targets. The symlink is removed, since rustc no longer looks up the archive. The check now uses the target instead of the host, so the Apple cross jobs on Linux no longer try to link the host's libstdc++.a.
  • The CUDA build in cc-test sets cpp_link_stdlib(None) on *-linux-gnu, since baz already links stdc++ statically there.

Validation

  • A new subprocess test checks the emitted lines. Three compiles on one Build and a clone emit the stdlib once. Two separate Builds emit it twice. A compile with metadata off doesn't count. wasm32, pauthtest and x86_64-apple-darwin keep static=. CXXSTDLIB=static:-bundle=c++ and CXXSTDLIB=dylib=stdc++ are passed through. The test sets its target and host, so it doesn't depend on the CI host.
  • cargo test, cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings (also with --features cc-test/test_cuda) pass on Windows (MSVC), and cc-test builds and passes there. On CI, cc-test links libstdc++ statically without the symlink on x86_64 and i686 Linux, and the CUDA job passes. MinGW, the reporter's setup, is not covered by cc-test.
  • Checked with rustc directly: -l static:-bundle=foo builds an rlib without finding foo, while -l static:-bundle=foo -l foo fails with "overriding linking modifiers from command line is not supported".

Limitations

  • A build script that links the same C++ stdlib from two separate Builds with this option, or links it both statically and dynamically, now fails in rustc. The docs suggest one Build or its clones, or cpp_link_stdlib(None) on the others.
  • A crate built as a staticlib no longer contains the C++ stdlib objects. It is listed by --print native-static-libs and has to be linked by the user.
  • The third point of the issue, an explicit cpp_link_stdlib overriding CXXSTDLIB, is not covered here.

Thanks @kb-1000 for the report and for handing this over.

Fixes #1856

With `cpp_link_stdlib_static(true)`, cc emitted `rustc-link-lib=static=stdc++`, which makes rustc look up the archive in its own search paths and bundle it into the rlib. That fails when the archive is only in the toolchain's directories, as with MinGW. Emit `static:-bundle=` instead so the linker of the final binary resolves it.

rustc rejects a library passed twice when either mention has link modifiers, so the line is emitted once per `Build` (shared with its clones), however many times it compiles. `wasm32` and `pauthtest`, where cc may also link the C++ stdlib through `rustc-flags`, keep `static=`. So do Apple targets, where rustc passes no static hint and ld64 prefers the dylib, so an unbundled stdlib would silently link dynamically.

A C++ stdlib value that already names a link kind, such as `CXXSTDLIB=static:-bundle=stdc++`, is now emitted unchanged instead of getting a second `static=` prefix.

cc-test now builds `baz` once, statically linked on `*-linux-gnu` targets, and no longer needs the `libstdc++.a` symlink. Its CUDA build sets `cpp_link_stdlib(None)` there, since `baz` already links stdc++.

Fixes rust-lang#1856
@waltuov

waltuov commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

One data point before review. A GitHub code search for cpp_link_stdlib_static finds mostly crates with a single C++ Build, but sorah/protovalidate-rb compiles six libraries with separate Builds and turns on the static stdlib for each of them on musl and MinGW. It depends on cc = "1", so with this PR it would hit the rustc error after a cargo update, until it shares one Build or sets cpp_link_stdlib(None) on five of them.

Are you fine with that as a documented change, or would you rather dedupe this one line per build script process after all?

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! Will cut a new release afterwards today

@NobodyXu
NobodyXu merged commit 87533b7 into rust-lang:main Sep 25, 2026
83 checks passed
@waltuov
waltuov deleted the fix/cxx-stdlib-static-once-per-build branch September 25, 2026 13:16
@NobodyXu

Copy link
Copy Markdown
Contributor

so with this PR it would hit the rustc error after a cargo update, until it shares one Build or sets cpp_link_stdlib(None) on five of them.

Hmm I think we'd have to ship the right behaviour instead of bundling stdc++ into rlib

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.

Broken behavior of cpp_link_stdlib_static

2 participants