help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make question: Specify which CXXFLAGS to use


From: William Payne
Subject: Re: Make question: Specify which CXXFLAGS to use
Date: Thu, 14 Oct 2004 16:12:38 +0200

"Glynn Clements" wrote
> "William Payne" <mikas493_no_spam@student.liu.se> writes:
>
>> Hello, consider this Makefile:
>>
>> CXX = g++
>> LD = g++
>> CXXFLAGS_DEBUG = -Wall -W -ansi -pedantic -g -ggdb -c -o
>> CXXFLAGS_RELEASE = -Wall -W -ansi -pedantic -O -c -o
>> LDFLAGS_DEBUG = -mwindows -lole32 -L/usr/local/gcc-3.4.2/lib/ -o $(EXEC)
>> LDFLAGS_RELEASE = -s -mwindows -lole32 -L/usr/local/gcc-3.4.2/lib/ -o
>> $(EXEC)
>> EXEC = debug_file_remover
>> OBJECTS = \
>>  main_dialog_procedure.o \
>>  selected_directory_edit_box_procedure.o \
>>  winmain.o \
>>  debug_file_remover.res
>>
>> debug: $(OBJECTS)
>>  $(LD) $(OBJECTS) $(LDFLAGS_DEBUG)
>>
>> release: $(OBJECTS)
>>  $(LD) $(OBJECTS) $(LDFLAGS_RELEASE)
>>
>> %.o: %.cpp
>>  $(CXX) $(CXXFLAGS) $@ $<
>>
>> %.res: %.rc
>>  windres $< -O coff -o $@
>>
>> clean:
>>  rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
>>
>> My problem is that depending on whether the user is doing "make debug" or
>> "make release", I want to specify which CXXFLAGS_ to use.
>> Can I have a variable, say $CXXFLAGS, that I set to either 
>> $CXXFLAGS_DEBUG
>> or RELEASE as appropiate? If so, how?
>
> It's better (and probably easier) to have separate names for the debug
> and release versions of the object files, e.g.:
>
> DEBUG_OBJECTS = $(patsubst %.o,dbg/%.o,$(OBJECTS))
> RELEASE_OBJECTS = $(patsubst %.o,dbg/%.o,$(OBJECTS))
>
> dbg/%.o: %.cpp
>                $(CXX) $(DEBUG_CXXFLAGS) -o $@ $<
>
> rel/%.o: %.cpp
>                $(CXX) $(RELEASE_CXXFLAGS) -o $@ $<
>
> debug: $(DEBUG_OBJECTS)
>        $(LD) $(LDFLAGS_DEBUG) $^
>
> release: $(RELEASE_OBJECTS)
>        $(LD) $(LDFLAGS_RELEASE) $^
>
> This has the advantage that "make debug ; make release" will do the
> right thing, i.e. build each target from the correct set of .o files,
> rather than building the release target from the pre-existing (debug)
> versions of the .o files.
>
> -- 
> Glynn Clements <glynn.clements@virgin.net>

Thanks Glynn, based on your input I've constructed the following Makefile:
CXX = g++
LD = g++
CXXFLAGS_DEBUG = -Wall -W -ansi -pedantic -g -ggdb -c -o
CXXFLAGS_RELEASE = -Wall -W -ansi -pedantic -O -c -o

LDFLAGS_DEBUG = -mwindows -lole32 -L/usr/local/gcc-3.4.2/lib/ -o 
$(EXEC_DEBUG)
LDFLAGS_RELEASE = -s -mwindows -lole32 -L/usr/local/gcc-3.4.2/lib/ -o 
$(EXEC_RELEASE)

EXEC_DEBUG = debug/debug_file_remover
EXEC_RELEASE = release/debug_file_remover

OBJECTS = \
 main_dialog_procedure.o \
 selected_directory_edit_box_procedure.o \
 winmain.o \
 debug_file_remover.res

DEBUG_OBJECTS = $(patsubst %.o,debug/%.o,patsubst 
%.res,debug/%.res,$(OBJECTS))
RELEASE_OBJECTS = $(patsubst %.o,release/%.o,patsubst 
%.res,debug/%.res,$(OBJECTS))

debug: $(DEBUG_OBJECTS)
 $(LD) $(DEBUG_OBJECTS) $(LDFLAGS_DEBUG)

release: $(RELEASE_OBJECTS)
 $(LD) $(RELEASE_OBJECTS) $(LDFLAGS_RELEASE)

debug/%.o: %.cpp
 $(CXX) $(CXXFLAGS_DEBUG) $@ $<

release/%.o: %.cpp
 $(CXX) $(CXXFLAGS_RELEASE) $@ $<

%.res: %.rc
 windres $< -O coff -o $@

clean:
 rm -f debug/$(OBJECTS) debug/$(EXEC) /release/*.stackdump
 rm -f release/$(OBJECTS) release/$(EXEC) release/*.stackdump
 rm -f *~

It has two problems. First, make clean doesn't seem to actually clean up the 
sub-directories debug and release, how should I rewrite the "clean" rule so 
it does? Secondly, I tried making so the .res file goes into its respective 
subdirectory as well, but when I try for example:
$ make debug
make: *** No rule to make target `patsubst', needed by `debug'.  Stop.
So I have a syntax error as well (I haven't seen patsubst before) and I 
would like to fix that (and make the .res file go into the same directory as 
the .o files).

Thanks for any replies

/ WP 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]