gcc gcc: fatal error: no input files compilation terminated. make: *** [main.o] Error 4
但是,如果我有一个管道,则管道中最后一个命令的退出状态将被采用.例如,gcc |猫不会因为猫成功而失败.
我知道整个管道的退出代码存储在PIPESTATUS数组中,我可以用${PIPESTATUS [0]}获取错误代码4.我应该如何构造我的makefile来处理一个管道命令,并正常退出失败?
在评论中,另一个例子是gcc | grep的东西在这里,我认为最需要的行为仍然是gcc,只有gcc导致失败,如果没有找到任何东西,则不会grep.
解决方法
在GNU Make 3.81(大概早些虽然我不知道肯定),你应该能够用SHELL = / bin / bash -o pipefail这样做.
在GNU Make 3.82(和更新版本)中,您应该可以使用SHELL = / bin / bash和.SHELLFLAGS = -o pipefail -c(尽管我不知道如何添加-c到最后是必要的,如果make将为您添加,即使指定.SHELLFLAGS.
从bash手册页:
The return status of a pipeline is the exit status of the last command,unless the pipefail option is enabled. If pipefail is enabled,the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands exit successfully. If the reserved word ! precedes a pipeline,the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.