Makefile书写命令相关内容

一、命令显示

1.@关闭命令的回显
2.make带入参数“-n”或“--just-print”,只是显示命令,但不会执行命令,这个功能方便调试 Makefile。
3.make 参数“-s”或“--slient”全面禁止命令的显示。

二、命令执行

make 逐条执行其后的命令。
如果打算上一条命令结果应用到下一条命令,需要把要执行的命令写在同一行使用分号;隔开。

test:
    cd ./path1
    pwd

运行结果:

root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1
pwd
/home/workspace/my_workspace/study/makefile
test:
    cd ./path1;pwd

运行结果:

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时出现报错,这个并不应该影响到后面命令的执行。

test:
    @mkdir path1
    @echo continue

运行结果:

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

运行结果:

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

四、嵌套执行make

根据功能和模块将相关的文件放置在不同的文件夹中,每个文件夹中单独创建一个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 在执行命令包时,命令包中的每个命令会被依次独立执行。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354