Re: GNU Make implicit rules



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
.



Relevant Pages

  • Re: Want wrong with my makefile
    ... command line: fatal: cannot open input file ... *** Error code 1 ... I don't have a target .y. ... cc -c $(CFLAGS) solaris_os_specific.c ...
    (comp.unix.solaris)
  • Re: Want wrong with my makefile
    ... *** Error code 1 ... make: Fatal error: Command failed for target '.y' ... I don't have a target .y. ... cc -c $(CFLAGS) solaris_os_specific.c ...
    (comp.unix.solaris)
  • Want wrong with my makefile
    ... command line: fatal: cannot open input file ... *** Error code 1 ... I don't have a target .y. ... cc -c $(CFLAGS) solaris_os_specific.c ...
    (comp.unix.solaris)
  • Re: builworld fails if debugging enabled in usr.sbin/pkg_install/lib
    ... added the contents of the DEBUG variable to CFLAGS. ... I've "fixed" these makefiles to not add DEBUG to CFLAGS. ... Passing DEBUG_FLAGS=-g to ``make installworld'' will also cause the ... for having the debugger symbols in binaries. ...
    (freebsd-current)
  • Re: make: *** No rule to make target
    ... $$(CFLAGS) project.c -o project ... But when I run make from the folder I just get: ... Any hints to what I might be doing wrong? ... target. ...
    (comp.lang.c)