1

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

asked Dec 5, 2009 at 0:52

2 Answers 2

4

Usually something like CXXFLAGS+=-DSAVE_DATA

answered Dec 5, 2009 at 0:59
Sign up to request clarification or add additional context in comments.

Comments

3

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

4 Comments

Note that modifying CXXFLAGS is how you pass options to the C++ compiler in make.
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.
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.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.