三十六. vim配置文件
三十七. gcc工作流程
//在hello文件夹中,已经写好了名为hello.c的文件
[root@VM_0_15_centos home]# cd hello
[root@VM_0_15_centos hello]# ls
hello.c
//方法一:
//首先预处理
[root@VM_0_15_centos hello]# gcc -E hello.c -o hello.i
[root@VM_0_15_centos hello]# ls
hello.c hello.i
[root@VM_0_15_centos hello]# vi hello.i
//然后编译c文件成会汇编文件
[root@VM_0_15_centos hello]# gcc -S hello.i -o hello.s
[root@VM_0_15_centos hello]# vi hello.s
//然后把汇编文件,变成二进制文件
[root@VM_0_15_centos hello]# gcc -c hello.s -o hello.o
[root@VM_0_15_centos hello]# vi hello.o
//最后进行链接
[root@VM_0_15_centos hello]# gcc hello.o -o hello
[root@VM_0_15_centos hello]# ls
hello hello.c hello.i hello.o hello.s
//方法二:
//可以直接如下所示,但不指定名字的话,默认生成名为a.out的可执行文件
[root@VM_0_15_centos hello]# gcc hello.o
[root@VM_0_15_centos hello]# ls
a.out hello hello.c hello.i hello.o hello.s
//方法三,可以指定生成的可执行文件的名字
[root@VM_0_15_centos hello]# gcc hello.c -o result
[root@VM_0_15_centos hello]# ls
a.out hello.c hello.i hello.o hello.s result
[root@VM_0_15_centos hello]# ./result
hello world[root@VM_0_15_centos hello]#
在这四个流程里,最花费时间的,是编译过程,也就是将预处理后的c文件,变为汇编文件这个过程
三十八. gcc的参数 -l
首先我们有如图所示文件目录结构
[root@VM_0_15_centos home]# tree experiment/
experiment/
|-- include
| `-- head.h
`-- sum.c
1 directory, 2 files
然后这里的sum.c
#include
#include "head.h"
#define DEBUG
int main()
{
int a = NUM1;
int b = NUM2;
int sum = a+b;
#ifdef DEBUG
printf("The sum value is:%d + %d =%d\n",a,b,sum);
#endif
return 0;
}
这里的head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define NUM1 10
#define NUM2 20
#endif
接下来我们试着用gcc编译
[root@VM_0_15_centos experiment]# ls
include sum.c
[root@VM_0_15_centos experiment]# gcc sum.c -o sum
sum.c:2:18: fatal error: head.h: No such file or directory
#include "head.h"
^
compilation terminated.
//上面没有引入参数I来指定头文件所在目录,这里改正后就ok了
[root@VM_0_15_centos experiment]# gcc sum.c -I ./include/ -o sum
[root@VM_0_15_centos experiment]# ls
include sum sum.c
[root@VM_0_15_centos experiment]# ./sum
The sum value is:10 + 20 =30
三十九. gcc的参数
-c参数的使用
[root@VM_0_15_centos experiment]# ls
include sum sum.c
[root@VM_0_15_centos experiment]# gcc sum.c -c -I ./include/
[root@VM_0_15_centos experiment]# ls
include sum sum.c sum.o
[root@VM_0_15_centos experiment]# file sum.o
sum.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
[root@VM_0_15_centos experiment]# file sum.c
sum.c: C source, ASCII text
[root@VM_0_15_centos experiment]# file sum
sum: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=63370d37cd31aa192241fcfaa8194e9eccd028e2, not stripped
-g参数的使用
显然当加入调试的-g参数后,最终文件大了一些
比如多一个d的,有调试模式的,会大很多
接下来我们去使用gcc的-D参数
#include
#include "head.h"
//这里我们注释掉了下面这行,以演示gcc中的-D参数
//#define DEBUG
int main()
{
int a = NUM1;
int b = NUM2;
int sum = a+b;
#ifdef DEBUG
printf("The sum value is:%d + %d =%d\n",a,b,sum);
#endif
return 0;
}
[root@VM_0_15_centos experiment]#
然后我们运行一下,就知道-D的意义在于让宏强行生效,所以多用于测试阶段
[root@VM_0_15_centos experiment]# ls
include sum sum.c sum.o sum_plus
[root@VM_0_15_centos experiment]# gcc sum.c -I ./include/ -o sum1
[root@VM_0_15_centos experiment]# ls
include sum sum1 sum.c sum.o sum_plus
[root@VM_0_15_centos experiment]# ./sum1
[root@VM_0_15_centos experiment]# gcc sum.c -I ./include/ -o sum2 -D DEBUG
[root@VM_0_15_centos experiment]# ls
include sum sum1 sum2 sum.c sum.o sum_plus
[root@VM_0_15_centos experiment]# ./sum2
The sum value is:10 + 20 =30
接下来我们看一下-Wall参数
首先把sum.c改成有warning的情况
#include
#include "head.h"
//这里我们注释掉了下面这行,以演示gcc中的-D参数
//#define DEBUG
int main()
{
int a = NUM1;
int b = NUM2;
//This line is writen to show the -Wall parameter in gcc
int aa;
int sum = a+b;
#ifdef DEBUG
printf("The sum value is:%d + %d =%d\n",a,b,sum);
#endif
return 0;
}
然后在gcc中使用-Wall 参数
[root@VM_0_15_centos experiment]# vi sum.c
[root@VM_0_15_centos experiment]# ls
include sum sum1 sum2 sum.c sum.o sum_plus
[root@VM_0_15_centos experiment]# gcc sum.c -I ./include/ -D DEBUG -o sum_wall -Wall
sum.c: In function ‘main’:
sum.c:12:6: warning: unused variable ‘aa’ [-Wunused-variable]
int aa;
^
[root@VM_0_15_centos experiment]#
四十. 库的使用