Tuesday, June 24, 2014

Makefile template

A makefile to be used as template. It automatically creates header dependencies using the -MMD flag of gcc so the user only needs to specify the source files. Libraries and compiler flags are generated using pkg-config.

CXXFLAGS =    -g  -I/usr/include/blah $(shell pkg-config --cflags thelib)
LIBS     = $(shell pkg-config --libs thelib) -lotherlib 
CXX      =  g++
SOURCES = source.cpp
OBJS = $(SOURCES:.cpp=.o)
DEPS = $(SOURCES:.cpp=.d)

TARGET =    a.out

$(TARGET):    $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all:    $(TARGET)

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET) $(DEPS)

-include $(DEPS)