当前位置:文档之家› Linu系统编程实验gccgdb的使用以及Makefile文件的编写

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 文件,内容如下:#include <stdio,h> int main(void) {priritf("hello world\n"); return 0;}2、gcc 指令的一般格式为:gcc [选项]要编译的文件[选项][目标文件]例:使用gcc 编译命令,编译 hello.c生成可执行文件 hello ,并运行hellogcc]# vi hello ・ cgcc]# gcc hello.c -o hello gcc]# ./hello上面的命令一步由.c 文件生成了可执行文件,将 gcc 的四个编译流程:预处理、编译、 汇编、连接一步完成,下面将介绍四个流程分别做了什么工作 3、 -E 选项的作用:只进行预处理,不做其他处理。

例:只对hello.c 文件进行预处理,生成文件 hello.i ,并查看[root@locdlhost gcc ]# gcc -E hello •匚-o hello * i [root (alocalhost gcc ]# Is hello hel lo.c: hel lo.i通过查看可以看到头文件包含部分代码 #include <stdio.h>经过预处理阶段之后,编译 器已将stdio.h 的内容贴了进来。

4、 -S 选项的使用-S 选项的作用:只是编译不汇编,生成汇编代码[root@localhost [root@localhost [root@localho5t hello world [rootglocalhostgcc]#例:将hello.i文件只进行编译而不进行汇编,生成汇编代码 hello.s[root^localhost[rootglocalhost nello hello* c[root^localhostgcc]# gcc -S hello.i -o hello.s gcc]# Is hello.i hello,s gcc]# |5、-c 选项的使用 -c 选项的作用:只是编译不连接,生成目标文件 .0 例:将汇编代码hello.s 只编译不链接成 hello.o 文件 [root (alocalhost gcc]# gcc - c hello . s -o hello [root (alocalhost gcc]# Ishello hello.c hello.i hello.o hello.s6、将编译好的hello.o 链接库,生成可执行文件 hello[root (alocalhost [root (alocalhost hello hello.c [root (alocalhost hello worldgcc]# gcc hello ・o -o hello gcc]# Is hello.i hello.o hello.s gcc]# ・/hello7、-static 选项的使用 -static 选项的作用:链接静态库 例:比较hello.c 连接动态库生成的可执行文件 hellol 的大小 hello 和链接静态库生成的可执行文件-rwxr- xr x 1 root root 4641 J un103:47 hello -rwxr- xr-x 1 root root 605990 Jun 1 03:47 hellol -rw - r ■ ■「 ________________ 1 root root 75 J un 1 03:15 hello,c -rw -厂.1 rootroot 18880 J un 1 B3:27 hello.i -rw - r- ■ ■「八1 「oot root 844 J un 1 03:41 hello^o -rw - r- --IP -- 1 rootroot 416 J un 1 03:35 hello.s[root@localhost gcc]# gcc hello.c -o hello[root@localhost gcc]# gcc -static hello .匚-o hellol [root@localhost gcc]# ll total 636可以看到静态链接库的可执行文件 hellol 比动态链接库的可执行文件hello 要大的多,他们的执行效果是一样的8、-g 选项的使用 -g 选项的作用:在可执行程序中包含标准调试信息 例:将hello.c 编译成包含标准调试信息的可执行文件 hello22、将test.c 文件编译成包含标准调试信息的文件 testgcc]# gcc -g hello.c -o hello2 gcc]# Ishello.i hell O .Ehello »o带有标准调试信息的可执行文件可以使用9、-02选项的使用gdb 调试器进行调试,以便找出逻辑错误-02选项的作用:完成程序的优化工作例:将hello.c 用02优化选项编译成可执行文件 hello3,和正常编译产生的可执行文件hello 进行比较gcc]# gcc -02 hEllo.c -o hello3 gcc]# Is hello .c hello .o hello .i hello .s gcc]# ./lhello qcc]# ./Ihello3 (二) gdb 调试器1先用vi 编辑文件test.c 用于gdb 调试器调试,内容如下[root^localhost[root@localhost hello hello2 hellol hello.c [root (alocalhos t [root@localhost hello hello2 hellol hello3 [root (alocalhost hello world[root (alocalhost hello world[root(alocalhost gdb]# gcc -g test.c -o test [rootfatocalhost gdb]# Is test test ・c3、启动gdb进行调试l[raot^localhost gdb]# gdb testGNU gdb Red Hat Linux (6.5-25.el5rh)Copyright {C) 2006 Free Software Faundation, Inc.GDB is free software,匚overed by the GNU General Public Li匚ense r and you arewelcome to change it and/or distribute copies of it und er certain conditions* Type ^show copying" to see the conditions・There is absolut已ly no warranty for GDB* Type h,show wa rranty" for details.This GDB was configured as ''iaSG-redhat-linux-gnu1'.・・Us ing host libthreaddb library "/Iib/i686/no5egneg/libth read db.so・1"・[gdb)可以看到gdb启动界面中显示了gdb的版本、自由软件等信息,然后进入了有”gdb”开头的命令行界面4、I (list )命令I命令用于查看文件(gdb) list1#includE <;stdio ・hA2int main(void)3{4int sum(ir»t sum);5int i,result=0;6sum(160);7for(i-l;i<=100;i++){8result+=i;g}16printf("The sum in main function is %d\n"』result);(gdb)111厂etu厂n G;12}13int sum(int num)可以看到每行代码面前都有对应的行号,这样方便我们设置断点。

5、b (breakpoint )命令b用于设置断点,断点调试时调试程序的一个非常重要的手段,设置方法:在”b”命令之后加上对应的行号,如下图(gdb) b 6Breakpoint 2 at 0x804839c: file test.c, line 6.在gdb中可以设置多个断点。

代码运行时会到断点对应的行之前暂停,上图中,代码就会运行到第7行之前暂停(并没有运行第7行)。

6、info命令info命令用于查看断点情况,设置好断点后可以用它来查看7 、r ( run ) 命令(gdb) b 7Breakpoint 1 at 0x8O483a8: file test.c, line 7.(gdb) info bNum Type Disp Enb Address What1 breakpoint keep y 0xG8O483a8 in mainat test.c:7 (gdb) | r命令用于运行代码,默认是从首行开始运行,也可以在r后面加上行号,从程序中指定行开始运行。

(gdb)「Starting program: /home/Linux C/test/gdb/testThe sum in sum function is 5050Breakpoint 1, main () at test•c:77 for(i=l;i<=100;i++){(gdb) |可以看到程序运行到断点处就停止了8、p ( print )命令p命令用于查看变量的值,在调试的时候我们经常要查看某个变量当前的值与我们逻辑设定的值是否相同,输入p+变量名即可(gdb) p result$1 = 0(gdb) p numNo symbol ''num'1 in current con text((gdb) p i$2 = 2420724(gdb) |可以看到result在第6行已被赋值为零,而i目前还没有被赋值所以是一个随机数,在主函数里看不到num的值,只有进入子函数才能看到9、s ( step )命令s命令用于单步运行,另外n (next)命令也用于单步运行,他们的区别在于:如果有函数调用的时候,s会进入该函数而n不会进入该函数。

相关主题