How can I automatically add '.s' to a targ

2019-09-08 10:07发布

问题:

I have a phony debug and asm target. debug works by updating the variable CFLAGS and then compiles the normal target, which uses these new CFLAGS to produce debug symbols. This works as intended.

Analogously I want to define the asm target set the -S switch and to change the output name from file to file.s. However, the last part does not work, and I get:

$ 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

notice the last part [..] -S -masm=intel file.c -o file with the extra parameters but without the .s extension.

What am I missing?

Makefile:

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) 

回答1:

You could just add a $(TARGET).s target to the rule and use that in asm:

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

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