Skip to content

[native_toolchain_c] Fix Android c++_static not linking libc++abi#3241

Merged
dcharkes merged 2 commits intodart-lang:mainfrom
blaugold:feature/android-link-cxxabi
Mar 17, 2026
Merged

[native_toolchain_c] Fix Android c++_static not linking libc++abi#3241
dcharkes merged 2 commits intodart-lang:mainfrom
blaugold:feature/android-link-cxxabi

Conversation

@blaugold
Copy link
Contributor

@blaugold blaugold commented Mar 17, 2026

Summary

Fixes cppLinkStdLib: 'c++_static' on Android not linking libc++abi, which caused dlopen failures on newer Android versions with stricter linker namespace enforcement.

Approach

Uses -Wl,-Bstatic -lc++ -Wl,-Bdynamic instead of -l c++_static directly. This forces the linker to resolve -lc++ using the NDK's libc++.a linker script, which expands to INPUT(-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,-Bdynamic when cppLinkStdLib is c++_static on Android
  • ctool.dart: Document c++_static in the cppLinkStdLib API docs
  • cbuilder_cross_android_test.dart: Add test that builds a C++ shared library using std::runtime_error and verifies _ZTISt13runtime_error is not an undefined symbol
  • helpers.dart: Add expectSymbolNotUndefined helper reusing readSymbols
  • cxxabi.cc: Test source file that exercises libc++abi symbols
  • Version bump to 0.17.6

Fixes #3240

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
Copy link
Collaborator

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

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

Speedy fixes! 🚀

@github-actions
Copy link

github-actions bot commented Mar 17, 2026

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
native_toolchain_c None 0.17.5 0.17.6 0.17.5 ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

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.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

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
@github-actions
Copy link

Package publishing

Package Version Status Publish tag (post-merge)
package:code_assets 1.0.0 already published at pub.dev
package:data_assets 0.19.6 already published at pub.dev
package:ffi 2.2.0 already published at pub.dev
package:hooks 1.0.2 already published at pub.dev
package:hooks_runner 1.1.1-wip WIP (no publish necessary)
package:native_toolchain_c 0.17.6 ready to publish native_toolchain_c-v0.17.6
package:record_use 0.6.0-wip WIP (no publish necessary)
package:swift2objc 0.2.0-wip WIP (no publish necessary)
package:swiftgen 0.1.2 already published at pub.dev

Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.

@blaugold blaugold requested a review from dcharkes March 17, 2026 12:24
@dcharkes dcharkes merged commit e5e2bed into dart-lang:main Mar 17, 2026
32 checks passed
@blaugold blaugold deleted the feature/android-link-cxxabi branch March 17, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[native_toolchain_c] cppLinkStdLib 'c++_static' on Android doesn't link libc++abi

2 participants