Repository navigation
feat: add CXXSTDLIB_STATIC to link the C++ stdlib statically from outside - #1957
Merged
Merged
Conversation
NobodyXu
approved these changes
Sep 27, 2026
NobodyXu
left a comment
Contributor
There was a problem hiding this comment.
Thank you!
The variable applies to every crate in the build.
Well, for that, we can potentially add support for crate prefix to every environment variable
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #1955 for the third point of #1856.
Problem
cpp_link_stdlib_staticcan only be turned on by the crate itself. When a crate setscpp_link_stdlib,CXXSTDLIBis ignored as well. So a user can't make such a crate link the C++ standard library statically without patching it.Change
A new environment variable,
CXXSTDLIB_STATIC, as discussed in #1856.Buildlinks the C++ stdlib as ifcpp_link_stdlib_static(true)were set, including crates that set it tofalse.cpp_link_stdlib, thenCXXSTDLIB, then the default).cpp_link_stdlib(None)still links nothing.CXXSTDLIB, for exampleCXXSTDLIB_STATIC_x86_64_unknown_linux_gnuorHOST_CXXSTDLIB_STATIC/TARGET_CXXSTDLIB_STATIC. A more specific variable wins, soCXXSTDLIB_STATIC_<target>=0turns it off for one target.CRATE_CC_NO_DEFAULTS, it can only turn the option on. Values follow the same rule asCC_FORCE_DISABLE: on unless set to"","0","no"or"false". The check is shared withget_env_boolean.cpp_link_stdlib_static(true):static:-bundle=<stdlib>once perBuild, andstatic=on wasm32, pauthtest and Apple.Validation
cxxstdlib_static_env_metadata(same subprocess pattern ascpp_link_stdlib_static_metadata, explicit target and host). It covers unset and false values, the plain and prefixed forms, a cross-compile withTARGET_CXXSTDLIB_STATIC, overridingcpp_link_stdlib_static(false), a target value taking precedence,cpp_link_stdlib(None), Apple keepingstatic=, and thererun-if-env-changedlines.src/lib.rschange.cargo test,cargo fmt --check,cargo clippy --workspace --all-targets -- -D warnings(also with--features cc-test/test_cuda) andcargo test -p cc-test. Linux, MinGW and macOS linking weren't run end to end. CI doesn't set the variable, so cc-test's Linux and CUDA blocks are unaffected.Limitations
cpp_link_stdlib_static(true), a build script that links the C++ stdlib from two separateBuilds (for example a C++ build and a CUDA build) then fails in rustc. This is documented.c++_shared, so this variable should go together withCXXSTDLIB=c++_staticthere.