Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile with different compiler flags for source files

How do I use different compiler flags for different source files in a Makefile? For example, I'd like a Makefile that will produce:

g++ -c -COMPILER_FLAGS_1   -g source1.cpp -o source1.o

g++ -c -COMPILER_FLAGS_2   -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2   -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2   -g source4.cpp -o source4.o

g++ -c -COMPILER_FLAGS_3   -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3   -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3   -g source7.cpp -o source7.o

g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o 

At the moment I've got about 20 source files (and that's expected to grow), so an easy to maintain file would be nice.

Thanks in advance.

like image 933
stormCloud Avatar asked Feb 20 '12 01:02

stormCloud


1 Answers

You could do something like the following (untested, so the syntax might be slightly off):

OBJS_1 := source1.o
OBJS_2 := source2.o source3.o source4.o
OBJS_3 := source5.o source6.o source7.o

OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)

output: $(OBJS)
    $(CXX) -g -o $@ $^

$(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
$(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
$(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)

$(OBJS): %.o: %.cpp
    $(CXX) -c $(CXXFLAGS) -g $< -o $@
like image 124
Oliver Charlesworth Avatar answered Nov 11 '22 21:11

Oliver Charlesworth