cmake configuration_error ai_generated true

CMake Error: The source directory /path/to/sub does not contain a CMakeLists.txt file.

ID: cmake/add-subdirectory-not-found

Also available as: JSON · Markdown
92%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3 active

Root Cause

add_subdirectory() points to a directory that lacks a CMakeLists.txt. Missing submodule, typo, or wrong path.

generic

Workarounds

  1. 95% success Initialize git submodules if the directory is a submodule
    git submodule update --init --recursive

    Sources: https://cmake.org/cmake/help/latest/command/find_package.html

  2. 90% success Check for typos in the directory path in CMakeLists.txt
    # Verify: ls -la path/to/subdirectory/CMakeLists.txt
  3. 82% success Guard with if(EXISTS) for optional subdirectories
    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/optional_sub/CMakeLists.txt)
      add_subdirectory(optional_sub)
    endif()

Dead Ends

Common approaches that don't work:

  1. Create an empty CMakeLists.txt in the subdirectory 82% fail

    An empty file silences the error but the subdirectory's targets will not be defined, causing link errors later.

  2. Use EXCLUDE_FROM_ALL option to skip the directory 78% fail

    EXCLUDE_FROM_ALL only skips default build; it does not skip configuration. CMake still needs the file to exist.