You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Makefile多行Bash命令中如何注释整行内容?

Makefile多行命令中整行注释的正确方法

在Makefile里,如果有如下的多行续行命令:

<line n content>    \
<line n+1 content>  \
<line n+2 content>  \
<line n+3 content>

想要让Shell最终收到的内容是 <line n content> <line n+2 content> <line n+3 content>(也就是去掉line n+1这行),直接给line n+1加#还保留末尾反斜杠的做法是无效的:

<line n content>    \
# <line n+1 content>  \
<line n+2 content>  \
<line n+3 content>

这种写法会让Make把# <line n+1 content> \整个传给Shell,Shell会把#后面的所有内容(包括后续行)都当作注释,导致line n+2和line n+3也被忽略,达不到预期。

正确的注释方式

给要注释的行开头加#,同时删掉该行末尾的反斜杠,示例:

<line n content>    \
# <line n+1 content>
<line n+2 content>  \
<line n+3 content>

原理说明

Makefile会自动忽略以#开头的注释行,而line n末尾的反斜杠表示“续行”,Make会跳过注释行,直接将line n和下一个非注释行(line n+2)拼接,再继续和line n+3拼接,最终传给Shell的就是去掉line n+1后的完整命令行,完全符合需求。

内容的提问来源于stack exchange,提问作者user21026706

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:55:21