GCC的-g选项应该在编译阶段起作用(转)

GCC的-g选项应该在编译阶段起作用(转)[@more@][table=95%][tr][td]编译过程:
源代码.c --&gt 预处理(把头文件.h纳入,预处理之后生成的是.i文件) --&gt 编译(检查正确后生成.s汇编代码文件) --&gt 汇编处理(将编译阶段生成的.s文件转换成目标文件.o) --&gt 链接(生成可执行文件)
[/td][/tr][/table]上面是GCC编译过程的分析。昨天练习建立多个文件的project,但是写好Makefile之后,使用GDB调试时发现问题,具体如下:
[table=95%][tr][td] [armlinux@lqm The-C-Programming-Language]$ cd example3
[armlinux@lqm example3]$ ls
Makefile copy.o getline.c main.c zifu
copy.c gc.h getline.o main.o
[armlinux@lqm example3]$ gdb zifu
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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-redhat-linux-gnu"...
(gdb) l
1 ../sysdeps/i386/elf/start.S: No such file or directory.
in ../sysdeps/i386/elf/start.S

[/td][/tr][/table]
也就是说无法list。原因是没有加入-g选项。原来的Makefile文件如下:
[table=95%][tr][td] CC = gcc

OBJS = main.o getline.o copy.o

zifu: $(OBJS)

$(CC) $^ -o $@

main.o:main.c gc.h

$(CC) -c $<

getline.o:getline.c

$(CC) -c $<

copy.o:copy.c

$(CC) -c $<

.PHONY:clean

clean:

rm -f $(OBJS) zifu


[/td][/tr][/table]那么-g选项具体加在那个过程呢?实际上,-g选项应该在编译阶段加入,在生成的汇编代码文件中就有了调试信息,否则就会出现start.S找不到的情况了。所以,修改了Makefile文件如下:

[table=95%][tr][td] CC = gcc

OBJS = main.o getline.o copy.o

zifu: $(OBJS)

$(CC) $^ -o $@

main.o:main.c gc.h

$(CC) -g -c $<

getline.o:getline.c

$(CC) -g -c $<

copy.o:copy.c

$(CC) -g -c $<

.PHONY:clean

clean:

rm -f $(OBJS) zifu


[/td][/tr][/table]然后就可以调试了,如下所示:

[table=95%][tr][td] [armlinux@lqm example3]$ gdb zifu
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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-redhat-linux-gnu"...
(gdb) l
9
10 /*print the longest input line*/
11 int main()
12 {
13 int len; /*current line length*/
14 int max; /*maximum length seen so far*/
15 char line[MAXLINE]; /*current input line*/
16 char longest[MAXLINE]; /*longest line saved here*/
17
18 max = 0

[/td][/tr][/table]----------------------------------------
附:实例源代码
[table=95%][tr][td] [armlinux@lqm example3]$ tree
.
|-- Makefile
|-- copy.c
|-- gc.h
|-- getline.c
`-- main.c

0 directories, 5 files

[/td][/tr][/table]
[table=95%][tr][td] /*
*copy.c
*copy 'from' into 'to';assume to is big enough
*/


void copy(char to[],char from[])
{
int i;

i = 0;
while ((to[i] = from[i]) != '')
++i;
}

[/td][/tr][/table]
[table=95%][tr][td] /*
*getline.c
*read a line into s,return length
*/


#include <stdio.h>

int getline(char s[],int lim)
{
int c,i;

for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != ' ' ; ++i)
s[i] = c;
if (c == ' ') {
s[i] = c;
++i;
}
s[i]='';
return i;
}

[/td][/tr][/table]
[table=95%][tr][td] /*
*main.c
*/


#include <stdio.h>
#include "gc.h"

#define MAXLINE 1000 /*maximum input line length*/

/*print the longest input line*/
int main()
{
int len; /*current line length*/
int max; /*maximum length seen so far*/
char line[MAXLINE]; /*current input line*/
char longest[MAXLINE]; /*longest line saved here*/

max = 0;
while ((len = getline(line,MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest,line);
}
if (max > 0)
printf("%s",longest);
return(0);
}

[/td][/tr][/table]
[table=95%][tr][td] /*
*gc.h
*/
int getline(char [],int);
void copy(char [],char []);

[/td][/tr][/table]这个project很短小,但是也说明了如何建立多个源文件的project。其中,公用的宏定义、自定义的函数声明、自定义的结构体变量都放在自定义头文件里。事先要规划好程序结构!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10617542/viewspace-960165/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10617542/viewspace-960165/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值