`

c语言学习笔记二十

    博客分类:
  • c
c 
阅读更多

makefile基础


基本规则


一般的程序都是由多个源文件编译链接而成,而这些源文件的处理通常是由makefile来处





示例代码如下:
/*test29.h*/
#ifndef MAIN_H
#define MAIN_H
/*声明点类型*/
typedef struct point {int row,col} 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){
stack[top++]=p;
}


item_t pop(void){
return stack[--top];
}


int is_empty(voicol){
return top==0;
}



/*地图头文件*/
#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 <stdio.h>
#include "test32.h"


/*定义地图*/
int maze[MAX_ROW][MAX_COL]={
0,1,0,0,0,
0,1,0,1,0,
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
};


/*打印地图*/
void print_maze(void){
int i,j;
for(i=0;i<MAX_ROW;i++){
for(j=0;j<MAX_ROW;j++){
printf("%d ",maze[i][j]);
}
}
printf("%d ",maze[i][j]);
}








/*test34.c深度优先搜索解迷问题*/
#include <stdio.h>
#include "test29.h"
#include "test30.h"
#include "test32.h"


/*地图中点坐标*/
struct point predecessor[MAX_ROW][MAX_COL]={
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
};


int visit(int row,int col,struct point pre){
/*初始化地图*/
struct point visit_point={row,col};
/*所到之上修改地图坐标值*/
maze[row][col]=2;
predecessor[row][col]=pre;
/*压栈*/
push(visit_point);
}


int main(void){
struct point p={0,0};
maze[p.row][p.col]=2;
push(p);


while(!is_empty){
p=pop();
/*goal到达终点*/
if(p.row==MAX_ROW-1&&p.col==MAX_COL-1){
break;
}
/*right*/
if(p.col+1<MAX_COL&&maze[p.row][p.col+1]==0){
visit(p.row,p.col+1,p);
}
/*down*/
if(p.row+1<MAX_ROW&&maze[p.row+1][p.col]==0){
visit(p.row+1,p.col,p);
}
/*left*/
if(p.col-1>=0&&maze[p.row][p.col-1]==0){
visit(p.row,p.col-1,p);
}
/*up*/
if(p.row-1>=0&&maze[p.row-1][p.col]==0){
visit(p.row-1,p.col,p);
}
print_maze();
}
if(p.row==MAX_ROW-1&&p.col==MAX_COL-1){
printf("(%d,%d)\n",p.row,p.col);
while(predecessor[p.row][p.col].row!=-1){
p=predecessor[p.row][p.col];
printf("(%d,%d)\n",p.row,p.col);
}
}else{
printf("No Path!\n");
}
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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics