# CMake 错误：FetchContent：'googletest' 的内容已经被填充。请使用 FETCHCONTENT_FULLY_DISCONNECTED 或清除填充目录。

- **ID:** `cmake/fetchcontent-already-populated`
- **领域:** cmake
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

FetchContent 被多次调用（例如在不同子目录中）未加适当保护，或之前的配置留下了过时的填充状态。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.14+ | active | — | — |
| googletest 1.12.0 | active | — | — |

## 解决方案

1. ```
   在 CMakeLists.txt 中保护 FetchContent 调用：if(NOT googletest_POPULATED) FetchContent_Declare(googletest ...) FetchContent_MakeAvailable(googletest) endif()。
   ```
2. ```
   清理 FetchContent 填充目录：在构建目录中执行 rm -rf _deps/googletest*，然后重新配置。
   ```
3. ```
   在 FetchContent_Declare 中使用 OVERRIDE_FIND_PACKAGE 选项让 find_package 处理：FetchContent_Declare(googletest GIT_REPOSITORY ... OVERRIDE_FIND_PACKAGE)。
   ```

## 无效尝试

- **Delete the build directory and reconfigure from scratch** — Temporary fix; if the root cause (duplicate FetchContent calls) isn't fixed, it will happen again on next clean configure. (80% 失败率)
- **Add set(FETCHCONTENT_FULLY_DISCONNECTED ON) globally** — This prevents any new downloads but may cause stale content if the dependency version changes. (50% 失败率)
- **Wrap each FetchContent_Declare in if(NOT <name>_POPULATED) but forget to call FetchContent_MakeAvailable** — Missing MakeAvailable means the content isn't populated; the error persists. (90% 失败率)
