unh*_*mer 6 makefile exit-code
生成文件:
\n\nfoo.o: foo.cpp\n g++ -o foo.o foo.cpp\nRun Code Online (Sandbox Code Playgroud)\n\nfoo.cpp:
\n\naslkfdj\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\n$ g++ -o foo.o foo.cpp; echo $?\nfoo.cpp:1:1: error: \xe2\x80\x98aslkfdj\xe2\x80\x99 does not name a type\n aslkfdj\n ^\n1\n\n$ make; echo $?\ng++ -o foo.o foo.cpp\nfoo.cpp:1:1: error: \xe2\x80\x98aslkfdj\xe2\x80\x99 does not name a type\n aslkfdj\n ^\nMakefile:2: recipe for target 'foo.o' failed\nmake: *** [foo.o] Error 1\n0\n$ \nRun Code Online (Sandbox Code Playgroud)\n\n我期望 make 的退出代码为 2,因为 g++ 返回非零,参见。
\n\n$ man make|grep exits -A2\n GNU make exits with a status of zero if all makefiles were successfully parsed and no targets that were built failed. A sta\xe2\x80\x90\n tus of one will be returned if the -q flag was used and make determines that a target needs to be rebuilt. A status of two\n will be returned if any errors were encountered.\nRun Code Online (Sandbox Code Playgroud)\n\n我究竟做错了什么?
\n\n作为记录,我在 Xubuntu 15.10 上得到相同的退出代码:
\n\n$ make --version\nGNU Make 4.0\nBuilt for x86_64-pc-linux-gnu\nCopyright (C) 1988-2013 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\nRun Code Online (Sandbox Code Playgroud)\n\n就像在一些旧的 OS X 服务器上一样:
\n\n$ make --version\nGNU Make 3.81\nCopyright (C) 2006 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\n\nThis program built for i386-apple-darwin10.0\nRun Code Online (Sandbox Code Playgroud)\n
Make 明确指出它识别出错误,因此应该返回错误退出代码。
然而,在某些情况下,无论如何这都会导致意想不到的结果。例如,如果存在别名重新定义make,或者echo可能导致echo $?不报告make. 要检查是否是这种情况,当然可以运行alias来检查,type make看看是否有任何别名/shell 函数可能会覆盖该名称,或者简单地反斜杠去掉别名(即运行\make -f ; \echo $?--f不带文件名将导致make失败,而不需要 makefile) 。
一个相关的场景是make不是 make 的实际可执行文件。例如,您可以有一个 shell 脚本来包装make,但不转发退出代码。要检测这种情况,您可以运行which make以查看真正正在运行的可执行文件(除非别名有效)