GCC/GDB学习

GCC学习


1、gcc是根据后缀名来区分文件的
.c : c语言源文件
.a : 目标文件构成的库文件
.C/.cc/.cxx : c++源文件
.h : 头文件
.i : 预处理过的C源文件
.ii : 预处理过的C++源文件
.o : 编译后的目标文件
.s : 汇编语言源代码文件
.S : 经过预处理的汇编语言源代码文件

2、生成可执行文件四个步骤
预处理 :宏展开,头文件引入,
编译 :
汇编 :
连接 :

3、GCC编译选项
-o :指定可执行文件的名字,默认是a
-c :只做到编译即可,生成.o文件,汇编和连接不做
-g :添加调试信息
-O :对程序进行优化
-I :指定一个gcc可以查找头文件的目录 gcc -Ixx/xx/ xx.c -o xx
-l :
-static :静态连接,每一个程序都只有一份链接库的拷贝(程序和库文件合在一起)
和动态连接不同,动态连接是多个程序共享一份链接库的拷贝
默认采用的是动态连接
-Wall :生成所有的警告信息
-w :不生成任何警告信息
-D :定义宏比如gcc -DN test.c -o test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
//#include "test.h"
//#define N

int bar(int c, int d)
{
    int e = c + d;
    printf("sum is %d",e);
    return e;
}
int foo(int a, int b)
{
    return bar(a, b);
}
int main(void)
{
    int i = 0;
    foo(2, 5);

    #ifdef N
        printf("ppppppppppppppppppppppppp");
    #endif
    return 0;
}

GDB常用命令
1、编译的时候带上-g选项,如gcc -g test.c -o test
2、gdb test
3、break(b) main ===》在main函数上打上断点
4、run
5、next(n)===>F6
6、contiue(c)====>f8
7、list(l)
8、info break
9、delete 删除断点
10、step(s)===>F5
11、finish
12、watch
13、quit(q)
14、print(p)

原文地址:https://www.cnblogs.com/zsw-1993/p/4879765.html