在 Debian 上使用 gcc 编译出错时,可按下面思路逐步排查和解决。
编译报错一般分几类:
bash: gcc: command not found
原因:没装 gcc
解决:
sudo apt update
sudo apt install build-essential
build-essential 包含 gcc、g++、make 等。
fatal error: xxx.h: No such file or directory
原因:
解决:
# 例如缺少 pthread
sudo apt install manpages-dev
# 查找头文件所属包
apt-file search xxx.h
(若没装 apt-file:sudo apt install apt-file && apt-file update)
cannot find -lxxx
原因:没装对应的 -dev 库
解决:
sudo apt install libxxx-dev
例如:
sudo apt install libssl-dev
undefined reference to `func`
原因:
解决:
gcc main.c foo.c -o app -lm
gcc test.c -o test
./test
带调试信息:
gcc -g -Wall test.c -o test
gcc --version
如果你愿意,可以把完整的报错信息贴出来(不要只贴最后一行),我可以帮你精确定位。
例如:
gcc hello.c
hello.c:5:10: fatal error: stdio.h: No such file or directory
这样我能直接告诉你缺什么、怎么装。