How do I add conditional compilation to my makefile: say if I have a #ifdef SAVE_DATA in a cpp (.cc) file.
2 Answers 2
Usually something like CXXFLAGS+=-DSAVE_DATA
answered Dec 5, 2009 at 0:59
Richard Pennington
20.1k4 gold badges48 silver badges75 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
For gcc et al, you can use the -Dfoo flag to define foo:
g++ -DSAVE_DATA=1 -c foo.cpp
answered Dec 5, 2009 at 0:59
Dirk is no longer here
370k60 gold badges669 silver badges742 bronze badges
4 Comments
Dirk is no longer here
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.
Steve Jessop
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.
lang-cpp