[native_toolchain_c] Fix Android c++_static not linking libc++abi#3241
Merged
dcharkes merged 2 commits intodart-lang:mainfrom Mar 17, 2026
Merged
[native_toolchain_c] Fix Android c++_static not linking libc++abi#3241dcharkes merged 2 commits intodart-lang:mainfrom
dcharkes merged 2 commits intodart-lang:mainfrom
Conversation
On Android, let clang's driver handle C++ standard library linking instead of passing `-l c++_static` directly. This ensures both libc++_static.a and libc++abi.a are linked when statically linking the C++ standard library. When cppLinkStdLib is 'c++_static', pass `-static-libstdc++` to clang, which correctly expands to link both libraries. For the default c++_shared case, clang handles it automatically. Fixes dart-lang#3240
PR HealthBreaking changes ✔️
This check can be disabled by tagging the PR with Changelog Entry ✔️
Changes to files need to be accounted for in their respective changelogs. This check can be disabled by tagging the PR with API leaks ✔️The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
This check can be disabled by tagging the PR with |
On Android, use -Wl,-Bstatic/-Bdynamic to force static resolution of -lc++ when cppLinkStdLib is 'c++_static'. This picks up the NDK's libc++.a linker script which expands to INPUT(-lc++_static -lc++abi), ensuring libc++abi is also linked. Previously, -l c++_static was passed directly which only linked libc++_static.a but not libc++abi.a. This left C++ ABI symbols (typeinfo, vtables, RTTI) undefined, causing dlopen failures on newer Android versions with stricter linker namespace enforcement. Also adds a test that verifies the compiled shared library has no undefined C++ ABI symbols, and documents c++_static in the cppLinkStdLib API docs. Fixes dart-lang#3240
Package publishing
Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation. |
dcharkes
approved these changes
Mar 17, 2026
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.
Summary
Fixes
cppLinkStdLib: 'c++_static'on Android not linkinglibc++abi, which causeddlopenfailures on newer Android versions with stricter linker namespace enforcement.Approach
Uses
-Wl,-Bstatic -lc++ -Wl,-Bdynamicinstead of-l c++_staticdirectly. This forces the linker to resolve-lc++using the NDK'slibc++.alinker script, which expands toINPUT(-lc++_static -lc++abi).-static-libstdc++turned out to not work because the Android NDK clang driver does not inject the C++ standard library during linking — it leaves that entirely to the build system.Changes
run_cbuilder.dart: Use-Wl,-Bstatic -lc++ -Wl,-BdynamicwhencppLinkStdLibisc++_staticon Androidctool.dart: Documentc++_staticin thecppLinkStdLibAPI docscbuilder_cross_android_test.dart: Add test that builds a C++ shared library usingstd::runtime_errorand verifies_ZTISt13runtime_erroris not an undefined symbolhelpers.dart: AddexpectSymbolNotUndefinedhelper reusingreadSymbolscxxabi.cc: Test source file that exercises libc++abi symbolsFixes #3240