CMake 错误:目标 'myapp' 链接到目标 'mylib',后者使用 GLIBCXX_USE_CXX11_ABI=0,但 myapp 使用 GLIBCXX_USE_CXX11_ABI=1。这是 ABI 不兼容问题。
CMake Error: The target 'myapp' links to target 'mylib' which uses GLIBCXX_USE_CXX11_ABI=0, but myapp uses GLIBCXX_USE_CXX11_ABI=1. This is an ABI incompatibility.
ID: cmake/glibcxx-use-cxx11-abi-mismatch
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.16 | active | — | — | — |
| 3.21 | active | — | — | — |
| 3.24 | active | — | — | — |
| 3.28 | active | — | — | — |
根因分析
项目链接了使用不同 C++ 标准库 ABI 版本(旧版与新版 std::string 布局)编译的库,导致链接时符号不兼容。
English
A project links libraries compiled with different C++ standard library ABI versions (old vs new std::string layout), causing symbol incompatibility at link time.
官方文档
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html解决方案
-
确保所有依赖项使用相同的 ABI 设置。使用 -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=1 重新编译 mylib,或使用 -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0 重新编译 myapp 以匹配 mylib。
-
在 CMakeLists.txt 中使用 target_compile_definitions(mylib INTERFACE _GLIBCXX_USE_CXX11_ABI=0) 或为 myapp 使用相应设置,以一致地传播 ABI 选择。
-
如果使用 vcpkg 或 conan 等包管理器,请确保 triplet 或 profile 在所有库中统一设置 ABI 标志。
无效尝试
常见但无效的做法:
-
Adding -D_GLIBCXX_USE_CXX11_ABI=0 to CMAKE_CXX_FLAGS for all targets
70% 失败
This forces the old ABI for all targets, but if system libraries (e.g., Boost) are compiled with the new ABI, the mismatch persists.
-
Manually editing the generated Makefile to remove conflicting flags
95% 失败
The Makefile is regenerated by CMake on each run, so manual edits are overwritten and not portable across builds.
-
Setting CMAKE_CXX_STANDARD to a lower value to avoid C++11 ABI
90% 失败
The ABI mismatch is independent of the C++ standard version; it relates to the std::string implementation, not the language standard.