如何修复 CMake generation is not allowed within the source directory

问题

构建 CMake 项目时,你看到如下错误消息

cmake_run_in_build_dir.txt
CMake Error at CMakeLists.txt:844 (message):
  CMake generation is not allowed within the source directory! Remove the
  CMakeCache.txt file and try again from another folder, e.g.: rm
  CMakeCache.txt mkdir cmake-make cd cmake-make cmake ..

解决方案

此项目要求你创建单独的构建目录并从那里运行 CMake。以下是你可以这样做的方法:

  1. 为构建文件创建新目录:

    mkdir_build.sh
    mkdir build
    cd build
  2. 从新目录运行 CMake:

cmake_build.sh
cmake ..

注意是 cmake .. 而不是 cmake .,因为你希望 CMake 从父目录构建项目。

如果这不起作用,你必须删除 CMakeCache.txt 文件并重试:

remove_cmakecache.sh
rm CMakeCache.txt # if you are running from the root of the project
rm ../CMakeCache.txt # if you are running this from the build directory

然后,再次尝试运行 cmake ..


Check out similar posts by category: Build Systems