# CMake 错误：目标 'myapp' 链接到目标 'mylib'，后者使用 GLIBCXX_USE_CXX11_ABI=0，但 myapp 使用 GLIBCXX_USE_CXX11_ABI=1。这是 ABI 不兼容问题。

- **ID:** `cmake/glibcxx-use-cxx11-abi-mismatch`
- **领域:** cmake
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

项目链接了使用不同 C++ 标准库 ABI 版本（旧版与新版 std::string 布局）编译的库，导致链接时符号不兼容。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.16 | active | — | — |
| 3.21 | active | — | — |
| 3.24 | active | — | — |
| 3.28 | active | — | — |

## 解决方案

1. ```
   确保所有依赖项使用相同的 ABI 设置。使用 -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=1 重新编译 mylib，或使用 -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0 重新编译 myapp 以匹配 mylib。
   ```
2. ```
   在 CMakeLists.txt 中使用 target_compile_definitions(mylib INTERFACE _GLIBCXX_USE_CXX11_ABI=0) 或为 myapp 使用相应设置，以一致地传播 ABI 选择。
   ```
3. ```
   如果使用 vcpkg 或 conan 等包管理器，请确保 triplet 或 profile 在所有库中统一设置 ABI 标志。
   ```

## 无效尝试

- **Adding -D_GLIBCXX_USE_CXX11_ABI=0 to CMAKE_CXX_FLAGS for all targets** — This forces the old ABI for all targets, but if system libraries (e.g., Boost) are compiled with the new ABI, the mismatch persists. (70% 失败率)
- **Manually editing the generated Makefile to remove conflicting flags** — The Makefile is regenerated by CMake on each run, so manual edits are overwritten and not portable across builds. (95% 失败率)
- **Setting CMAKE_CXX_STANDARD to a lower value to avoid C++11 ABI** — The ABI mismatch is independent of the C++ standard version; it relates to the std::string implementation, not the language standard. (90% 失败率)
