GCC的-g選項應該在編譯階段起作用(轉)

BSDLite發表於2007-08-15
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章