【发布时间】:2022-01-18 15:18:40
【问题描述】:
我正在尝试在 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
编译程序,运行程序没有任何错误。
-
cmake --version:cmake version 3.22.1 -
g++ --version:g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 -
g++ -print-prog-name=cc1plus:/usr/lib/gcc/x86_64-linux-gnu/7/cc1plus -
uname -a:Linux <computer name> 5.4.0-91-generic #102~18.04.1-Ubuntu SMP <date+time> x86_64 x86_64 x86_64 GNU/Linux
编辑
使用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
【问题讨论】:
-
你能用
make VERBOSE=1构建吗? -
谢谢,我刚刚用输出编辑了我的原始帖子。
-
选项
-o指定编译后要创建的目标文件。 CMake 自动设置此选项。不要在 CMake 文件中设置-o选项:那样会损坏 CMake 准备的命令行。 -
谢谢@Tsyvarev。就是这样。删除
-o修复了它。我很困惑,因为这是在另一台计算机上编译的。那个使用CMake 3.10.2。失败的使用CMake 3.22.1。可能与版本有关。无论如何,非常感谢! -
不相关:
set(CMAKE_CXX_STANDARD 11)做同样的事情但可移植。