makefile 编写规则

发布时间 2023-04-01 13:26:45作者: ImreW

(一)makefile 规则

 一般开头都是 Tab ,不能空格, include 前面不能是 Tab;

  1、如果没编译过,将所有的  (.c) 文件编译并且链接;

  2、如果有其中的 (.c) 文件改变,编译并链接改变的文件;

  3、如果(.h)文件被修改,编译引用相应的 (.c) 文件, 链接;

  4、在随意修改时间的情况下,会导致编译过程中生成的 (.o 中间文件)与可执行文件时间不一致,此时会编译相应的文件,并链接,最终编译成可执行文件;

(二)例子

例如有2个  .h 文件(utils.h,  player.h, actor.h)和 3个 .c 文件( main.c, player.c, actor.c)需要编译链接:

/*****main.c*********/
#include "utils.h"
#include "player.h"

void main() {
 // do something
}
/*******player.c**********/

#include "utils.h"
#include "actor.h"

bool create_player() {
// do something
}
/****actor.c************/

#include "utils.h"

bool create_actor() {
// do something
}

1.第一版 makefile

/*********  makefile   *****************/    
    test : main.o actor.o
        cc -o test main.o actor.o

    main.o : main.c utils.h player.h actor.h
        cc -c main.c

    palyer.o: player.c player.h actor.h utils.h
        cc -c player.c

    actor.o: actor.h utils.h
        cc -c actor.c

    clean:
        rm test ain.o player.o actor.o

  优点:可毒性很强,思路清晰 明了;

  缺点:麻烦,重复的依赖过多,当需要编译大量文件时容易出错;

2.第二版:利用 makefile 的变量

/*********  makefile   *****************/    
    OBJ = main.o actor.o    // 跟第一版比较,唯一的区别在这

    test : $(OBJ)        // 这儿
        cc -o test $(OBJ)      // 这儿

    main.o : main.c utils.h player.h actor.h
        cc -c main.c

    palyer.o: player.c player.h actor.h utils.h
        cc -c player.c

    actor.o: actor.h utils.h
        cc -c actor.c
   .PHONY : clean    // 伪目标,避免:如果当前目录下存在 clean rm 指令不执行
    clean:
        -rm test $(OBJ)   // 前面的 (-) 表示,执行过程中不 care 出错;

3.第三版:利用 GUN make 的自动推导规则

当 make 看到 (.o )文件,他会自动把 (.c) 文件加上依赖关系,包括执行的语句(cc -c xx.c);

/*********  makefile   *****************/    
    OBJ = main.o actor.o    // 跟第一版比较,唯一的区别在这

    test : $(OBJ)        // 这儿
        cc -o test $(OBJ)      // 这儿

    main.o : utils.h player.h actor.h

    palyer.o: player.h actor.h utils.h

    actor.o: actor.h utils.h

   .PHONY : clean    // 伪目标,避免:如果当前目录下存在 clean rm 指令不执行
    clean:
        -rm test $(OBJ)

4.第四版: 对第三版的整理(有一些重复的  .h)

/*********  makefile   *****************/    
    OBJ = main.o actor.o    // 跟第一版比较,唯一的区别在这

    test : $(OBJ)        // 这儿
        cc -o test $(OBJ)      // 这儿

    $(OBJ) : utils.h actor.o
    main.o player.o

   .PHONY : clean    // 伪目标,避免:如果当前目录下存在 clean rm 指令不执行
    clean:
        -rm test $(OBJ)

 优点:简洁   缺点:不好理解 

5.大型项目makefile编写

Makefile 同样也有像 c / c++ 类似的include功能;

例如我们有一堆 a.mk , b.mk以及 foo.make和一个变量 $(bar),其包含了 e.mk,f.mk, 那么 

 include foo.make *.mk $(bar)   >———  等价——> include foo.make a.mk b.mk e.mk f.mk