1. 确认Linux系统是否已经安装了GCC
Linux系统中一般都会自带GCC,查看方式如下所示:
gcc -v
- 例子
[necde@fedora c_workspace]$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
目标:x86_64-redhat-linux
配置为:../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.2.1-20220819/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
线程模型:posix
Supported LTO compression algorithms: zlib zstd
gcc 版本 12.2.1 20220819 (Red Hat 12.2.1-2) (GCC)
如果没有安装,可以搜索自行安装,很简单的,不管是百度还是必应还是谷歌,一定都可以很快搜索到安装的指令。
2. 编写程序
这里以一个简单的hello.c
程序来演示,至于编辑器的使用,这里直接使用vi/vim
#include <stdio.h>
int main() {
printf("Hello, world \n");
return 0;
}
3. 编译
直接编译
gcc hello.c
此时该目录下会出现一个a.out
若是多个c文件一起编译,则可以用以下的例子:
gcc test1.c test2.c -o test.out
其中-o
可以指定输出的文件名称,上边的指令会输出一个 test.out
,注意输出的文件名可以不一定以.out
结尾,但是还是建议以 .out
结尾
4. 执行
./a.out
例子:
[necde@fedora c_workspace]$ ./a.out
Hello, World
如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~