cmake build_error ai_generated true

CMake Error: FetchContent: Content for 'googletest' has already been populated. Use FETCHCONTENT_FULLY_DISCONNECTED or clear the population directory.

ID: cmake/fetchcontent-already-populated

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CMake 3.14+ active
googletest 1.12.0 active

Root Cause

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.

generic

中文

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

Official Documentation

https://cmake.org/cmake/help/latest/module/FetchContent.html

Workarounds

  1. 95% success In CMakeLists.txt, guard FetchContent calls: if(NOT googletest_POPULATED) FetchContent_Declare(googletest ...) FetchContent_MakeAvailable(googletest) endif().
    In CMakeLists.txt, guard FetchContent calls: if(NOT googletest_POPULATED) FetchContent_Declare(googletest ...) FetchContent_MakeAvailable(googletest) endif().
  2. 85% success Clean the FetchContent population directory: rm -rf _deps/googletest* in the build directory, then reconfigure.
    Clean the FetchContent population directory: rm -rf _deps/googletest* in the build directory, then reconfigure.
  3. 80% success Use OVERRIDE_FIND_PACKAGE option in FetchContent_Declare to let find_package handle it: FetchContent_Declare(googletest GIT_REPOSITORY ... OVERRIDE_FIND_PACKAGE).
    Use OVERRIDE_FIND_PACKAGE option in FetchContent_Declare to let find_package handle it: FetchContent_Declare(googletest GIT_REPOSITORY ... OVERRIDE_FIND_PACKAGE).

中文步骤

  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)。

Dead Ends

Common approaches that don't work:

  1. Delete the build directory and reconfigure from scratch 80% fail

    Temporary fix; if the root cause (duplicate FetchContent calls) isn't fixed, it will happen again on next clean configure.

  2. Add set(FETCHCONTENT_FULLY_DISCONNECTED ON) globally 50% fail

    This prevents any new downloads but may cause stale content if the dependency version changes.

  3. Wrap each FetchContent_Declare in if(NOT <name>_POPULATED) but forget to call FetchContent_MakeAvailable 90% fail

    Missing MakeAvailable means the content isn't populated; the error persists.