Makefile之基础1
创建测试Demo
// cube.h
#ifndef INC_01_CUBE_H
#define INC_01_CUBE_H
#include <stdio.h>
struct cube{
int width;
int height;
};
float getCubeArea(const struct cube cb);
#endif //INC_01_CUBE_H
// cube.c
#include "cube.h"
float getCubeArea(const struct cube cb) {
return cb.height * cb.width;
}
// circle.h
#ifndef INC_01_CIRCLE_H
#define INC_01_CIRCLE_H
#include <stdio.h>
struct circle{
int radius;
};
float getCircleArea(const struct circle c);
#endif //INC_01_CIRCLE_H
// circle.c
#include "circle.h"
float getCircleArea(const struct circle c) {
return c.radius * c.radius * 3.14;
}
// main.c
#include <stdio.h>
#include "circle.h"
#include "cube.h"
int main() {
struct circle c;
c.radius = 10;
printf("circle area = %f\n",getCircleArea(c));
struct cube cb;
cb.width = 10;
cb.height = 20;
printf("cube area = %f\n",getCubeArea(cb));
return 0;
}
Makefile
TAG=test
OBJ=cube.o circle.o main.o
CC:=gcc
RM:=rm -rf
$(TAG):$(OBJ)
$(CC) $^ -o $@
%.o:%.c
$(CC) -c $^ -o $@
.PHONY:
clean:
$(RM) $(OBJ)
cleanall:
$(RM) $(OBJ) $(TAG)
说明
Makefile编写规则
1、格式:
目标文件:依赖文件
【Tab】指令
注意:IDE的tab键和vim的tab键的区别。
2、变量,用$(variable name)来获取变量
- = 赋值
- := 恒等于
- += 追加
3、.PHONY
.PHONY表示clean/cleanall的"伪目标",make clean会执行对应的指令。
4、通配符
- %.c 任意的.c文件
- *.c 所有的.c文件
5、非常重要的三个变量
- $@ 目标文件
- $^ 所有的依赖文件
- $< 第一个依赖文件。