tags:

views:

187

answers:

2

How do I add conditional compilation to my makefile: say if I have a #ifdef SAVE_DATA in a cpp (.cc) file.

+3  A: 

Usually something like CXXFLAGS+=-DSAVE_DATA

Richard Pennington
+3  A: 

For gcc et al, you can use the -Dfoo flag to define foo:

g++ -DSAVE_DATA=1 -c foo.cpp

Dirk Eddelbuettel
Note that modifying CXXFLAGS is how you pass options to the C++ compiler in make.
Roger Pate
Yes that is one of many possibilities, but you can also use it implicitly as argument to the command ultimately using as I did here.
Dirk Eddelbuettel
I tend to do both: if I define a custom rule for particular cpp files, I write it as `$(CXX) $(CPPFLAGS) $(CXXFLAGS) -special -flags -c -o $@ $<`. Or sometimes with the special flags before CXXFLAGS, it's kind of the same issue as !important in CSS.
Steve Jessop
Of course you can use target-specific variables, but in my experience they cause more confusion than they save. Maybe I should have stuck with them longer.
Steve Jessop