c語言學習筆記二十

2021-09-01 12:11:02 字數 2496 閱讀 8518

makefile基礎

基本規則

一般的程式都是由多個原始檔編譯鏈結而成,而這些原始檔的處理通常是由makefile來處

理示例**如下:

/*test29.h*/

#ifndef main_h

#define main_h

/*宣告點型別*/

typedef struct point item_t;

/*宣告最大列和行變數*/

#define max_row 5

#define max_col 5

#endif

/*test30.h定義堆疊標頭檔案*/

#ifndef stack_h

#define stack_h

#include "test29.h"

extern void push(item_t);/*壓棧 */

extern item_t pop(void);/*出棧*/

estern int is_empty(void);/*檢查堆疊是否為空*/

#endif

/*test31.c堆疊原始檔,定義基本操作*/

#include "test30.h"

static item_t stack[512];

static int top=0;

void push(item_t p)

item_t pop(void)

int is_empty(voicol)

/*地圖標頭檔案*/

#ifndef maze_h

#define maze_h

#include "test29.h"

extern int maze[max_row][max_col];

void print_maze(void);/*不用extern定義,原始檔中可以不作實現*/

#endif

/*test33.c地圖原始檔*/

#include

#include "test32.h"

/*定義地圖*/

int maze[max_row][max_col]=;

/*列印地圖*/

void print_maze(void),,,,},

,,,,},

,,,,},

,,,,},

,,,,},

};int visit(int row,int col,struct point pre);

/*所到之上修改地圖座標值*/

maze[row][col]=2;

predecessor[row][col]=pre;

/*壓棧*/

push(visit_point);

}int main(void);

maze[p.row][p.col]=2;

push(p);

while(!is_empty)

/*right*/

if(p.col+1=0&&maze[p.row][p.col-1]==0)

/*up*/

if(p.row-1>=0&&maze[p.row-1][p.col]==0)

print_maze();

}if(p.row==max_row-1&&p.col==max_col-1)

}else

return 0;

}編譯方法:

方式一:gcc test34.c test33.c test31.c -o test34

缺點:如果test31.c地圖修改後,需要重新編譯

方式二:

gcc -c test34.c

gcc -c test33.c

gcc -c test31.c

gcc test34.o test33.o test31.o -o test34

如果修改test31.c地圖後,只需:

gcc -c test31.c

gcc test34.o test33.o test31.o -o test34

方式三:同目錄下搗故個makefile檔案

test34:test34.o test33.o test31.o

(這裡是個tab) gcc test34.o test33.o test31.o -o test34

test34.o:test34.c test29.h test30.h test32.h

gcc -c test34.c

test33.o:test33.c test32.h test29.h

gcc -c test33.c

test31.o:test31.c test30.h test29.h

gcc -c test31.c

執行結果:

yuezhenhua@ubuntu:/opt/sdk/tc/makefile$ make

gcc -c test34.c

gcc -c test33.c

gcc -c test31.c

gcc test34.o test33.o test31.o -o test34

c語言學習筆記二十二

自動處理標頭檔案的依賴關係 all test34 test34 test34.o test33.o test31.o gcc o test34.o test34.c test29.h test30.h test32.h test33.o test33.c test32.h test29.h test...

c語言學習筆記二十一

makefile中關於變數的語法規則 示例 如下 foo bar bar huh?all echo foo 執行make後輸出 huh?優點 可以把變數的值推遲到後面定義 示例 main.o main.c cc cfags cppflags c cc gcc 編譯選項 cfags o g 預處理選項...

c語言學習筆記二十二

自動處理標頭檔案的依賴關係 all test34 test34 test34.o test33.o test31.o gcc o test34.o test34.c test29.h test30.h test32.h test33.o test33.c test32.h test29.h test...