[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is my Makefile okay?
From: |
Jeffrey Walton |
Subject: |
Re: Is my Makefile okay? |
Date: |
Sun, 8 Jan 2012 22:26:27 -0500 |
On Sun, Jan 8, 2012 at 9:38 PM, Pelias <address@hidden> wrote:
>
> I'm new to GNU make so I would like to know if I made any mistakes in the way
> I have written my Makefile.
> One thing I'm not sure whether to append -DDEBUG/-DNDEBUG to CPPFLAGS or to
> CFLAGS.
>
> # define the variables
>
> CC = g++
> CFLAGS = -Wall
> CPPFLAGS =
> LDFLAGS =
> DOUT = runme_debug
> ROUT = runme
> HEADERS = $(wildcard *.hh )
> SOURCES = $(wildcard *.cc )
> DOBJECTS = $(SOURCES:.cc=.do)
> OBJECTS = $(SOURCES:.cc=.o)
>
> # define the rules
>
> all: $(DOUT)
>
> release: $(ROUT)
>
> $(DOUT): CFLAGS += -g
> $(DOUT): CPPFLAGS += -DDEBUG=1
> $(DOUT): $(DOBJECTS)
> $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^
>
> $(ROUT): CPPFLAGS += -DNDEBUG=1
> $(ROUT): $(OBJECTS)
> $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^
>
> %.o: %.cc $(HEADERS)
> $(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
>
> %.do: %.cc $(HEADERS)
> $(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
>
> debug: $(DOUT)
> ./$(DOUT)
> run: release
> ./$(ROUT)
> test: all
> valgrind $(DOUT)
> clean:
> rm -f *.o
> rm -f *.do
> rm -f $(DOUT)
> rm -f $(ROUT)
>
My makefile would look similar to the following. It allows you to say
`make`, `make debug` and `make release`.
Jeff
#default is Debug
WANT_DEBUG = 1
DEBUG_GOALS = $(filter $(MAKECMDGOALS), debug)
ifneq ($(DEBUG_GOALS),)
WANT_DEBUG := 1
endif
RELEASE_GOALS = $(filter $(MAKECMDGOALS), release all $(DYNAMIC_LIB)
$(STATIC_LIB))
ifneq ($(RELEASE_GOALS),)
WANT_DEBUG := 0
WANT_RELEASE := 1
endif
...
# libstdc++ debug:
http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html
ifeq ($(WANT_DEBUG),1)
# Whoops, ABI compatibility issues with pre-built DSOs
# override CXXFLAGS += -D_GLIBCXX_DEBUG -DDEBUG=1 -g3 -ggdb -O0
override CXXFLAGS += -DDEBUG=1 -g3 -ggdb -O0
endif
ifeq ($(WANT_RELEASE),1)
override CXXFLAGS += -DNDEBUG=1 -g -O2
endif
...
Jeff