cmake
build_error
ai_generated
true
CMake 错误:FetchContent:'googletest' 的内容已经被填充。请使用 FETCHCONTENT_FULLY_DISCONNECTED 或清除填充目录。
CMake Error: FetchContent: Content for 'googletest' has already been populated. Use FETCHCONTENT_FULLY_DISCONNECTED or clear the population directory.
ID: cmake/fetchcontent-already-populated
80%修复率
88%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.14+ | active | — | — | — |
| googletest 1.12.0 | active | — | — | — |
根因分析
FetchContent 被多次调用(例如在不同子目录中)未加适当保护,或之前的配置留下了过时的填充状态。
English
FetchContent is called multiple times for the same dependency (e.g., in different subdirectories) without proper guards, or a previous configure left stale population state.
官方文档
https://cmake.org/cmake/help/latest/module/FetchContent.html解决方案
-
在 CMakeLists.txt 中保护 FetchContent 调用:if(NOT googletest_POPULATED) FetchContent_Declare(googletest ...) FetchContent_MakeAvailable(googletest) endif()。
-
清理 FetchContent 填充目录:在构建目录中执行 rm -rf _deps/googletest*,然后重新配置。
-
在 FetchContent_Declare 中使用 OVERRIDE_FIND_PACKAGE 选项让 find_package 处理:FetchContent_Declare(googletest GIT_REPOSITORY ... OVERRIDE_FIND_PACKAGE)。
无效尝试
常见但无效的做法:
-
Delete the build directory and reconfigure from scratch
80% 失败
Temporary fix; if the root cause (duplicate FetchContent calls) isn't fixed, it will happen again on next clean configure.
-
Add set(FETCHCONTENT_FULLY_DISCONNECTED ON) globally
50% 失败
This prevents any new downloads but may cause stale content if the dependency version changes.
-
Wrap each FetchContent_Declare in if(NOT <name>_POPULATED) but forget to call FetchContent_MakeAvailable
90% 失败
Missing MakeAvailable means the content isn't populated; the error persists.