Re: GNU Make implicit rules
- From: Larry I Smith <larryXiXsmith@xxxxxxxxxxx>
- Date: Sun, 26 Mar 2006 19:18:25 GMT
root wrote:
I wish to build both debug and release builds of my programs with
a makefile (simplified version below).
I have a problem when using implicit rule though in that I wish to
have separate CFLAGS for both my debug and release builds.
How should I specify implicit rule correctly so that my CFLAGS_DBG
gets picked up when building the debug target?
Any help with this appreciated.
CC = g++
# define CFLAGS for all targets except 'debug'
CFLAGS = -O0 -g -I/usr/include/SDL -I/usr/X11R6/include
# define CFLAGS just for target 'debug' and all of its
# dependents (i.e. OBJECT when 'debug' is the target)
debug : CFLAGS = -O3 -I/usr/include/SDL -I/usr/X11R6/include
LDFLAGS = -L/usr/X11R6/lib -lSDL -ldl -lm -lX11
OBJECT = main.o dialog.o cfgparser.o sprite.o ai.o actor.o
release: $(OBJECT)
$(CC) -o release.bin $(LDFLAGS) $(OBJECT)
debug: $(OBJECT)
$(CC) -o debug.bin $(LDFLAGS) $(OBJECT)
.cpp.o:
$(CC) -c $(CFLAGS) $<
all: release
Make the changes shown above, then:
do 'make' to build target 'release' using the non-debug
verion of CFLAGS.
'make release' or 'make all' will also do the same thing.
do 'make debug' to build target 'debug' (and OBJECT) using the
debug version of CFLAGS.
Be sure to delete '*.o' between 'release' and 'debug' builds;
otherwise 'make' will not rebuild them using the different
CFLAGS value.
Nit - GNU Make already has appropriate built-in rules.
You are overriding more than is necessary.
CC and CFLAGS are for C files; CXX and CXXFLAGS are for C++
files; CPPFLAGS are for C/C++ preprocessors.
Your example makefile could be:
# add my stuff to the default CPPFLAGS
CPPFLAGS += -I/usr/include/SDL -I/usr/X11R6/include
# add my stuff to the default LDFLAGS
LDFLAGS += -L/usr/X11R6/lib -lSDL -ldl -lm -lX11
OBJECT = main.o dialog.o cfgparser.o sprite.o ai.o actor.o
# the default target is release.bin
all : release.bin
# add's to default CXXFLAGS for debug.bin and its .o's
debug.bin : CXXFLAGS += -O0 -g
# debug.bin depends on OBJECT
debug.bin : $(OBJECT)
# add's to default CXXFLAGS for release.bin and its .o's
release.bin : CXXFLAGS += -O3
# release.bin depends on OBJECT
release.bin : $(OBJECT)
clean :
rm $(OBJECT)
Specific compile and Link rules do not need to be specified,
'make' already has appropriate defaults - once you
set LDFLAGS, LDLIBS, CPPFLAGS, CXXFLAGS, etc as desired
You can get much more sophisticated than the above.
You can put the debug and release .o's in seperate
sub-directories, etc, etc.
Spend some time reading the Make docs (info make).
Regards,
Larry
.
- References:
- GNU Make implicit rules
- From: root
- GNU Make implicit rules
- Prev by Date: Re: GNU Make implicit rules
- Next by Date: Re: timer with C
- Previous by thread: Re: GNU Make implicit rules
- Index(es):
Relevant Pages
|