fandyushin
fandyushin

Reputation: 2432

Makefile: How to change prefix of var?

I have project folder with src, obj and inc dirs.

I declare var with obj - OBJS

SDIR = src
ODIR = obj

# I change /src/*.c to /obj/*.o
_OBJS = $(patsubst %.c, %.o, $(wildcard $(SDIR)/*.c))
# I need to change /src/*.o to /obj/*.o
OBJS = $(??? $ODIR ??? $_OBJS ???)

Now in _OBJS - ./src/*.o, How to change in OBJS /src/ to /obj/?

Thanks.

Upvotes: 3

Views: 1920

Answers (2)

urban
urban

Reputation: 5692

What about this (if I understand the question correctly):

SDIR = src
ODIR = obj

# I change /src/*.c to /obj/*.o
_OBJS = $(patsubst %.c, %.o, $(wildcard $(SDIR)/*.c))
# I need to change /src/*.o to /obj/*.o
_OBJS := $(subst $(SDIR), $(ODIR), $(_OBJS))

debug:
    @echo $(_OBJS)

Output/Test:

$ mkdir ./src/
$ touch ./src/a.c
$ touch ./src/b.c
$ touch ./src/c.c
$ ls ./src/
a.c  b.c  c.c
$ make debug
obj/a.o obj/b.o obj/c.o

Upvotes: 4

fandyushin
fandyushin

Reputation: 2432

_OBJS = $(patsubst %.c, %.o, $(wildcard $(SDIR)/*.c))
_OBJS := $(notdir $(_OBJS)) 
OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS))

But, I think, its bad way

Upvotes: 1

Related Questions