【问题标题】:Substitute the value of a Make variables with multiple alterations?用多次更改替换 Make 变量的值?
【发布时间】:2021-12-03 08:58:43
【问题描述】:
#
# Sources are .c and .s files
#
# Append .o to both .c and .s:  
PRJ_OBJ := $(addprefix $(BUILD_PATH)/,$(addsuffix .o, $(PRJ_SRC)))
# Create .c.d from .c.o and .s.d from s.o:
PRJ_DEP_TEMP := $(PRJ_OBJ:.c.o=.c.d)
PRJ_DEP += $(PRJ_DEP_TEMP:.s.o=.s.d)

如何在一行而不是两行中同时替换 .c.o=.c.d.s.o=.s.d

文档:GNU Make Substitution References.

【问题讨论】:

  • PRJ_DEP += $(PRJ_OBJ:%.o=%.d) 也应该可以工作。 % 不区分文件名和扩展名。另请参阅gnu.org/software/make/manual/html_node/Substitution-Refs.html
  • 这两个建议都将替代 .c.o 和 .s.o 文件,这可能不是 OP 的意图。
  • PRJ_DEP = $(patsubst %.c.o,%.c.d,$(patsubst %.s.o,%.s.d,$(PRJ_OBJ))
  • @Danijel -- 您是否关心诸如foo.o 之类的文件(它与.c.o.s.o 模式不匹配?在这种情况下,您希望发生什么?那些文件?请注意,您当前的解决方案将在 PRJ_DEP 中包含 foo.o

标签: makefile gnu-make


【解决方案1】:

如果您的意图是仅获取 .c.o.s.o 文件的 .d 文件,那么您必须执行以下操作:

PRJ_DEP = $(patsubst %.o,%.d,$(filter %.c.o %.s.o,$(PRJ_OBJ)))

$(filter ... 会删除您不想为其创建相应的.d 文件的任何文件,然后您只需使用$(patsubst....o 替换为.d

另一方面,如果您知道PRJ_OBJ 仅包含.c.o.s.o 文件,那么您可以按照@Vroomfondel 或@urcodebetterznow 的建议去做:

PRJ_DEP += $(PRJ_OBJ:%.o=%.d)

【讨论】:

  • PRJ_DEP += $(PRJ_OBJ:%.o=%.d) 工作得很好。
猜你喜欢
  • 2020-01-13
  • 2021-06-06
  • 2016-01-27
  • 2018-06-01
  • 2018-11-09
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多