How to define C++ preprocessor variable in Makefile

C++Makefile

C++ Problem Overview


I have a C++ preprocessor written like this:

  #ifdef cpp_variable
   //x+y;
  #endif

Can anyone tell me how to define this in Makefile.

C++ Solutions


Solution 1 - C++

This is compiler specific.

GCC uses -Dcpp_variable=VALUE or just -Dcpp_variable

Microsoft's compilers use /D

Solution 2 - C++

Search your compiler documentation to find how to do that.

For example for g++ the syntax is :

g++ -Dcpp_variable <other stuff>

Which corresponds to adding

CPPFLAGS += -Dcpp_variable

in your makefile.

Solution 3 - C++

Add to Makefile:

CPPFLAGS = -Dcpp_variable

Solution 4 - C++

The syntax is compiler specific, for gcc use the -D option like so: -Dcpp_variable.

Solution 5 - C++

Take a variable in Makefile and whatever you need to define in it just add -DXXX. Where XXX in you case is cpp_variable.

For example

COMPILE_OPTS = -DXXX

g++ -c $(COMPILE_OPTS) $<

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoelView Question on Stackoverflow
Solution 1 - C++Reed CopseyView Answer on Stackoverflow
Solution 2 - C++fouronnesView Answer on Stackoverflow
Solution 3 - C++Peter TsengView Answer on Stackoverflow
Solution 4 - C++Eugen Constantin DincaView Answer on Stackoverflow
Solution 5 - C++Akhil PathaniaView Answer on Stackoverflow