stormCloud
stormCloud

Reputation: 993

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.

Upvotes: 5

Views: 9949

Answers (2)

Frank
Frank

Reputation: 1414

Here is UNIX/LINUX makefile which I wrote and tested on Solaris LINUX to handle different compilation flags for different sections of a GNUmakefile. Please let me know if it can be improved. Thank you

# GNUmakefile
#
# makefile for mdRightFielder
#
# Builds:
#   libmdRightFielder.so or libmdRightFielder.sl

ifndef SUB
include ../header.mk

else

VPATH=../Source:../Source/PCRE:../Source/SQLite:../../cpswindows/Source:../../util/mdLicense
INCL=-I../Include -I../Include/PCRE -I../Include/SQLite -I../../cpswindows/Include -I ../../util -I../../util/mdLicense
APIOBJ=cGlobalDataDestructor.o cPCRE.o CppInterface.o cRightFielder-FillTokenGaps.o
PCREOBJ=pcre_chartables.o pcre_compile.o pcre_exec.o pcre_fullinfo.o pcre_get.o pcre_globals.o pcre_newline.o \
    pcre_tables.o pcre_try_flipped.o
SQLITEOBJ=sqlite3.o
CPSWINDOWSOBJ=BinarySearch.o cConfigFile.o cCriticalSection.o cDateTime.o cException.o cFile.o cSQLite.o \
    QuickSort.o StringFunctions.o
MDLICENSEOBJ=CBigNum.o mdLicense.o RSA.o

ifeq ($(CPU),sparc)
    ifdef workshop
        CALIGN=-xmemalign=1s 
        ifdef release
            CXXALIGN=-Qoption cg -xmemalign=1s
        else
            CXXALIGN=-Qoption ccfe -y-xmemalign=1s
        endif
    endif
endif

COMPILER_FLAGS_1=-D_NO_GUI 
COMPILER_FLAGS_2=-D_NO_GUI -DHAVE_CONFIG_H 


CXXFLAGS+=-DPCRE_STATIC -DUSE_STATIC
CFLAGS+=-D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
DEPFLAGS+=-DLINK_SIZE=2 -D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC

.PHONY: all clean

all:    libmdRightFielder.so
    cp -fp ../Include/mdRightFielder.h ../../util/mdEnums.h libmdRightFielder.so $(SHIP)
    if [ `uname` = HP-UX ] ; \
    then \
      /bin/mv -f $(SHIP)/libmdRightFielder.so $(SHIP)/libmdRightFielder.sl ; \
    fi

clean:
    rm -f *.o *.so *.sl deps
    rm -f core core.[0-9]*

$(APIOBJ): CXXFLAGS+=$(COMPILER_FLAGS_1) 
$(PCREOBJ): CXXFLAGS+=$(COMPILER_FLAGS_2) 

MARYOBJS = $(APIOBJ) $(PCREOBJ)



libmdRightFielder.so: \
    $(MARYOBJS)
     -$(CXX) $(CXXFLAGS) $(INCL) $(SHARED) $^ -o $@ $(LDLIBS)

mary:
    %.o : %.cpp  # cancel implicit CPP compilation rule

        %.o :  %.cpp
    $(CXX) $(CXXFLAGS) $(INCL) $(PIC) $< -o $@ -c


        %.o : %.c     # cancel implicit C compilation rule

        %.o : %.c
    $(CC) $(CFLAGS) $(INCL) $(PIC) $< -o $@ -c

endif

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272447

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 $@

Upvotes: 12

Related Questions