部分源程序参考了华清远见的《嵌入式Linux C编程入门》(第2版)P231,实现的功能是单链表的插入和合并功能,Makefile是自己编写的,以便将几个子函数一起弄过来编译链接。
Makefile文件
OBJS = main.o init.o ListInsert.o printlist.o MergeList.o
main : $(OBJS)
gcc $(OBJS) -o main
main.o : main.c include.h
gcc -g -c main.c -o main.o
init.o : init.c
gcc -g -c init.c -o init.o
ListInsert.o : ListInsert.c
gcc -g -c {1}lt; -o $@
printlist.o : printlist.c
gcc -g -c {1}lt; -o $@
MergeList.o : MergeList.c
gcc -g -c {1}lt; -o $@
clean :
rm main $(OBJS)
main.c文件
#include"include.h"
main()
{ struct STU e;
LinkList La,Lb,Lc;
printf("
-------------List Demo is running...-------------
");
/*Init the first LinkList*/
init(&La);
/*Insert the first element to the first LinkList*/
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age = 80;
e.score = 1000;
ListInsert(La,1,e);
/*Insert the second element to the first LinkList*/
strcpy(e.name,"stu3");
strcpy(e.stuno,"100002");
e.age = 80;
e.score = 1000;
ListInsert(La,2,e);
printlist(La); //print La
getchar();
/*Init the second LinkList*/
init(&Lb);
/*Insert the first element to the second LinkList*/
strcpy(e.name,"stu2");
strcpy(e.stuno,"100001");
e.age = 70;
e.score = 1000;
ListInsert(Lb,1,e);
/*Insert the second element to the second LinkList*/
strcpy(e.name,"stu4");
strcpy(e.stuno,"100002");
e.age = 70;
e.score = 1000;
ListInsert(Lb,2,e);
/*Insert the third element to the second LinkList*/
strcpy(e.name,"stu6");
strcpy(e.stuno,"100003");
e.age = 70;
e.score = 1000;
ListInsert(Lb,3,e);
printlist(Lb);
getchar();
/*Merge La and Lb*/
MergeList(La,Lb,&Lc);
printlist(Lc);
getchar();
}
include.h文件
#include
#include
#include
#include
/*page 226*/
struct STU
{ char name[20];
char stuno[10];
int age;
int score;
}stu[50];
typedef struct STU ElemType;
struct LNODE
{ ElemType data;
struct LNODE *next;
};
typedef struct LNODE LNode,*LinkList;
int init(LinkList *L);
int ListInsert(LinkList L,int i,ElemType e);
int printlist(LinkList L);
void MergeList(LinkList La,LinkList Lb,LinkList *Lc);
init.c文件
#include"include.h"
int init(LinkList *L)
{ (*L) = (LinkList)malloc(sizeof(LNode));
if(!L)
exit(0);
memset(&((*L)->data),0,sizeof(struct STU));
(*L)->next = NULL;
return 1;
}/*init*/
ListInsert.c文件
#include"include.h"
int ListInsert(LinkList L,int i,ElemType e)
{ LinkList p,s;
int j;
p = L;j = 0;
while(p && j < i-1)
{ p = p->next;
++j;
}
if(!p || j > i-1)
return 0;
s = (LinkList)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return 1;
}
MergeList.c文件
#include"include.h"
void MergeList(LinkList La,LinkList Lb,LinkList *Lc)
{ LinkList pa,pb,pc;
pa = La->next;pb = Lb->next;
*Lc = pc = La;
while(pa && pb)
{ if(Less_EqualList(&pa->data,&pb->data))
{ pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{ pc->next = pb;
pc = pb;
pb = pb->next;
}
pc->next = pa ? pa : pb;
free(La);
free(Lb);
}
}
int Less_EqualList(ElemType *e1,ElemType *e2)
{ if(strcmp(e1->name,e2->name) <= 0)
return 1;
else
return 0;
}
printlist.c文件
#include"include.h"
int printlist(LinkList L)
{ int i;
LinkList p;
p = L;
printf("name stuno age score
");
while(p->next)
{ p = p->next;
printf("%-10s%s %d %d
",p->data.name,p->data.stuno,p->data.age,p->data.score);
}
printf("
");
}
大概总结了下写简单Makefile的方法,就是include之类的和一些全局定义的结构体或者变量和函数等要放在同一个.h文件里面,
然后下面每一个子函数都要include进去这个头文件,这下再根据依赖关系就可以写出一个简单的Makefile了。
make结果
运行结果
原创文章,欢迎转载,转载请注明:blog.csdn.net/jjzhoujun2010
作者:Dream Fly