***本文是参考 linux内核完全注释.pdf 写的***
简单的makefile文件包含一些规则,规则形式如下
目标(target).. : 先决条件(prerequisites)...
\t 命令(command)
\t ...
注: 命令必须以tab键开始.
1. 自动变量: 在命令行上根据具体情况能被自动替换的变量,在命令执行前被替换。
例如: '$^' 的值表示规则的所有先决条件,包括他们所处的目录的名称;‘$<’表示规则的第一个先决条件;'$@'表示目标对象
例如:
foo.o : foo.c defs.h hack.h
cc -c $(CFLAGS) $< -o $@
$<将被替换成foo.c , $@将被替换成foo.o
2. as86, ld86简介
汇编编译工具(as86)和链接工具(ld86)
例子:
// @filename hello.c
// for makefile and as ld
int main()
{
printf("hello\n");
return 0;
}
// @filename Makefile
// for make
hello: hello.c
gcc -o $@ -Wall -Werror $< -g
执行
#make; ./hello // 输出hello
或
gcc -S hello.c // 生成hello.s
as hello.s -o hello.o
ld -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o -lc hello.o -o hello
./hello // 输出hello