我正在嘗試在 Ubuntu 18.04 上使用 CMake 編譯一個簡單的 C 程式,但是當我運行該make命令時,我的所有 CMake 專案都失敗了。下面是一個最小的作業示例。
目錄結構如下所示:
- project directory
|-build
|-main.cpp
|-CMakeLists.txt
主程式
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.1g --version:g (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0g -print-prog-name=cc1plus:/usr/lib/gcc/x86_64-linux-gnu/7/cc1plusuname -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
uj5u.com熱心網友回復:
盡管關于缺席錯誤訊息.d檔案似乎是內部向CMake(例如檔案用于由編譯器生成收集頭的依賴關系),它的通常的原因是指定一些輸出控制編譯器選項中CMakeLists.txt。
在您的情況下,它是-o損壞 CMake 生成的命令列的選項。CMake 本身使用此選項來指定將作為編譯結果創建的目標檔案。所以添加另一個-o是錯誤的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382638.html
