make工程管理器及gdb调试器 基本知识

Make工程管理器完全根据Makefile文件中的编译规则进行编译,Makefile由以下三项基本内容组成:

 

    1)需要生成的目标文件(target file

    2)生成目标索取要的依赖文件(dependency file

3)生成目标文件的编译规则命令行(command

 

    这三项内容按以下格式进行组织:

   target dependency

TABcommand

 

vim hello.c (编写简单程序)

Hello.c

#include<stdio.h>

int main()

{

printf(“Hello world!\n”);

return 0;

}

 

 

vim Makefile (Makefile 的作用是告诉make要做的事情)

Makefile

CC = gcc

Target = test

Object = test.o

(Tab)  $(target) : $(object)

(Tab)  $(CC) $(object) -o $(target)

.PHONY : clean  (注:此句目的是将clean变为一个伪目标防止make执行存在的文件clean)

clean:

rm -rf test *.o

 

再执行 make  即可出来结果 Hello world!

 

 

 

gdb运用

GDBGNU开源组织发布的一个强大的UNIX下的程序调试工具。

 

以程序add.c 为例

#include<stdio.h>

int add(int i,int j)

{

return(i+j);

}

 

int main()

{

Int i=1,j=4,sum =0;

sum=add(i,j);

printf(sum i %d,sum);

return 0;

}

 

    通过运行命令gcc -g add.c -o add”对add.c进行编译,其中参数g的作用是把调试信息加入生成的add可执行文件中,否则GDB就无法对add进行调试,然后就可以运用 ” gdb add” 命令进入gdb调试界面

 

[root@localhost gdb]# gdb add

GNU gdb Red Hat Linux (6.5-25.el5rh)

Copyright (C) 2006 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"...Using host libthread_db library "/lib/libthread_db.so.1".

 

接下来便是一系列命令符

(gdb) l

(在 gdb 中通过命令 llist 的缩写)可以查看所有的代码行数。一次显示十行)

 

(在 gdb 中通过命令 rrun 的缩写)运行程序。GDB 默认从代码的首行开始运行 (也可以通过“r 行数”的方式让程序从指定行数开始运行)。如果程序中有断点,则 程序会在断点行数的前一行暂停运行))

 如:

(gdb) r

Starting program: /home/gdb/add

sum is 6

 

 

(断点是调试程序的重要方法,通过断点可以知道程序每一步的执行状况(比如当前 变量的值、函数是否调用、堆栈使用情况等)。在gdb 中通过命令 bbreakpoint 的缩写)进行断点设置。)

  

(使用命令 b 可以设置多个断点,所以用户需要能够随时查看各个断点的情况,在 gdb中通过命令“info b”查看所有的断点情况。)

 

(在 gdb 中通过命令 c 让程序继续往下运行。)

 

(gdb) b 5

Breakpoint 1 at 0x8048387: file add.c, line 5.

(gdb) info b

Num Type           Disp Enb Address    What

1   breakpoint     keep y   0x08048387 in add at add.c:5

(gdb) r

Starting program: /home/gdb/add

 

Breakpoint 1, add (i=1, j=5) at add.c:5

5         return (i+j);

(gdb) c

Continuing.

sum is 6

 

Program exited normally.

(gdb)

 

在程序逻辑比较复杂的时候往往需要程序能一步一步的往下运行,但如果每行都设置一个断点的话又会很麻烦。

 

gdb中可以通过命令 sstep 的缩写)和 nnext 的缩写)让程序一步一步的往下运行。其中 s 可以在发生函数调用时进入函数内部运行,而 n 不会进入函数内部运行。)

 

 p(print)  x 便可查看x的值

 q(quit) :退出gdb

 return : 从函数返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值