Make命令运行时,须要一个 Makefile 文件,以告诉make命令须要怎么样的去编译和链接程序。
一个Makefile文件包含五个方面的内容:
- 具体规则
- 隐含规则
- 定义变量
- 指令
- 注释
Makefile规则
Makefile内容的核心是一系列的规则,这些规则告诉make程序要做的事以及做这件事所依赖的条件
规则的基本格式是:
target: dependency
(Tab字符)command
arget(目标): 通常是要产生的文件的名称
dependency(依赖): 指用来输入从而产生目标的文件
示例
//hello.c//
#include <stdio.h>
void print(char *s);
int main()
{
int a;
print ("Hello the world!\n");
return 0;
}
//printf.c//
#include<stdio.h>
void print(char *s)
{
printf("%s\n",s);
}
//makefile//
target=text
object=hello.c print.o //隐含规则//
CC=gcc
//声明变量//
$target:$object
$CC $object -o $target
.PHONY:clean //伪目标//
clean:
rm -rf *.o $target
//clean命令 定义clean的规则,无依赖//