Bunyod Shams
Bunyod Shams

Reputation: 33

(Makefile) Multiple string replacement on the same line

I have working makefile code which first finds all the files in the u folder, then removes ./u/ and finally replaces the back extension of .c to .o . Is there a way to make this a one or two-liner instead? I feel that this code looks messy and could be improved.

UTILS=$(wildcard ./u/*.c)
TEMP=$(UTILS:./u/%=%)
OBJ=$(TEMP:.c=.o)

Upvotes: 0

Views: 1158

Answers (1)

larsks
larsks

Reputation: 311655

You can't nest $(var:x=y) style replacements, but you can nest the equivalent patsubst function calls, so you could write this:

OBJ=$(patsubst %.c, %.o, $(patsubst ./u/%,%,$(wildcard ./u/*.c)))

Which you can simplify to:

OBJ=$(patsubst ./u/%.c,%.o,$(wildcard ./u/*.c))

But with that simplification in mind, you can do the same thing with the original version:

UTILS=$(wildcard ./u/*.c)
OBJ=$(UTILS:./u/%.c=%.o)

Which is maybe easier to read.

Upvotes: 1

Related Questions