Pages

Makefile for Boost

I'm setting up a testing environment for boost::thread on g++ in my Red Hat Linux box. Assuming that the compiler and the Boost libraries are already available, as it is in my case, what it is left to do before programming is creating a Makefile.

The cool thing about make is that you can let it auto-detect almost everithing. In my case I specify just the project name (actually, the name I want to get for the executable), the custom library I'm about to use (boost_thread-mt), and the debugging options (-Wall -g).

As a source names I can say "all the files with .cpp extension", as objects "all the source files, changing their extension to .o", and this is it.

I don't even have to specify the compiler I'm actually using. Make is smart enough to get it by its own.

Here is my Makefile:
# 
# Makefile for some Boost testing
#

MY_NAME := hello
MY_SRCS := $(wildcard *.cpp)
MY_OBJS := ${MY_SRCS:.cpp=.o}
MY_INCLUDE_DIRS :=
MY_LIBRARY_DIRS :=
MY_LIBRARIES := boost_thread-mt

CXXFLAGS += $(foreach includedir,$(MY_INCLUDE_DIRS),-I$(includedir))
CXXFLAGS += -Wall -g
LDFLAGS += $(foreach librarydir,$(MY_LIBRARY_DIRS),-L$(librarydir))
LDLIBS += $(foreach library,$(MY_LIBRARIES),-l$(library))

.PHONY: all clean

all: ${MY_NAME}

$(MY_NAME): $(MY_OBJS)
    $(LINK.cc) $(MY_OBJS) -o $(MY_NAME) $(LDLIBS)

clean:
    @- rm -rf $(MY_OBJS) $(MY_NAME)

OK, there are still a few nuisances. The Makefile syntax is not beautiful, most remarkably the mandatory TAB character in the task lists is a real pain. But, all in all, we can't complain.

No comments:

Post a Comment