How can I automatically add '.s' to a target in my Makefile?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 07:50:00

问题


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


来源:https://stackoverflow.com/questions/39572468/how-can-i-automatically-add-s-to-a-target-in-my-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!