CMake fatal error: CMakeFiles/<path>.dir/main.cpp.d: No such file or directory

CMake fatal error: CMakeFiles/<path>.dir/main.cpp.d: No such file or directory

我正在尝试在 Ubuntu 18.04 上使用 CMake 编译一个简单的 C++ 程序,但是当我 运行 make 命令时,我所有的 CMake 项目都失败了。 下面是一个最小的工作示例。

目录结构如下所示:

- project directory
|-build
|-main.cpp
|-CMakeLists.txt

main.cpp

int main(void)
{
    return 0;
}

CMakeLists.txt

cmake_minimum_required (VERSION 3.1)
project(Test-Project)

add_executable(a
    main.cpp
)
target_compile_options(a
    PUBLIC -Wall -o -std=c++11
)

建筑

cd build
cmake ../ # this works without any error
make  # this fails

错误

[ 50%] Building CXX object CMakeFiles/a.dir/main.cpp.o
cc1plus: fatal error: CMakeFiles/a.dir/main.cpp.d: No such file or directory
compilation terminated.
CMakeFiles/a.dir/build.make:75: recipe for target 'CMakeFiles/a.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/a.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/a.dir/all' failed
make[1]: *** [CMakeFiles/a.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

尝试在系统上编译任何基于 CMake 的程序时出现此错误。 但是,如果我直接使用 g++ 来编译程序,它会毫无怨言地编译。 例如:

g++ ../main.cpp

编译程序,运行程序没有任何错误。

编辑

使用make VERBOSE=1编译时的终端输出:

/home/kani/.local/lib/python2.7/site-packages/cmake/data/bin/cmake -S/home/kani/Documents/test -B/home/kani/Documents/test/build --check-build-system CMakeFiles/Makefile.cmake 0
/home/kani/.local/lib/python2.7/site-packages/cmake/data/bin/cmake -E cmake_progress_start /home/kani/Documents/test/build/CMakeFiles /home/kani/Documents/test/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/kani/Documents/test/build'
make  -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/depend
make[2]: Entering directory '/home/kani/Documents/test/build'
cd /home/kani/Documents/test/build && /home/kani/.local/lib/python2.7/site-packages/cmake/data/bin/cmake -E cmake_depends "Unix Makefiles" /home/kani/Documents/test /home/kani/Documents/test /home/kani/Documents/test/build /home/kani/Documents/test/build /home/kani/Documents/test/build/CMakeFiles/a.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/kani/Documents/test/build'
make  -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/build
make[2]: Entering directory '/home/kani/Documents/test/build'
[ 50%] Building CXX object CMakeFiles/a.dir/main.cpp.o
/usr/bin/c++   -Wall -o -std=c++11 -MD -MT CMakeFiles/a.dir/main.cpp.o -MF CMakeFiles/a.dir/main.cpp.o.d -o CMakeFiles/a.dir/main.cpp.o -c /home/kani/Documents/test/main.cpp
cc1plus: fatal error: CMakeFiles/a.dir/main.cpp.d: No such file or directory
compilation terminated.
CMakeFiles/a.dir/build.make:75: recipe for target 'CMakeFiles/a.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/a.dir/main.cpp.o] Error 1
make[2]: Leaving directory '/home/kani/Documents/test/build'
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/a.dir/all' failed
make[1]: *** [CMakeFiles/a.dir/all] Error 2
make[1]: Leaving directory '/home/kani/Documents/test/build'
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

尽管关于缺少 .d 文件的错误消息似乎是 CMake 内部的(此类文件用于收集编译器生成的 header 依赖项),其通常的原因是指定一些 output-controlling编译器选项CMakeLists.txt.

在您的情况下,-o 选项损坏了 CMake 生成的命令行。 CMake 本身使用此选项指定 object 文件,该文件将作为编译结果创建。所以再加一个-o是错误的。