如何编写makefile

在Makefile中也#开始的行都是注释行.Makefile中最重要的是描述文件的依赖关系的说明。一般的格式是:

target:components

TAB rule

第一行表示的是依赖关系。第二行是规则。

比如说我们上面的那个Makefile文件的第二行。

main:main.omytool1.o mytool2.o

表示我们的目标(target)main的依赖对象(components)是main.o mytool1.omytool2.o 当倚赖的对象在目标修改后修改的话,就要去执行规则一行所指定的命令。就象我们的上面那个Makefile第三行所说的一样要执行 gcc-o main main.omytool1.o mytool2.o 注意规则一行中的TAB表示那里是一个TAB键

Makefile有三个非常有用的变量。分别是$@,$^,$<代表的意义分别是:

$@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件。

如果我们使用上面三个变量,那么我们可以简化我们的Makefile文件为:

# 这是简化后的Makefile

main:main.omytool1.o mytool2.o

gcc -o $@ $^





一个例子:

这个makefile有六个源文件和六个头文件分别是:

 

func1.c  func2.c  func3.c func4.c  func5.c  main.c

head1.h  head2.h  head3.h head4.h head5.h  head.h

上面的c源文件分别会用到其下的头文件各个文件的内容分别是:

func1.c 文件

#include "head.h"

#include "head1.h"

void f1()

{

              struct student1stu;

              stu.id =10101;

              strcpy(stu.name,"ygt1");

              stu.sex ='m';

              printf("id= %d\t name = %s\t sex = %c\n",stu.id,stu.name,stu.sex);

}

func2.c 文件

#include "head.h"

#include "head2.h"

void f2()

{

              structstudent2 stu;

              stu.id =10102;

              strcpy(stu.name,"ygt2");

              stu.sex ='m';

              printf("id= %d\t name = %s\t sex = %c\n",stu.id,stu.name,stu.sex);

}

func3.c 文件

#include "head.h"

#include "head3.h"

void f3()

{

              structstudent3 stu;

              stu.id =10103;

              strcpy(stu.name,"ygt3");

              stu.sex ='m';

              printf("id= %d\t name = %s\t sex = %c\n",stu.id,stu.name,stu.sex);

}

func4.c 文件

#include "head.h"

#include "head4.h"

void f4()

{

              structstudent4 stu;

              stu.id =10104;

              strcpy(stu.name,"ygt4");

              stu.sex ='m';

              printf("id= %d\t name = %s\t sex = %c\n",stu.id,stu.name,stu.sex);

}

func5.c 文件

#include "head.h"

#include "head5.h"

void f5()

{

              structstudent5 stu;

              stu.id =10105;

              strcpy(stu.name,"ygt5");

              stu.sex ='m';

              printf("id= %d\t name = %s\t sex = %c\n",stu.id,stu.name,stu.sex);

}

 

main.c 文件

#include "head.h"

extern void f1();

extern void f2();

extern void f3();

extern void f4();

extern void f5();

int main()

{

              f1();

              f2();

              f3();

              f4();

              f5();

              printf("theend\n");

       return 0;

}

 

以上是这个工程的的所有源文件及其代码

head1.h 头文件

struct student1

{

       int id;

       char name[20];

       char sex;      

};

 

head2.h 头文件

struct student2

{

       int id;

       char name[20];

       char sex;      

};

 

head3.h 头文件

struct student3

{

       int id;

       char name[20];

       char sex;      

};

 

head1.h 头文件

struct student3

{

       int id;

       char name[20];

       char sex;      

};

 

head4.h 头文件

struct student4

{

       int id;

       char name[20];

       char sex;      

};

 

head51.h 头文件

struct student5

{

       int id;

       char name[20];

       char sex;      

};

 

head.h 头文件

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

以上是头文件的内容

以上文件都准备好后就要开始写makefile文件了

 

 

Makefile 文件的可以这么写:

 

 

 

exefile: main.o func1.o func2.o func3.o func4.ofunc5.o

       gcc -oexefile main.o func1.ofunc2.o func3.o func4.o func5.o

main.o:main.c head.h

       gcc -cmain.c

func1.o:func1.c head.h head1.h

       gcc -cfunc1.c

func2.o:func2.c head.h head2.h

       gcc -cfunc2.c

