1.@关闭命令的回显linux
2.make带入参数“-n”或“--just-print”,只是显示命令,但不会执行命令,这个功能方便调试 Makefile。数据库
3.make 参数“-s”或“--slient”全面禁止命令的显示。bash
make 逐条执行其后的命令。 若是打算上一条命令结果应用到下一条命令,须要把要执行的命令写在同一行使用分号;隔开。less
test:
cd ./path1
pwd
复制代码
运行结果:ide
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1
pwd
/home/workspace/my_workspace/study/makefile
复制代码
test:
cd ./path1;pwd
复制代码
运行结果:ui
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1;pwd
/home/workspace/my_workspace/study/makefile/path1
复制代码
makefile使用mkdir建立目录时,不存在时makefile正常运行,可是目录存在则没法mkdir时出现报错,这个并不该该影响到后面命令的执行。this
test:
@mkdir path1
@echo continue
复制代码
运行结果:spa
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: *** [test] Error 1
复制代码
命令执行错误直接终止。 解决办法 (1)Makefile命令行前加一个减号- (2)全局的办法,给 make 加上“-i”或是“--ignore-errors”参数。命令行
test:
-@mkdir path1
@echo continue
复制代码
运行结果:debug
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
continue
复制代码
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test --ignore-errors
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
continue
复制代码
若是一个规则是以“.IGNORE”做为目标的,那么这个规则中的全部命令将会忽略错误。
.IGNORE: test
test:
@mkdir path1
@cat no_exist_file
@echo continue
复制代码
运行结果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
cat: no_exist_file: No such file or directory
make: [test] Error 1 (ignored)
continue
复制代码
根据功能和模块将相关的文件放置在不一样的文件夹中,每一个文件夹中单首创建一个Makefile来进行管理维护。最外层有个总控Makefile,能够实现全编译。
定义$(MAKE)宏变量的意思是,定义成一个变量便于make参数传递,利于维护。
subdir = ./path1
test:
cd $(subdir) && $(MAKE)
复制代码
运行结果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
复制代码
总控Makefile中的参数要传递给子目录下的Makefile。 只需在总控Makefile的变量前添加export便可,相反若是特定变量不想传递下去则使用unexport。
总控Makefile
export subdir = ./path1
test:
cd $(subdir) && $(MAKE)
复制代码
子目录path1中的Makefile
hello.o: hello.c
gcc -c hello.c
echo $(subdir)
复制代码
运行结果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c echo ./path1 ./path1 make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
复制代码
若是不少变量要传递,只须要添加export关键字,后面不须要跟变量名。
export
subdir = ./path1
other = 666
test:
cd $(subdir) && $(MAKE) && rm *.o
复制代码
运行结果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make && rm *.o
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c ./path1 666 make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
复制代码
注意: SHELL、MAKEFLAGE这两个变量比较特殊,无论是否export它们都会传递到子目录Makefile中。MAKEFLAGE仍是系统级别的环境变量。
总控Makefile定义的变量传递到下级Makefile中,若是下级Makefile有定义同名的变量。下级Makefile变量值不会被覆盖。若是想要覆盖,执行makefile的时传递参数-e -e, --environment-overrides Environment variables override makefiles. 覆盖makefile环境变量。
make的参数还蛮多的
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make --help
Usage: make [options] [target] ...
Options:
-b, -m Ignored for compatibility.
(忽略的兼容性)
-B, --always-make Unconditionally make all targets.
(无条件完成全部目标)
-C DIRECTORY, --directory=DIRECTORY
Change to DIRECTORY before doing anything.
(在作任何事情以前切换到目录)
-d Print lots of debugging information.
(打印大量调试信息)
--debug[=FLAGS] Print various types of debugging information.
(打印各类类型的调试信息)
-e, --environment-overrides
Environment variables override makefiles.
(环境变量覆盖makefile)
-f FILE, --file=FILE, --makefile=FILE
Read FILE as a makefile.
(将文件读取为makefile)
-h, --help Print this message and exit.
(打印该信息并退出)
-i, --ignore-errors Ignore errors from commands.
(忽略来自命令的错误)
-I DIRECTORY, --include-dir=DIRECTORY
Search DIRECTORY for included makefiles.
(搜索包含makefile的目录)
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
(一次容许N个做业;没有参数的无限做业)
-k, --keep-going Keep going when some targets can't be made. (当一些目标没法达成时,继续执行) -l [N], --load-average[=N], --max-load[=N] Don't start multiple jobs unless load is below N.
(除非负载小于N,不然不要启动多个做业)
-L, --check-symlink-times Use the latest mtime between symlinks and target.
(使用符号连接和目标之间的最新时间)
-n, --just-print, --dry-run, --recon
Don't actually run any commands; just print them. (不要实际运行任何命令;只是打印) -o FILE, --old-file=FILE, --assume-old=FILE Consider FILE to be very old and don't remake it.
(考虑文件是很是旧的,不要从新建立)
-p, --print-data-base Print make's internal database. (打印make的内部数据库) -q, --question Run no commands; exit status says if up to date. (运行任何命令;退出状态表示是否最新) -r, --no-builtin-rules Disable the built-in implicit rules. (禁用内置的隐式规则) -R, --no-builtin-variables Disable the built-in variable settings. (禁用内置变量设置) -s, --silent, --quiet Don't echo commands.
(不要echo命令)
-S, --no-keep-going, --stop
Turns off -k.
-t, --touch Touch targets instead of remaking them.
(触摸目标,而不是重作)
-v, --version Print the version number of make and exit.
(打印make和exit的版本号)
-w, --print-directory Print the current directory.
(打印当前目录)
--no-print-directory Turn off -w, even if it was turned on implicitly.
(关闭-w,即便它是隐式打开的)
-W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
Consider FILE to be infinitely new.
(认为文件是无限新的)
--warn-undefined-variables Warn when an undefined variable is referenced.
(引用未定义的变量时发出警告)
This program built for x86_64-pc-linux-gnu
Report bugs to <bug-make@gnu.org>
复制代码
make 的参数“-k”或“--keep-going”,参数意思是,若是某规则中的命令出错了,那么就终目该规则的执行,但继续执行其它规则。
可是 make 命令中的有几个参数并不往下传递,它们是“-C”,“-f”,“-h”“-o”和“-W”。 当你使用“-C”参数来指定 make 下层 Makefile 时,“-w”会被自动打开的。若是参数中有“-s”(“--slient”)或是“--no-print-directory”,那么,“-w”老是失效的。
makefile中的命令包写法相似C语言中的define
define my_action
touch smile
endef
test:
$(my_action)
复制代码
定义一个my_action 它的功能是建立个smile文件。调用方式也跟引用变量的方式是同样的。make 在执行命令包时,命令包中的每一个命令会被依次独立执行。