Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append suffix after makefile's automatic variable?

OBJS := a.o b.o c.o

rule : $(OBJS)
  @echo $^
  @echo $^.bc // @echo a.o.bc b.o.bc c.o.bc -> what I want to do

I want to add suffix after automatic variable $^

However, even though I use $^.bc, it shows

a.o b.o c.o.bc

not,

a.o.bc b.o.bc c.o.bc

Are there any ways to do this?

like image 213
sungjun cho Avatar asked Oct 28 '25 07:10

sungjun cho


1 Answers

Problem

The automatic variable $^ is in your case a space-separated list of elements since it expands to a.o b.o c.o. Therefore, echo $^.bc will result in a.o b.o c.o.bc instead of a.o.bc b.o.bc c.o.bc, because the suffix .bc is only appended to the last element of the list, i.e., c.o.

If you want to obtain a.o.bc b.o.bc c.o.bc, then what you want is to add the suffix .bc to every single element of that space-separated list, not just the last one.


Solution

GNU Make's addsuffix builtin function does work on lists:

OBJS := a.o b.o c.o

rule: $(OBJS)
    @echo $^
    @echo $(addsuffix .bc,$^)

Running make on the makefile above:

$ make
a.o b.o c.o
a.o.bc b.o.bc c.o.bc

produces the output you are looking for.

like image 96
ネロク・ゴ Avatar answered Oct 30 '25 21:10

ネロク・ゴ