Linu系统编程实验gccgdb的使用以及Makefile文件的编写.docx
《Linu系统编程实验gccgdb的使用以及Makefile文件的编写.docx》由会员分享,可在线阅读,更多相关《Linu系统编程实验gccgdb的使用以及Makefile文件的编写.docx(16页珍藏版)》请在冰豆网上搜索。
Linu系统编程实验gccgdb的使用以及Makefile文件的编写
实验二:
gcc、gdb、Makefile的使用
实验目的:
(一)学会使用gcc编译器
(二)学会gdb调试器的使用
(三)学会编写Makefile
实验要求:
(一)编写一应用程序,使用gcc进行编译,并分别使用-o,-g,-static,-02等选项
(二)编写一应用程序,使用gdb调试,调试中使用到该小节所介绍的所有命令
(三)实现一应用程序,该程序有两个c文件构成,使用makefile来完成对该程序的编译
实验器材:
软件:
安装了Linux的vmware虚拟机
硬件:
PC机一台
实验步骤:
(一)gcc编译器
1先用vi编辑hello.c文件,内容如下:
#includeintmain(void){
priritf("helloworld\n");return0;
}
2、gcc指令的一般格式为:
gcc[选项]要编译的文件[选项][目标文件]
例:
使用gcc编译命令,编译hello.c生成可执行文件hello,并运行hello
gcc]#vihello・c
gcc]#gcchello.c-ohellogcc]#./hello
上面的命令一步由.c文件生成了可执行文件,将gcc的四个编译流程:
预处理、编译、汇编、连接一步完成,下面将介绍四个流程分别做了什么工作
3、-E选项的作用:
只进行预处理,不做其他处理。
例:
只对hello.c文件进行预处理,生成文件hello.i,并查看
[root@locdlhostgcc]#gcc-Ehello•匚-ohello*i
[root(alocalhostgcc]#Is
hellohello.c:
hello.i
通过查看可以看到头文件包含部分代码#include经过预处理阶段之后,编译器已将stdio.h的内容贴了进来。
4、-S选项的使用
-S选项的作用:
只是编译不汇编,生成汇编代码
[root^localhost[rootglocalhostnellohello*c
[root^localhost
gcc]#gcc-Shello.i-ohello.sgcc]#Is
hello.ihello,s
gcc]#|
5、-c选项的使用
-c选项的作用:
只是编译不连接,生成目标文件.0
例:
将汇编代码hello.s只编译不链接成hello.o文件
[root(alocalhostgcc]#gcc-chello.s-ohello[root(alocalhostgcc]#Is
hellohello.chello.ihello.ohello.s
6、将编译好的hello.o链接库,生成可执行文件hello
[root(alocalhost[root(alocalhosthellohello.c
[root(alocalhosthelloworld
gcc]#gcchello・o-ohellogcc]#Is
hello.ihello.ohello.sgcc]#・/hello
7、-static选项的使用
-static选项的作用:
链接静态库
例:
比较hello.c连接动态库生成的可执行文件hellol的大小
hello和链接静态库生成的可执行文件
-rwxr-
xrx
1
root
root
4641
Jun
1
03:
47
hello
-rwxr-
xr-x
1
root
root
605990
Jun
1
03:
47
hellol
-rw-r■
■「
1
root
root
75
Jun
1
03:
15
hello,c
-rw-厂.
1
root
root
18880
Jun
1
B3:
27
hello.i
-rw-r-
■■「八
1
「oot
root
844
Jun
1
03:
41
hello^o
-rw-r-
--IP--
1
root
root
416
Jun
1
03:
35
hello.s
[root@localhostgcc]#gcchello.c-ohello
[root@localhostgcc]#gcc-statichello.匚-ohellol[root@localhostgcc]#ll
total636
可以看到静态链接库的可执行文件hellol比动态链接库的可执行文件hello要大的多,
他们的执行效果是一样的
8、-g选项的使用
-g选项的作用:
在可执行程序中包含标准调试信息
例:
将hello.c编译成包含标准调试信息的可执行文件hello2
[root^localhost[root@localhosthellohello2hellolhello.c
gcc]#gcc-ghello.c-ohello2gcc]#Is
hello.ihello.e
hello»o
带有标准调试信息的可执行文件可以使用
9、-02选项的使用
gdb调试器进行调试,以便找出逻辑错误
-02选项的作用:
完成程序的优化工作
例:
将hello.c用02优化选项编译成可执行文件hello3,和正常编译产生的可执行文
[root(alocalhost[root@localhosthellohello2hellolhello3
[root(alocalhosthelloworld
[root(alocalhosthelloworld
件hello进行比较
gcc]#gcc-02hEllo.c-ohello3gcc]#Is
hello.chello.o
hello.ihello.s
gcc]#./lhello
qcc]#./Ihello3
(二)gdb调试器
1先用vi编辑文件test.c用于gdb调试器调试,内容如下
#include
intmain(void)
{
intsum(intsum);
inti,result=O;
sum(100);
for(i=1;i<=100;i++){
result+=i;
}
printf("Thesuminmainfunctionis%d\n”,result);
return0;
}
intsum(intnum)
{
inti,n=0;
for(i=0;i<=num;i++){
n+=i;
}
printf("Thesuminsumfunctionis%d\n”,n);
}
[root(alocalhostgdb]#gcc-gtest.c-otest[rootfatocalhostgdb]#Istesttest・c
3、启动gdb进行调试
l[raot^localhostgdb]#gdbtest
GNUgdbRedHatLinux(6.5-25.el5rh)
Copyright{C)2006FreeSoftwareFaundation,Inc.
GDBisfreesoftware,匚overedbytheGNUGeneralPublicLi匚enserandyouare
welcometochangeitand/ordistributecopiesofitundercertainconditions*
Type^showcopying"toseetheconditions・
Thereisabsolut已lynowarrantyforGDB*Typeh,showwarranty"fordetails.
ThisGDBwasconfiguredas''iaSG-redhat-linux-gnu1'.・・Usinghostlibthreaddblibrary"/Iib/i686/no5egneg/libthreaddb.so・1"・
[gdb)
可以看到gdb启动界面中显示了gdb的版本、自由软件等信息,然后进入了有”gdb”
开头的命令行界面
4、I(list)命令
I命令用于查看文件
(gdb)list
1
#includE<;stdio・hA
2
int
main(void)
3
{
4
intsum(ir»tsum);
5
inti,result=0;
6
sum(160);
7
for(i-l;i<=100;i++){
8
result+=i;
g
}
16
printf("Thesuminmainfunctionis%d\
n"』result);
(gdb)
1
11
厂etu厂nG;
12
}
13
int
sum(intnum)
可以看到每行代码面前都有对应的行号,这样方便我们设置断点。
5、b(breakpoint)命令
b用于设置断点,断点调试时调试程序的一个非常重要的手段,设置方法:
在”b”命
令之后加上对应的行号,如下图
(gdb)b6
Breakpoint2at0x804839c:
filetest.c,line6.
在gdb中可以设置多个断点。
代码运行时会到断点对应的行之前暂停,上图中,代码就
会运行到第7行之前暂停(并没有运行第7行)。
6、info命令
info命令用于查看断点情况,设置好断点后可以用它来查看
7、r(run)命令
(gdb)b7
Breakpoint1at0x8O483a8:
filetest.c,line7.
(gdb)infob
NumTypeDispEnbAddressWhat
1breakpointkeepy0xG8O483a8inmain
attest.c:
7(gdb)|
r命令用于运行代码,默认是从首行开始运行,也可以在r后面加上行号,从程序中指
定行开始运行。
(gdb)「
Startingprogram:
/home/LinuxC/test/gdb/test
Thesuminsumfunctionis5050
Breakpoint1,main()attest•c:
7
7for(i=l;i<=100;i++){
(gdb)|
可以看到程序运行到断点处就停止了
8、p(print)命令
p命令用于查看变量的值,在调试的时候我们经常要查看某个变量当前的值与我们逻辑
设定的值是否相同,输入p+变量名即可
(gdb)presult
$1=0
(gdb)pnum
Nosymbol''num'1incurrentcontext(
(gdb)pi
$2=2420724
(gdb)|
可以看到result在第6行已被赋值为零,而i目前还没有被赋值所以是一个随机数,
在主函数里看不到num的值,只有进入子函数才能看到
9、s(step)命令
s命令用于单步运行,另外n(next)命令也用于单步运行,他们的区别在于:
如果有函数调用的时候,s会进入该函数而n不会进入该函数。
Breakpoint2,main()attest.c:
6
6sum(lOO);
(gdb)si
sum(num=100)attest.c:
15
15int=0;
(gdb)|
(gdb)pnum
$3=100
(gdb)|
可以看到进入了sum子函数,这时候就能看到num的值为100。
10、n(next)命令
n命令用于单步运行,下面是n命令的使用:
Breakpoint2*main()attest.c:
6
6sum(100);
(gdb)n
Thesuminsumfunctionis505G
7for(i-l;i<=100;i++){(gdb)I
和s命令的运行效果对比会发现,使用n命令后,程序显示函数sum的运行结果并向下执行,而使用s命令后则会进入到sum函数之中单步运行
11、finish命令
finish命令用于运行程序,直到当前函数结束。
例如我们进入了sum函数,使用finish命令的情况
Breakpoint2tmain()attest,c:
6
Thesuminsumfunctioni55056main()attest.c:
7
7for(i=l;i<=10G;i++){
Valuereturnedis$5=32
(gdb)
当我们调试的时候如果觉得某个函数存在问题,进入函数调试之后发现问题不在这个函
数,那么我们就可以使用finish命令运行程序,知道当前函数结束。
12、c命令用于恢复程序的运行,例如我们再一个程序中设置了两个断点,而觉得问题不会
再这两个断点之间的代码上,那么我们局可以在查看完第一个断点的变量及堆栈情况后,
用c命令恢复程序的正常运行,代码就会停在dier个断点处
0x8048395:
filetest.c,line5・
Ox80483d9:
filetest.ctline12.
(gdb)c
Continuing•
Thesuminsumfunctionis5050
Thesuminmainfunctionis5G50
Breakpoint5fmain()attest
12}
(gdb)c
Continuing.
Programexitednormally.
(gdb)
13、q(quit)命令
q命令用于退出gdb调试器
(gdb)q
[root^localhostgdb]#
(三)Makefile文件的编写
1、先用vi编辑一个简单的c程序,由两个文件组成
文件fun.c内容
#include"fun.h"
intmax_fun(intx,inty)
{
if(x>=y)
returnx;
else
returny;
}
文件main.c内容
#include"fun.h"
intmain(void)
{
inta,b;
printf("Pleaseenterthenumberaandb\n”);
scanf("%d%d",&a,&b);
intmax=0;
max=max_fun(a,b);
printf("Themaxnumberis%d\n",max);return0;
}
文件fun.h内容
#include
externintmaxfun(intx,inty);
2、使用gcc编译命令直接编译出可执行文件main,并运行查看结果
.匚-uln
main
makefile如下
[root@lci匸吕llhostmakefile]#gccmain*cfun[root@locaLhostmakefile]#Is
fun,cfun・hm^inmain,c
[root(alocaLhostmakefile]#./main
Pleaseenterthenumberaandb
12
56
Themaxnumberis56
[root(alocalhostmakefile]#
3、用vi编辑makefile,内容如下所示
main:
main.ofun.o
gccmain.ofun.o-omain
main.o:
main.cfun.h
gcc-cmain.c-omain.o
fun.o:
fun.cfun.h
gcc-cfun.c-ofun.o
clean:
rm-fmain*.o
4、退出并保存,在shell中键入make,查看并运行产生的可执行文件
[root^localhostmakefile]#makegcc-cmain.c-qmain.ogccfun.cfun・o
gt匚main・ofun.o-omain
[root^localhostmakefile]#Isfun.cfun.hfun*omainmain.c
[root^ilocalhostmakefile]#・/mainPleaseterthenumberaandb2367
Themaxnum厂is67
[root^localhostmakefile]#|
5、用vi打开makefile进行改写,用变量进行替换,经变量替换后的
OBJS=main.0fun.o
CC=gcc
CFLAGS=-c
main:
$(OBJS)
$(CC)$(OBJS)-omain
main.o:
main.cfun.h
$(CC)$(CFLAGS)main.c-omain.o
fun.o:
fun.cfun.h
$(CC)$(CFLAGS)fun.c-ofun.oclean:
rm-fmain*.o
退出保存后,在shell中执行make和makeclean命令的效果和前面第4步是一样的
6、改写makefile,使用自动变量,改写后的情况如下
OBJS=main.0fun.o
CC=gcc
CFLAGS=-c
main:
$(OBJS)
$(CC)$(OBJS)-o$@
main.o:
main.cfun.h
$(CC)$(CFLAGS)$<-o$@
fun.o:
fun.cfun.h
$(CC)$(CFLAGS)$<-o$@
clean:
rm-fmain*.o
退出保存后,在shell中执行make和makeclean命令的效果和前面一样
上机报告要求:
1、总结选项-o,-E,-S,-c,-static,-g的功能作用。
-o指定目标文件名称
-E选项的作用:
只进行预处理,不做其他处理。
-S选项的作用:
只是编译不汇编,生成汇编代码
-c选项的作用:
只是编译不连接,生成目标文件.o-static选项的作用:
链接静态库
-g选项的作用:
在可执行程序中包含标准调试信息2、启动gdb的方式有几种?
分别如何启动?
1)gdb+调试程序名
2)gdb
file调试程序名
3、总结gdb中step命令与next命令的区别?
finish命令与quit命令的区别?
s命令用于单步运行,另外n(next)命令也用于单步运行,他们的区别在于:
如果有
函数调用的时候,s会进入该函数而n不会进入该函数。
finish命令用于运行程序,直到当前函数结束。
q命令用于退出gdb调试器
makefile,指出这三大
4、编写makefile文件的三大构成要素是什么?
分析第三个步骤的要素分别对应的具体代码?
目标:
依赖命令
main:
main.ofun.o
(Tab)gccmain.ofun.o-omain
main.o:
main.cfun.h
(Tab)gcc-cmain.c-omain.o
fun.o:
fun.cfun.h
(Tab)gcc-cfun.c-ofun.o
clean:
(Tab)rm-fmain*.o