Hayashi Yoshiaki
Hayashi Yoshiaki

Reputation: 217

double text substituion in GNU Make

suppose I have source file list like this.

SRCS = A/src/A1.c A/src/A2.c B/src/B1.c B/src/B2.c

I want objects file list from above list.

OBJS = A/obj/A1.o A/obj/A2.o B/obj/B1.o B/obj/B2.o

I want to write something like this.

OBJS = $(SRCS: %1/src/%2 = %1/obj/%2)

But since this feature is not provided in GNU make, I need to think up workaround. Currently I'm using this.

SRCS = $(foreach DIR, A B, $(wildcard $(DIR)/src/*.c))
OBJS = $(foreach DIR, A B, 
         $(addprefix $(DIR)/obj/, 
           $(patsubst %.c, %.o,
             $(notdir $(wildcard $(DIR)/src/*.c))
           )
         )
       )
  1. remove old dir name
  2. do substitution
  3. append new dir name

But this way is obviously complicated so I want a better solution. Anyone know better solution to this?

Upvotes: 1

Views: 55

Answers (1)

Beta
Beta

Reputation: 99144

I would use this:

OBJS := $(subst /src/,/obj/,$(SRCS))

And then fix the suffix:

OBJS := $(patsubst %.c,%.o, $(subst /src/,/obj/,$(SRCS)))

Upvotes: 2

Related Questions