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

GNU Make中以点开头的目标(.test7)无法默认执行的问题及解决

GNU Make默认目标以点开头时的异常问题及解决方法

问题描述

我编写了如下Makefile,其中目标名称NAME = .test7以.开头:

# 问题似乎出在文件名中的".",因为
# 当NAME = test7时可以得到预期结果!
# 不过,我查阅GNU make命令的特殊内置目标名称章节后,
# 并未找到相关说明?

NAME        =   .test7

$(NAME):
    @echo Making $(NAME)...
    @touch $(NAME)
    @echo Done.

hello:
    @echo Hello :\)

all: $(NAME)
    @:

clean fclean:
    rm -r $(NAME)

re: fclean all
    @:

.PHONY: all clean fclean re

直接运行make时,无论首次还是多次执行,输出都是:

Hello :)

明明第一条规则是$(NAME),却被直接忽略。但当NAME = test7(不以.开头)时,首次运行make会正常执行该目标:

Making test7...
Done.

后续运行则提示:

make: 'test7' is up to date.

这是预期行为吗?我查阅了GNU Make的特殊内置目标相关文档,并未找到相关说明。请问如何让.test7目标的表现与普通名称目标一致?

解决方案

通过显式设置.DEFAULT_GOAL指定默认目标,修改后的Makefile如下:

NAME        =   .test7

.DEFAULT_GOAL := $(NAME)

$(NAME):
    @echo Making $(NAME)...
    @touch $(NAME)
    @echo Done.

hello:
    @echo Hello :\)

all: $(NAME)

clean fclean:
    rm -r $(NAME)

re: fclean all
    @:

.PHONY: all clean fclean re default_target

运行结果

  • 首次直接运行make:
Making .test7...
Done.
  • 后续运行make:
make: '.test7' is up to date.
  • 运行make all:
make: Nothing to be done for 'all'.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 17:30:01