basic GNU makefile for small c++ prog
Posted on 2007-11-27
This is under linux using GNU tools and in particular GNU make (I don't care about Makefile portability) What's the "best" makefile for a small c++ project made of 3 c++ files and using a third party module (with its includes and lib)?
Let's starting with this
MYINCDIR = /usr/blabla/inc
MYLIBDIR = /usr/blabla/lib
LDFLAGS = -L$(MYLIBDIR) -lmylib -lm -lpthread
CPPFLAGS = -m64 -O -fPIC -fexceptions -DNDEBUG -I$(MYINCDIR)
OBJS=a.o b.o c.o
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS)
rm -f $(PROG) $(OBJS)
GNU make has several implicit rules and maybe tricks that I don't know. Can you make suggestions to improve this makefile with the following aims:
- keep it small and short, exploiting gnu make implicit rules and mechanisms (but keeping compilation and linking separate)
- avoid the explicit rule for linking. Note that by default gnu make use gcc (instead of g++) for linking .o files (but that wouldn't work on a c++ project) and also note that if the implicit rule passes the -l options before the OBJS list, that can also break the linking. That's why in the sample above here I had to use an explicit rule. But maybe there is a better way.
- I'd prefer to specify the list of source files, rather than the list of intermediate .o files
- is there a better way to specify additional include dirs (maybe using .INCLUDE_DIRS?) and library dirs?
- is there any automatic mechanism for handling .h dependencies