Makefile 是Make 读入的惟一配置文件,因此本节的内容实际就是讲述Makefile 的编写
规则。在一个Makefile中通常包含如下内容:
· 需要由make工具创建的目标体(target),通常是目标文件或可执行文件;
· 要创建的目标体所依赖的文件(dependency_file);
· 创建每个目标体时需要运行的命令(command)。
它的格式为:
target: dependency_files
command
例如,有两个文件分别为hello.c 和hello.h,创建的目标体为hello.o,执行的命令为gcc
编译指令:gcc –c hello.c,那么,对应的Makefile就可以写为:
#The simplest example
hello.o: hello.c hello.h
gcc –c hello.c –o hello.o
接着就可以使用make了。使用make的格式为:make target,这样make就会自动读入
Makefile(也可以是首字母小写makefile)并执行对应target 的command 语句,并会找到相
应的依赖文件。如下所示:
[root@localhost makefile]# make hello.o
gcc –c hello.c –o hello.o
[root@localhost makefile]# ls
hello.c hello.h hello.o Makefile
可以看到,Makefile执行了“hello.o”对应的命令语句,并生成了“hello.o”目标体。