Ubuntu 运行 C 代码 - 常见错误及解决方法
在 Ubuntu 环境下运行 C 代码时,可能会遇到各种错误。例如,以下代码在编译时可能会遇到问题:
#include 'stdio.h'
int main
printf('hello world!12345678900000\n'); return 0;
}
错误 1:缺少标准库文件
该错误可能是因为缺少了标准库文件,需要在编译命令中添加 -lc 参数,即:
aarch64-linux-gnu-gcc-11 test.c -static -lc
错误 2:main 函数语法错误
另外,main 函数的语法有误,应该加上大括号包围语句块,即:
int main() {
printf('hello world!12345678900000\n');
return 0;
}
经过修改后,代码应该如下:
#include 'stdio.h'
int main() {
printf('hello world!12345678900000\n');
return 0;
}
现在就可以使用以下命令编译并运行代码了:
aarch64-linux-gnu-gcc-11 test.c -static -lc
./a.out
如果仍然遇到问题,请检查代码是否正确,并仔细阅读编译器输出的错误信息。
原文地址: https://www.cveoy.top/t/topic/oQdP 著作权归作者所有。请勿转载和采集!