如何在makefile中不将注释打印在输出中

51

我有一个像这样的makefile:

install:
    @somecommand

    #some explanation for next command
    @lastcommand

问题是当我执行make install时,注释#some explanation for next command被打印出来了。如何在Makefile中添加一个不会被打印的注释?也许我正在寻找类似于Windows下的echo off的Unix等效方法?

(实际上,这与这个问题相反.)

2个回答

87

不要缩进注释——当行以制表符开头时,它是由shell执行的命令(而shell将注释视为注释)。

概念证明(ss.mk):

all:
    echo "This is the first command"
    # This comment is echoed

# This comment is not echoed
    echo "This is the second command"

样例输出:

$ make -f ss.mk
echo "This is the first command"
This is the first command
# This comment is echoed
echo "This is the second command"
This is the second command
$

21
如果您真的想缩进一个以“@”为前缀的注释,它也不会显示。 - cmbuckley
11
这是正确的,但这种写注释的方式非常笨重,因为make会分叉并执行一个shell来扫描注释,推断它是一条注释,然后成功退出...除非make针对这个做了优化,但很可能不会。 - Jonathan Leffler
是的...之前我的评论结尾大概是“...但那可能有点荒谬”。不确定为什么我删掉了它! - cmbuckley
不缩进的问题在于你无法确定编辑器的制表符大小,因此无法美观地对齐内容(如命令行开关)。 - Des Nerger

3

我尝试了这个方法,它完美地运行:

test:
    @echo "test 1"
    @# echo "test 2"
  • 输出:
$ make test

test 1
  • 处理错误:
    如果您想确保执行所有行,应在可能会停止make文件的行开头使用-
test:
    @echo test1
    -dkfhfwefkwef
    @echo test2
    @-jddhwehjwejfw
    @echo test3

$ make test

test1
dkfhfwefkwef
make: dkfhfwefkwef: No such file or directory
make: [Makefile:3: test] Error 127 (ignored)
test2
make: jddhwehjwejfw: No such file or directory
make: [Makefile:5: test] Error 127 (ignored)
test3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接