Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile: How to change prefix of var?

Tags:

makefile

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.

like image 362
fandyushin Avatar asked Dec 06 '25 15:12

fandyushin


2 Answers

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
like image 175
urban Avatar answered Dec 08 '25 23:12

urban


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

But, I think, its bad way

like image 37
fandyushin Avatar answered Dec 09 '25 00:12

fandyushin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!