[table=95%][tr][td]编译过程:
源代码.c -->预处理(把头文件.h纳入,预处理之后生成的是.i文件) -->编译(检查正确后生成.s汇编代码文件) -->汇编处理(将编译阶段生成的.s文件转换成目标文件.o) -->链接(生成可执行文件)
[/td][/tr][/table]上面是GCC编译过程的分析。昨天练习建立多个文件的project,但是写好Makefile之后,使用GDB调试时发现问题,具体如下:
[table=95%][tr][td][font=新宋体][color=#0000cc][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[/font][/color]
[/td][/tr][/table]
也就是说无法list。原因是没有加入-g选项。原来的Makefile文件如下:
[table=95%][tr][td][font=新宋体]CC [color=#0000cc]= 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
[/font]
[/color]
[/td][/tr][/table]那么-g选项具体加在那个过程呢?实际上,-g选项应该在编译阶段加入,在生成的汇编代码文件中就有了调试信息,否则就会出现start.S找不到的情况了。所以,修改了Makefile文件如下:
[table=95%][tr][td][font=新宋体]CC [color=#0000cc]= 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
[/font][/color]
[/td][/tr][/table]然后就可以调试了,如下所示:
[table=95%][tr][td][font=新宋体][color=#0000cc][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[/font][/color]
[/td][/tr][/table]----------------------------------------
附:实例源代码
[table=95%][tr][td][font=新宋体][color=#0000cc][armlinux@lqm example3]$ tree
.
|-- Makefile
|-- copy.c
|-- gc.h
|-- getline.c
`-- main.c
0 directories, 5 files[/font][/color]
[/td][/tr][/table][font=新宋体][/font]
[table=95%][tr][td][font=新宋体][color=#ff9900]/*
*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]) != '\0')
++i;
}[/font][/color]
[/td][/tr][/table][font=新宋体][/font]
[table=95%][tr][td][font=新宋体][color=#ff9900]/*
*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 != '\n' ; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i]='\0';
return i;
}[/font][/color]
[/td][/tr][/table][font=新宋体][/font]
[table=95%][tr][td][font=新宋体][color=#ff9900]/*
*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);
}[/font][/color]
[/td][/tr][/table][font=新宋体][/font]
[table=95%][tr][td][color=#0000ff][font=新宋体]/*[/font][/color]
[color=#0000ff][font=新宋体] *gc.h[/font][/color]
[color=#0000ff][font=新宋体] */[/font][/color]
[font=新宋体][color=#0000ff]int getline(char [],int);
void copy(char [],char []);[/font][/color]
[/td][/tr][/table]这个project很短小,但是也说明了如何建立多个源文件的project。其中,公用的宏定义、自定义的函数声明、自定义的结构体变量都放在自定义头文件里。事先要规划好程序结构!