"write error: stdout" when calling "make" from Makefile

"write error: stdout" when calling "make" from Makefile

这是我的 Makefile:

SHELL=/bin/bash
.SHELLFLAGS = -e -o pipefail -c 

env:
    make -version | head -1

这就是我在 Ubuntu 20.04.3:

make -version | head -1
GNU Make 4.2.1
make[1]: write error: stdout
make: *** [Makefile:4: env] Error 1

怎么了?

我在 GNU make 4.3 中没有看到这个问题,所以我猜是为了让它工作而进行了一些更改。

要修复它,您可以尝试将规则更改为:

env:
        make -version | cat | head -1

错误是因为 head 程序在读取第一行时关闭了它的标准输入,然后 make 抱怨因为它无法写入它的标准输出(因为它已关闭)。

通过在此处引入 catcat 会在不关闭管道的情况下从 make 读取所有输入,因此 make 不会出现任何错误。

PS。在调用子 make 时,您应该始终使用变量 $(MAKE),永远不要使用硬编码的 make.