Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

patsubst on makefile

Tags:

makefile

I have to create different *.o files from a same set of *.c using various CFLAGS. I wanted to use patsubst to generate different *.o files from same *.c. I am doing something wrong the following statement, please help (I want to generate one set of object files having ($<)_O0.o and the other ($<)_O2.o from the same set of c source files):

 $(CC) $(CFLAGS_02) -c $< -o $(patsubst %.c,%_O2.o,$<)

Thanks

like image 870
Sayan Avatar asked Feb 11 '11 17:02

Sayan


2 Answers

Use patsubst to make lists of the objects that you want to build, and then use separate rules for each type of build.

Something like this:

SRC_FILES = source1.c source2.c 

OBJ_FILES_O0 = $(patsubst %.c,%_O0.o,$(SRC_FILES)) 
OBJ_FILES_O2 = $(patsubst %.c,%_O2.o,$(SRC_FILES))

CFLAGS_O0 := -O0 
CFLAGS_O2 := -O2

all: $(OBJ_FILES_O0) $(OBJ_FILES_O2)

$(OBJ_FILES_O0): %_O0.o: %.c
    $(CC) $(CFLAGS_O0) -c $< -o $@

$(OBJ_FILES_O2): %_O2.o: %.c
    $(CC) $(CFLAGS_O2) -c $< -o $@
like image 197
ewindes Avatar answered Sep 23 '22 00:09

ewindes


You can also use wild cards to specify all files in the directory.

eg:

#Generic Makefile.

CC := g++

LD := g++

CFLAGS := -c

LDFLAGS := -L<path to lib> -l<libname> \

           -L<path to lib> -l>libname> \
            ......................

ifeq (${TARGETARCH}, debug)

  CFLAGS += -g -O0

elif 

  CFLAGS += -O4 -DNDEBUG

SRCFILES := $(wildcard *.cpp)

OBJFILES := $(patsubst %.cpp, %.o, ${SRCFILES})


all: main

main: ${OBJFILES}

  @echo "[Linking]"$@

  ${LD} ${LDFLAGS} ${OBJFILES}

%.o: %.cpp

  @echo "[Compiling]"$@

  ${CC} ${CFLAGS} $^ -o $@
like image 24
Vijay Nag Avatar answered Sep 23 '22 00:09

Vijay Nag