gnu make学习笔记一

来源:互联网 发布:淘宝奶粉质量投诉电话 编辑:程序博客网 时间:2024/05/23 01:21

代码(红色部分)来自于《GNU Make Manual》4.14 Generating Prerequisites Automatically

http://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html#Automatic-Prerequisites

 


 

 

%.d: %.c
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$; /
    sed 's,/($*/)/.o[ :]*,/1.o $@ : ,g' < $@.$$$$ > $@; /
    rm -f $@.$$$$



make
----------------------------------------------------------------------------
"%": 模式字符, 匹配一个或多个字符
"$": 表示变量或者函数的引用,如果我们的规则如果需要“$”,需要书写两个连续的(“$$”)。
"$<": 规则的第一个依赖文件名
"$@": 代表规则中的目标文件名
"$$$$": 转译为"$$"
"$*": The stem with which an implicit rule matches. 意思是"%.c"如果匹配了foo.c, "$*"就是foo

gcc
----------------------------------------------------------------------------
"-M": GCC使用"-M"参数自动生成依赖的头文件列表

shell
----------------------------------------------------------------------------
">": 重定向
"$$": 表示当前的进程号
";": 继续执行下一个命令. 这里用于保证"$$$$"是同一个进程ID
"/": 续行符

sed与正则表达式
----------------------------------------------------------------------------
's,/($*/)/.o[ :]*,/1.o $@ : ,g'
","是分割符, sed可以用任意字符作为分割符
"/(/)", 正则表达式中表示为“(pattern)“, 匹配pattern并获取这一匹配
"[ :]", 字符“ "或者":"
"[ :]*", 表示一个或多个字符“ "或者":"
"/.", 表示点号,正则表达式中"."表示任意单个字符,这里用"/"进行转义
"/1", 表示第一个匹配, 这里表示"/(/)"的内容"$*"

 

 


$ cat hello.c
#include <stdio.h>

int main(int argc, char* argv[])
{
        printf("Hello, world!/n");
}


$ gcc -M hello.c
hello.o: hello.c /usr/include/stdio.h /usr/include/features.h /
 /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h /
 /usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h /
 /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stddef.h /
 /usr/include/bits/types.h /usr/include/bits/typesizes.h /
 /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h /
 /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stdarg.h /
 /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h


$ gcc -M hello.c | sed 's//(hello/)/.o[ :]*//1.o hello.d : /g'
hello.o hello.d : hello.c /usr/include/stdio.h /usr/include/features.h /
 /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h /
 /usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h /
 /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stddef.h /
 /usr/include/bits/types.h /usr/include/bits/typesizes.h /
 /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h /
 /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stdarg.h /
 /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h