Makefile with different compiler flags for source files

How to use different compiler flags for different source files in a Makefile? For example, I need a Makefile that will create:

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 

I currently have about 20 source files (and they are expected to grow), so itโ€™s easy to save the file.

Thanks in advance.

+4
source share
2 answers

You can do something like the following (untested, so the syntax may be a bit 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 $@ 
+11
source

Here is the UNIX / LINUX build file that I wrote and tested on Solaris LINUX to handle different compilation flags for different GNUmakefile partitions. Please let me know if this can be improved. thanks

 # 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 
0
source

Source: https://habr.com/ru/post/1397350/


All Articles