func3.o:func3.c head.h head3.h

       gcc -cfunc3.c

func4.o:func4.c head.h head4.h

       gcc -cfunc4.c

func5.o:func5.c head.h head5.h

       gcc -cfunc5.c

 

 

clean:

       rm -f *.oexefile

在这个makefile 蓝色的就是目标文件 可执行文件 目标文件时那些 *.o文件, 可执行文件就是exefile 文件,它是最终可以执行的文件。依赖文件就是那些*.c  *.h 文件。

每一个*.o 文件都有一组依赖文件,*.o 文件就是靠这些依赖文件生成。而生成的这些*.o文件又都是 exefile 可执行文件的依赖文件,他们生成 exefile 可执行文件。依赖关系 其实就是 说明了 目标文件是由哪些文件生成的,换言之,就是目标文件是由哪些文件更新的。

定义好依赖关系下一行就是make执行的命令,这个命令定义了操作系统如何生成这些目标文件的。命令行开始一定要以tab键开头。其执行过程就是make会比较 target 文件 prerequisites 文件 的修改日期,如果prerequisites 文件的日期比target文件的日期要新,或者target 不存在的话,make就会执行后续定义的命令。

 

讲解:

exefile: main.o func1.o func2.o func3.ofunc4.o func5.o

       gcc-o exefile main.o func1.o func2.o func3.o func4.o func5.o

 

exefile 依赖main.o func1.o func2.o func3.o func4.o func5.o这些*.o文件,也即是说exefile 就是开这些文件生成的。一开始这些*.o文件是不存在的,那么make就会往下执行语句,而暂时先不执行gcc -o main main.ofunc1.o func2.o func3.o func4.o func5.o 这句命令。

 

main.o:main.c head.h

       gcc-c main.c

main.o 依赖main.c head.h这两个文件 执行其命令生成 main.o目标文件,指着往下执行。。。

 

func1.o:func1.c head.h head1.h

       gcc-c func1.c

main.o 的执行。。。

func2.o:func2.c head.h head2.h

       gcc-c func2.c

main.o 的执行。。。

 

func3.o:func3.c head.h head3.h

       gcc-c func3.c

main.o 的执行。。。

 

func4.o:func4.c head.h head4.h

       gcc-c func4.c

main.o 的执行。。。

 

func5.o:func5.c head.h head5.h

       gcc-c func5.c

main.o 的执行。。。

当这些 *.o文件都别生成了后 make 就会执行 第一个依赖和第一个 依赖之后的命令

exefile: main.o func1.o func2.o func3.ofunc4.o func5.o

       gcc-o exefile main.o func1.o func2.o func3.o func4.o func5.o

最终生成 exefile 之可行文件。

 

clean:

       rm-f *.o exefile

clean 后面没有依赖文件,make 是不会执行 其后的命令的 ,只能make clean 显视 的执行。这句就是伪命令,就是做一些清理,把生成的 目标文件 *.o文件 exefile 删掉。

Make 后执行结果:

[yanggentao@wkp mfile]$ make clean

rm -f *.o exefile

[yanggentao@wkp mfile]$ make

gcc -c main.c

gcc -c func1.c

gcc -c func2.c

gcc -c func3.c

gcc -c func4.c

gcc -c func5.c

gcc -o main main.o func1.o func2.o func3.ofunc4.o func5.o

[yanggentao@wkp mfile]$

 

Make clean 后执行结果:

[yanggentao@wkp mfile]$ make clean

rm -f *.o exefile

[yanggentao@wkp mfile]$

根据makefile 的依赖规则我们还可以这样写,至于为什么这样写,我们先且不说。

exefile: main.o func1.o func2.o func3.ofunc4.o func5.o

       gcc-o exefile main.o func1.o func2.o func3.o func4.o func5.o

main.o:main.c

       gcc-c main.c

func1.o:func1.c

       gcc-c func1.c

func2.o:func2.c

       gcc-c func2.c

func3.o:func3.c

       gcc-c func3.c

func4.o:func4.c

       gcc-c func4.c

func5.o:func5.c

       gcc-c func5.c

 

 

clean:

       rm-f *.o exefile

 

这样写是把头文件都给去掉了,这样也对的,makefile的隐式规则会自动找这些在文件里包含的头文件的。


<script type="text/javascript" id="wumiiRelatedItems"> </script>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值