gcc及gdb基础(转载)

来源:互联网 发布:linux 修改账户密码 编辑:程序博客网 时间:2024/06/04 18:36

复习一下Gcc and Gdb的基础知识吧(转载)

GCC编译选项及其功能

-L
定义连接库文件的目录

-I
定义 C源码的头文件的目录

-o
后面跟的参数为要生成的执行文件的名

-O
进行编译优化,可以指定使用不同的优化级别,从 O2O6,每个不同的级别使用的优化设置不同。

相关的选项还有定义生成的指令码类型的参数,如 -m486生成486指令,缺省的gcc版本(2.7.2)不支持Pentium代码。

-g
加入调试代码,可以在完成后使用 strip命令删除用于调试的信息

-c
仅仅进行编译而不进行连接,生成目标文件

-fPic
生成相对地址的代码,可用于最后生成动态连接库

-static
强制生成静态连接的程序

-aout
生成 a.out格式的执行文件、目标代码等,缺省使用ELF格式

-elf
3.0
之后为缺省设置,生成ELF格式的目标和执行代码


-ansi

只支持 ANSI 标准的 C 语法。这一选项将禁止 GNU C 的某些特色,例如 asm typeof 关键词。


-DMACRO

以字符串“1”定义 MACRO 宏。

-DMACRO=DEFN

以字符串“DEFN”定义 MACRO 宏。

-E

只运行 C 预编译器。


-IDIRECTORY

指定额外的头文件搜索路径DIRECTORY

-LDIRECTORY

指定额外的函数库搜索路径DIRECTORY

-lLIBRARY

连接时搜索指定的函数库LIBRARY


-shared

生成共享目标文件。通常用在建立共享库时。

-UMACRO

取消对 MACRO 宏的定义。

-w

不生成任何警告信息。

-Wall

生成所有警告信息。


GDB的使用

cat >tst.c

#include<stdio.h>
int func(int n)

{

int sum=0,i;

for(i=0;i<100;i++)

{

sum+=i;

}

return sum;

}
main()

{

int i;

long result =0;

for(i=1;i<=100;i++)
{
result+=i;
}

printf("result[1-100]=%d/n",result);

printf("result[1-250]=%d/n",func(250));

}

编译生成执行文件:(Linux下)
hchen/test> cc -g tst.c -o tst

使用GDB调试:

hchen/test> gdb tst <---------- 启动GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) l <-------------------- l
命令相当于list,从第一行开始例出原码。
#include<stdio.h>
int func(int n)

{

int sum=0,i;

for(i=0;i<100;i++)

{

sum+=i;

}

return sum;

}
main()

{

int i;

long result =0;

for(i=1;i<=100;i++)

{

result+=i;

}
printf("result[1-100]=%d/n",result);

printf("result[1-250]=%d/n",func(250));

}
Breakpoint 1, main () at tst.c:17 <----------
在断点处停住。
17 long result = 0;
(gdb) n <---------------------
单条语句执行,next命令简写。
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) c <---------------------
继续运行程序,continue命令简写。
Continuing.
result[1-100] = 5050 <----------
程序输出。

Breakpoint 2, func (n=250) at tst.c:5
5 int sum=0,i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p i <---------------------
打印变量i的值,print命令简写。
$1 = 134513808
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8 sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt <---------------------
查看函数堆栈。
#0 func (n=250) at tst.c:5
#1 0x080484e4 in main () at tst.c:24
#2 0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish <---------------------
退出函数。
Run till exit from #0 func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24 printf("result[1-250] = %d /n", func(250) );
Value returned is $6 = 31375
(gdb) c <---------------------
继续运行。
Continuing.
result[1-250] = 31375 <----------
程序输出。

Program exited with code 027. <--------程序退出,调试结束。
(gdb) q <---------------------
退出gdb
hchen/test>

原创粉丝点击