如何编写makefile

来源:互联网 发布:淘宝tbc 编辑:程序博客网 时间:2024/05/22 01:46

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

target:components

TAB rule

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

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

main:main.omytool1.omytool2.o

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

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

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

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

# 这是简化后的Makefile

main:main.omytool1.omytool2.o

gcc -o $@$^





一个例子:

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

 

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

head1.h head2.h  head3.h head4.hhead5.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 voidf1();

extern voidf2();

extern voidf3();

extern voidf4();

extern voidf5();

int main()

{

             f1();

             f2();

             f3();

             f4();

             f5();

             printf("theend\n");

      return 0;

}

 

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

head1.h头文件

structstudent1

{

      int id;

      char name[20];

      charsex;      

};

 

head2.h头文件

structstudent2

{

      int id;

      char name[20];

      charsex;      

};

 

head3.h头文件

structstudent3

{

      int id;

      char name[20];

      charsex;      

};

 

head1.h头文件

structstudent3

{

      int id;

      char name[20];

      charsex;      

};

 

head4.h头文件

structstudent4

{

      int id;

      char name[20];

      charsex;      

};

 

head51.h头文件

structstudent5

{

      int id;

      char name[20];

      charsex;      

};

 

head.h头文件

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

以上是头文件的内容

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

 

 

Makefile文件的可以这么写:

 

 

 

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

      gcc-oexefile main.o func1.ofunc2.o func3.ofunc4.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.ofunc1.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.chead.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的隐式规则会自动找这些在文件里包含的头文件的。


 
原创粉丝点击