调试器(GDB)GNU发布的一款功能强大的程序调试工具。
GDB主要完成下面三个方面的功能:
1、 启动被调试程序;
2、 让被调试的程序在指定的位置停住;
3、 当程序被停住时,可以检查程序的状态-变量值;
以假定文件test.c为例,说明gdb的功能:
[root@localhost GDB]# ls
demo test.c
[root@localhost GDB]# gcc -g test.c -otext.exe
[root@localhost GDB]# ls
demo test.c text.exe
[root@localhost GDB]# gdb te
test.c text.exe
[root@localhost GDB]# gdb text.exe
GNU gdb Red Hat Linux (6.5-25.el5rh)
Copyright (C) 2006 Free SoftwareFoundation, Inc.
GDB is free software, covered by the GNUGeneral Public License, and you are
welcome to change it and/or distributecopies of it under certain conditions.
Type "show copying" to see theconditions.
There is absolutely no warranty forGDB. Type "show warranty" fordetails.
This GDB was configured as"i386-redhat-linux-gnu"...Using host libthread_db library"/lib/libthread_db.so.1".
(gdb) run
Starting program: /root/Desktop/1112/GDB/text.exe
result = 5050
命令选项介绍:
1、-g:产生调试信息;
2、gdb “可执行文件”;
3、run:执行程序;
4、list:查看源代码;
5、quit:退出gdb;
6、break “行号”:设置断点;
7、info break:查看断点信息;
8、delete:删除断点;
9、step:单步执行,会进入子函数;
10、next:单步执行,不会进入子函数;
11、print 变量名:可显示结果;
12、continue:运行到最后;
13、finish:结束当前函数;
Make工程管理器:
优点:1、使用方便;2、调试效率高;(提高代码的维护性、可读性)
Makefile:make工程管理器是完全根据makefile文件中的编译规则命令进行工作的。Makefile文件有以下三项基本内容组成:
1、 需要生成目标文件;(targetfile)
2、 生成目标文件所需要的依赖文件;(dependency file)
3、 生成目标文件的编译规则命令行;(command)
最强大的功能:实现批处理(脚本文件也有这个批处理功能)
以以下程序为例简要说明简单makefile文件的制作与使用
[root@localhost test]# ls
add.c div.c main.c makefile mul.c sub.c
[root@localhost test]# cat *.c
int add(int a, int b)
{
return a + b;
}
int div(int a, int b)
{
return a / b;
}
#include <stdio.h>
int main()
{
printf("add = %d\n",add(6,3));
printf("sub = %d\n",sub(6,3));
printf("mul = %d\n",mul(6,3));
printf("div = %d\n",div(6,3));
return 0;
}
int mul(int a, int b)
{
return a * b;
}
int sub(int a, int b)
{
return a - b;
}
[root@localhost test]# cat makefile
cal:add.o sub.o div.o mul.o main.o
gcc add.o sub.o div.o mul.o main.o -o cal
add.o:add.c
gcc -c add.c
sub.o:sub.c
gcc -c sub.c
div.o:div.c
gcc -c div.c
mul.o:mul.c
gcc -c mul.c
main.o:main.c
gcc -c main.c
.PHONY:clean
clean:
rm -f *.o cal
简单的语法规则:
目标文件:依赖文件
指令
.PHONY:定义假目标;
真目标与假目标:真目标真正的编译出可执行文件,假目标仅仅是一串指令;
一个大工程的makefile文件有三种类型:
1、 最顶层:总控makefile,一般在主目录下;
2、 功能目录:功能目录makefile;
3、 Scripts目录:头文件makefile;