Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach to use different CFLAGS for the same source files?

Tags:

makefile

i need to build the same source tree twice,

1 - with normal cflags to build the project binary
2 - with cflags plus -fPIC to build a static library that would be some sort of SDK to develop project dynamic modules.

Using only one Makefile, what is the best approach to accomplish this?

It would be nice to do some sort of :

all: $(OBJECTS)

lib_rule: $(OBJECTS)
   CFLAGS += -fPIC

.cpp.o: 
   $(CC) -c $< -o $@ $(CFLAGS)

But obviously it can't be done.

Thanks

like image 861
Simone Margaritelli Avatar asked Mar 25 '10 17:03

Simone Margaritelli


1 Answers

One thing I've used in the past is a different extension:

.cpp.o:
    $(CC) -c $< -o $@ $(CFLAGS)

.cpp.lo:
    $(CC) -c $< -o $@ $(CFLAGS) $(EXTRA_CFLAGS)

You then build your static library from the .lo files and you binary from the .o files:

prog: a.o b.o

libsdk.a: a.lo b.lo

Assuming you are using GNU Make, you can use some built in functions to only have to maintain the list of objects one time:

OBJS = a.o b.o
LOBJS = $(patsubst %.o, %.lo, $(OBJS))
like image 128
R Samuel Klatchko Avatar answered May 05 '23 07:05

R Samuel Klatchko