在 CentOS 上使用 gcc 编译程序,一般步骤如下:
gcc --version
如果提示 command not found,说明还没安装。
sudo yum install gcc gcc-c++ -y
sudo dnf install gcc gcc-c++ -y
安装完成后再次确认:
gcc --version
创建文件:
vi hello.c
内容:
#include <stdio.h>
int main() {
printf("Hello CentOS!\n");
return 0;
}
保存退出(vi 中按 Esc,输入 :wq)。
gcc hello.c
会生成默认可执行文件 a.out
运行:
./a.out
gcc hello.c -o hello
运行:
./hello
C++ 文件用 g++:
g++ test.cpp -o test
./test
gcc hello.c -o hello -Wall # 显示所有警告
gcc hello.c -o hello -g # 生成调试信息(gdb用)
gcc hello.c -o hello -O2 # 优化编译
gcc main.c func.c -o app
如果你是想:
可以告诉我,我可以进一步讲。