Makefile and Paul's Rules of Makefiles
来源:互联网 发布:php pdo查询数据库 编辑:程序博客网 时间:2024/05/29 17:59
Defining and Redefining Pattern Rules
Here are some examples of pattern rules actually predefined in make
. First, the rule that compiles ‘.c’ files into ‘.o’ files:
%.o : %.c $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
defines a rule that can make any file x.o from x.c. The recipe uses the automatic variables ‘$@’ and ‘$<’ to substitute the names of the target file and the source file in each case where the rule applies (seeAutomatic Variables).
https://www.gnu.org/software/make/manual/make.html#Pattern-IntroAutomatic Variables
11 Using make
to Update Archive Files
https://www.gnu.org/software/make/manual/make.html#Archiveslibfoo.a: libfoo.a(x.o) libfoo.a(y.o) ... ranlib libfoo.a
You can write a special kind of suffix rule for dealing with archive files. See Suffix Rules, for a full explanation of suffix rules. Archive suffix rules are obsolete in GNU make
, because pattern rules for archives are a more general mechanism (see Archive Update). But they are retained for compatibility with other make
s.
To write a suffix rule for archives, you simply write a suffix rule using the target suffix ‘.a’ (the usual suffix for archive files). For example, here is the old-fashioned suffix rule to update a library archive from C source files:
.c.a: $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o $(AR) r $@ $*.o $(RM) $*.o
This works just as if you had written the pattern rule:
(%.o): %.c $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o $(AR) r $@ $*.o $(RM) $*.o
In fact, this is just what make
does when it sees a suffix rule with ‘.a’ as the target suffix. Any double-suffix rule ‘.x.a’ is converted to a pattern rule with the target pattern ‘(%.o)’ and a prerequisite pattern of ‘%.x’.
Since you might want to use ‘.a’ as the suffix for some other kind of file, make
also converts archive suffix rules to pattern rules in the normal way (see Suffix Rules). Thus a double-suffix rule ‘.x.a’ produces two pattern rules: ‘(%.o): %.x’ and ‘%.a: %.x’.
‘$?’ is useful even in explicit rules when you wish to operate on only the prerequisites that have changed. For example, suppose that an archive named lib is supposed to contain copies of several object files. This rule copies just the changed object files into the archive:
lib: foo.o bar.o lose.o win.o ar r lib $?https://www.gnu.org/software/make/manual/make.html#Automatic-Variables
Paul's Rules of Makefiles
A somewhat tongue-in-cheek title, but this page lists a few very important rules you should always keep in mind when creating makefiles. Following these rules will allow your makefiles to be both pithy and beautiful. And they will make maintaining and modifying them, and thus your entire life, a much more pleasant experience.
- Use GNU
make
.Don't hassle with writing portable makefiles, use a portable
make
instead! - Every non-
.PHONY
rule must update a file with the exact name of its target.Make sure every command script touches the file "
$@
"-- not "../$@
", or "$(notdir $@)
", but exactly$@
. That way you and GNUmake
always agree. - Life is simplest if the targets are built in the current working directory.
Use
VPATH
to locate the sources from the objects directory, not to locate the objects from the sources directory. - Follow the Principle of Least Repetition.
Try to never write a filename more than once. Do this through a combination of make variables, pattern rules, automatic variables, and GNU
make
functions. - Every non-continued line that starts with a TAB is part of a command script--and vice versa.
If a non-continued line does not begin with a TAB character, it is never part of a command script: it is always interpreted as
makefile
syntax. If a non-continued line does begin with a TAB character, it is always part of a command script: it is never interpreted asmakefile
syntax.Continued lines are always of the same type as their predecessor, regardless of what characters they start with.
Yes, that's all of them... so far. It's not that that's all I have to say on the subject, but coming up with points which are both truly fundamental and expressible in a succinct rule format, ain't easy.
Let me know if you have suggestions.
- Makefile and Paul's Rules of Makefiles
- Makefile:1279: *** mixed implicit and normal rules.
- Makefile:1279: *** mixed implicit and normal rules.
- Makefile详解-环境变量MAKEFILES
- Rob Pike's 5 Rules of Programming
- Review of Zuo Lab's Rules
- Makefile:431: *** mixed implicit and normal rules. Stop.
- Makefile:431: *** mixed implicit and normal rules. Stop.
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.
- Makefile:431: *** mixed implicit and normal rules. Stop.
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.
- Error:Makefile:1449:*** mixed implicit and normal rules: deprecated syntax
- Makefile:1607 *** mixed implicit and nomal rules:depracated syntax
- Error:Makefile:1449:*** mixed implicit and normal rules: deprecated syntax
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.
- PAUL'S MSN BLOG
- mod and it's makefile
- ANR---android程序架构时必须考虑的问题
- 如何使用SWFTools 命令 将pdf文件转换为swf文件,并进行在线浏览该pdf文件
- 深度优先搜索----部分和问题
- invalid conversion from ‘void*’ to ‘void* (*)(void*)’ 问题解决
- Mapinfo Sql语句查询方法
- Makefile and Paul's Rules of Makefiles
- git remote添加本地库的方法
- 基础总结篇之一:Activity生命周期
- [VNC] CentOS6.5中VNC无法启动服务的一种可能解决方案
- java命名规范,action中变量写了getset方法,页面取不到值
- Web数据库框架
- ARM 程序状态寄存器的结构
- 没有目的的行程是迷茫的
- C++11新特性中的匿名函数Lambda表达式的汇编实现分析