cmake build_error ai_generated true

CMake Error: In-source builds are not allowed. Please remove the CMakeCache.txt file from the source directory.

ID: cmake/in-source-build-forbidden

Also available as: JSON · Markdown · 中文
95%Fix Rate
88%Confidence
1Evidence
2023-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.13 active
3.18 active
3.20 active
3.25 active
3.27 active

Root Cause

CMake is configured to forbid building directly in the source tree, but the build directory is the same as the source directory.

generic

中文

CMake 配置为禁止在源码目录内直接构建,但构建目录与源码目录相同。

Official Documentation

https://cmake.org/cmake/help/latest/policy/CMP0011.html

Workarounds

  1. 95% success Create a separate build directory and run cmake from there: mkdir build && cd build && cmake .. && make
    Create a separate build directory and run cmake from there: mkdir build && cd build && cmake .. && make
  2. 70% success If you must build in-source, set the policy in CMakeLists.txt: cmake_policy(SET CMP0011 OLD) at the top of the file, but this is discouraged for production.
    If you must build in-source, set the policy in CMakeLists.txt: cmake_policy(SET CMP0011 OLD) at the top of the file, but this is discouraged for production.
  3. 90% success Use cmake -B <build-dir> -S <source-dir> to explicitly separate directories: cmake -B build -S . && cmake --build build
    Use cmake -B <build-dir> -S <source-dir> to explicitly separate directories: cmake -B build -S . && cmake --build build

中文步骤

  1. 创建一个单独的构建目录并从那里运行 cmake:mkdir build && cd build && cmake .. && make
  2. 如果必须进行源内构建,请在 CMakeLists.txt 开头设置策略:cmake_policy(SET CMP0011 OLD),但不建议用于生产环境。
  3. 使用 cmake -B <构建目录> -S <源码目录> 明确分离目录:cmake -B build -S . && cmake --build build

Dead Ends

Common approaches that don't work:

  1. Deleting CMakeCache.txt and re-running cmake . in the source directory 90% fail

    Deleting the cache file alone does not change the build directory location; the next cmake invocation still uses the source directory as the build directory, triggering the same error.

  2. Setting CMAKE_DISABLE_IN_SOURCE_BUILD to OFF via command line 85% fail

    This policy is typically enforced by the project's CMakeLists.txt via cmake_policy(SET CMP0011 NEW) or a project-specific guard, so overriding it externally does not work.

  3. Adding a CMakeLists.txt in the build directory 95% fail

    The build directory is not supposed to contain a CMakeLists.txt; CMake expects to find it in the source directory. This approach confuses the build system and leads to other errors.