1

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

Bi.
  • 1,846
  • 8
  • 25
  • 34

2 Answers2

4

Usually something like CXXFLAGS+=-DSAVE_DATA

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
3

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

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

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Note that modifying CXXFLAGS is how you pass options to the C++ compiler in make. –  Dec 05 '09 at 01:20
  • 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 Dec 05 '09 at 01:47
  • 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 Dec 05 '09 at 02:21
  • 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 Dec 05 '09 at 02:26