I've trying to learn the "best practice" makefile's for a project.
Please review my Makefile file below and suggest changes to enhance it.
The dir layout:
root dir
--- Makefile
deps
--- deps
bin
--- binary
objs
--- all .o files
include
--- all .h files
src
--- all .c .cc files
The makefile:
#
# Generic makefile
#
all: tengine test2
#
# Include files for compiling, and libraries for linking.
#
INC=-I /usr/include -I /usr/local/include -I /usr/include/hiredis
LIB=-lhiredis
#
# Debug or not debug?
#
DEBUG=1
ifdef DEBUG
CFLAGS=-Wall -Winline -pipe -g -DDEBUG #-pedantic -pg
else
CFLAGS=-Wall -Winline -pipe -O3 -march=native -funroll-all-loops \
-finline-functions #-pedantic
endif
#CXXFLAGS=$(CFLAGS)
# Rules for creating dependency files
deps/%.d: src/%.cc
@echo Generating $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -MM -MT '$(patsubst src/%,obj/%,%(patsubst %.cc,%.o,$<))' $< > $@
deps/%.d: src/%.c
@echo Generating $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -MM -MT '$(patsubst src/%,obj/%,%(patsubst %.c,%.o,$<))' $< > $@
# Rules for compilation
#
# C source with header and no c++ code
obj/%.o: src/%.c src/%.h deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(INC) -o $@ -c $<
# C++ source with header.
obj/%.o: src/%.cc src/%.h deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<
# C source without header and no c++ code
obj/%.o: src/%.c deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(INC) -o $@ -c $<
# C++ source without header.
obj/%.o: src/%.cc deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<
# ##############################################################
#
# TARGET: tengine
#
# ##############################################################
OBJS= obj/main.o obj/tengine.o
tengine: $(OBJS)
$(CXX) -pipe $(CXXFLAGS) -o bin/tengine $(OBJS) $(LIB)
# ##############################################################
#
# TARGET: test2
#
# ##############################################################
OBJS= obj/main.o obj/test2.o
test2: $(OBJS)
$(CXX) -pipe $(CXXFLAGS) -o bin/test2 $(OBJS) $(LIB)
# ##############################################################
#
# Cleanup
#
# ##############################################################
clean:
rm -f *~ bin/* obj/* deps/* src/*~ gmon.out
help:
@echo ""
@echo "make - builds tengine"
@echo "make test2 - builds test2"
@echo "make all - builds tengine test2"
@echo "make clean - deletes prior build"