make工程管理器及gdb偵錯程式 基本知識

2021-08-09 01:46:24 字數 3530 閱讀 5744

make工程管理器完全根據

makefile

檔案中的編譯規則進行編譯,

makefile

由以下三項基本內容組成:

1)需要生成的目標檔案(

target file)

2)生成目標索取要的依賴檔案(

dependency file)

3)生成目標檔案的編譯規則命令列(

command)

這三項內容按以下格式進行組織:

target :

dependency

(tab)

command

vim hello.c (編寫簡單程式)

hello.c

#include

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運用

gdb是

gnu開源組織發布的乙個強大的

unix

下的程式除錯工具。

以程式add.c 為例

#include

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)

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 中通過命令 l(

list

的縮寫)可以檢視所有的**行數。一次顯示十行)

(在 gdb 中通過命令 r(

run

的縮寫)執行程式。

gdb

預設從**的首行開始執行 (也可以通過「

r 行數」的方式讓程式從指定行數開始執行)。如果程式中有斷點,則 程式會在斷點行數的前一行暫停執行))如:

(gdb) r

starting program: /home/gdb/add

sum is 6

(斷點是除錯程式的重要方法,通過斷點可以知道程式每一步的執行狀況(比如當前

變數的值、函式是否呼叫、堆疊使用情況等)。在gdb 中通過命令 b(

breakpoint

的縮寫)進行斷點設定。)

(使用命令 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中可以通過命令 s(

step

的縮寫)和 n(

next

的縮寫)讓程式一步一步的往下執行。其中

s 可以在發生函式呼叫時進入函式內部執行,而

n 不會進入函式內部執行。)

p(print)  x 便可檢視

x的值

q(quit) :退出

gdb

return : 從函式返回

偵錯程式gdb 工程管理器 make

格式 gcc g 檔名.c o 可執行檔案 gdb 可執行檔案 list 檢視程式 quit 退出 run 執行 break 行號 設定斷點 break 行號 if i 3 語句 info break 檢視斷點 delete 斷點編號 刪除斷點 next 單步執行 不進入子函式 step 單步執行 ...

gdb偵錯程式與make工程管理器

gdb偵錯程式 gdb是gnu開源組織發布的乙個強大的unix下的程式除錯工具,gdb主要可幫助工程師完成下面4個方面的功能 1.啟動程式,可以按照工程師自定義的要求隨心所欲的執行程式。2.讓被除錯的程式在工程師指定的斷點處停住,斷點可以是條件表示式。3.當程式被停住時,可以檢查此時程式中所發生的事...

GDB偵錯程式 工程管理器 make和指令碼Shell

一 gdb功能 1 啟動被除錯程式 2 讓被除錯程式在指定的位置停住 3 當程式被停住時,可以檢查程式狀態 變數值 gcc g test.c o test gdb test 啟動gdb break main 在main函式處設定斷點 run r 執行程式 next n 單步執行程式 不進入子函式 s...