GCC
GCC是一套由GNU开发的编程语言编译器,它是一套GNU编译器套装。GCC原本作为GNU操作系统的官方编译器,现已被大多数类UNIX操作系统(如Linux、BSD、Mac、OS等)采纳为标准的编译器,GCC同样适用于微软的Windows。
GCC原本只能处理C语言,但后来得到扩展,变得既可以处理C++,又可以处理Fortran、Pascal、Objective-C、java、以及Ada与其他语言。
1、安装GCC
(1)、检查是否安装GCC
[root@waiwai ~]# rpm -qa|grep gcc
gcc-c++-4.8.5-28.el7.x86_64
libgcc-4.8.5-28.el7.x86_64
gcc-4.8.5-28.el7.x86_64
gcc-gfortran-4.8.5-28.el7.x86_64
//表示已安装,如果系统还没有安装,就用yum命令安装
yum clean all //安装前先清除缓存
yum install gcc -y
所有软件包安装完毕后,可以使用rpm命令在查询一次
rpm -qa|grep gcc
2、单一程序
1、编辑程序代码,即源码
例:
[root@waiwai /he/cheng]# vim hello.c //用C语言写的程序扩展名建议用.c
#include <stdio.h> //这个“#” 不是注解
int main (void)
{
printf ("Hello World\n");
}
####2、开始编译与测试运行
[root@waiwai /he/cheng]# gcc hello.c
[root@waiwai /he/cheng]# ll hello.c a.out
-rwxr-xr-x 1 root root 8440 Apr 14 14:40 a.out //此时会生成这个文件名
-rw-r--r-- 1 root root 66 Apr 14 14:40 hello.c
[root@waiwai /he/cheng]# ./a.out
Hello World //结果出现
在默认状态下,如果我们直接以GCC编译源码,并且没有加上任何参数,则执行文件的文件名就会被自动设置为a.out这个文件名。所以就能直接执行 ./a.out这个执行文件。
如果想要生成目标文件来进行其他操作,且执行文件的文件名不默认
[root@waiwai /he/cheng]# gcc -c hello.c
[root@waiwai /he/cheng]# ll hello*
-rw-r--r-- 1 root root 66 Apr 14 14:40 hello.c
-rw-r--r-- 1 root root 1496 Apr 14 15:03 hello.o //这就是生成的目标文件
[root@waiwai /he/cheng]# gcc -o hello hello.o
[root@waiwai /he/cheng]# ll hello*
-rwxr-xr-x 1 root root 8440 Apr 14 15:05 hello //这就是可执行文件(-o的结果)
-rw-r--r-- 1 root root 66 Apr 14 14:40 hello.c
-rw-r--r-- 1 root root 1496 Apr 14 15:03 hello.o
[root@waiwai /he/cheng]# ./hello
Hello World
主程序、子程序链接、子程序的编译
如果我们在一个主程序里又调用另一个子程序
以thinks.c这个主程序去调用thinks——2.c
1、撰写所需要的主程序、子程序
[root@waiwai /he/cheng]# vim thinks.c
#include <stdio.h>
int main (void)
{
printf("Hello World\n");
thinks_2();
}
#上面thinks_2(); 那一行就是调用子程序!
[root@waiwai /he/cheng]# vim thinks_2.c
#include <stdio.h>
void thanks_2(void)
{
printf("Think you!\n");
}
2、进行程序的编译与链接(link)
1、开始将源码编译成为可执行的binary file。
[root@waiwai /he/cheng]# gcc -c thinks.c thinks_2.c
[root@waiwai /he/cheng]# ll thinks*
-rw-r--r-- 1 root root 68 Apr 14 15:16 thinks_2.c
-rw-r--r-- 1 root root 1504 Apr 14 15:19 thinks_2.o //编译生成的目标文件
-rw-r--r-- 1 root root 78 Apr 14 15:12 thinks.c
-rw-r--r-- 1 root root 1560 Apr 14 15:19 thinks.o //编译生成的目标文件
[root@waiwai /he/cheng]# gcc -o thinks thinks.o thinks_2.o
[root@waiwai /he/cheng]# ll thinks*
-rw-r--r-- 1 root root 4870 Apr 14 15:19 thinks 最终结果会生成可执行文件
2、执行可执行文件
[root@waiwai /he/cheng]# ./thanks
Hello World
Think you!
*****注:如果有一天你升级了thinks_2.c这个文件内容,则你只需要编译thinks_2.c来产生新的thinks_2.o,然后再以链接制作成新的binary可执行文件,而不必重新编译其他没有改动过的源码文件。这对于软件开发者来说,是一个很重要的功能,因为有时候要将诺大的源码编译完成,会花很长一段时间。
网友评论