【问题标题】:How can I automatically add '.s' to a target in my Makefile?如何自动将“.s”添加到 Makefile 中的目标?
【发布时间】:2017-01-27 02:26:35
【问题描述】:

我有一个虚假的debugasm 目标。 debug 通过更新变量 CFLAGS 来工作,然后编译普通目标,它使用这些新的 CFLAGS 来生成调试符号。这按预期工作。

类似地,我想定义asm 目标设置-S 开关并将输出名称从file 更改为file.s。但是,最后一部分不起作用,我得到:

$ make asm -B
gcc -fmessage-length=0 -ansi -pedantic -Werror -Wall -Wextra -Wwrite-strings -Winit-self -Wcast-align -Wcast-qual -Wpointer-arith -Wstrict-aliasing -Wformat=2 -Wmissing-declarations -Wmissing-include-dirs -Wno-unused-parameter -Wuninitialized -Wold-style-definition -Wstrict-prototypes -Wmissing-prototypes -Wdouble-promotion  -S -masm=intel file.c -o file

注意最后一部分 [..] -S -masm=intel file.c -o file 带有额外参数但没有 .s 扩展名。

我错过了什么?

生成文件:

TARGET=file
SOURCE=*.c
HEADERS=*.h
TESTS=*.sh
PROJECT=$(TARGET) $(SOURCE) $(HEADERS) $(TESTS) Makefile
CC=gcc
CFLAGS=\
-ansi \
-pedantic \
-Wall \

# makefile-rules that don't produce files directly
.PHONY: default  all debug asm

default: $(TARGET) 


# Compile 
$(TARGET): $(SOURCE) $(HEADERS) $(TESTS)
    $(CC) $(CFLAGS) $< -o $@

debug: CFLAGS += -DDEBUG -ggdb
debug: $(TARGET)

asm: CFLAGS +=-S -masm=intel
asm: TARGET +=.s <------------------ THIS LINE !!!!!!
asm: $(TARGET) 

【问题讨论】:

  • TARGET +=.s == file .s,注意空格。
  • 您不能这样做,因为特定于目标的变量只能解析为规则的recipe 中的那些值。它们不会解析为先决条件列表中的那些值。见gnu.org/software/make/manual/html_node/Target_002dspecific.html
  • @MadScientist 您可以使用二次扩展并在先决条件列表中解决它。真正的问题是您不能对 目标名称 进行二次扩展,因此您仍然必须指定 $(TARGET).s 作为 CC 配方的额外目标。一旦你这样做了,使用$(TARGET).s 作为asm 的先决条件并完全跳过二次扩展会更干净。
  • 是的,还有 other 方法可以做到这一点:二次扩展、eval 等。但你不能这样做 this 方式:)

标签: makefile gnu-make


【解决方案1】:

您可以在规则中添加一个$(TARGET).s 目标并在asm 中使用它:

$(TARGET) $(TARGET).s: $(SOURCE) $(HEADERS) $(TESTS)
    $(CC) $(CFLAGS) $< -o $@

asm: CFLAGS += -S -masm=intel
asm: $(TARGET).s

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多