58

I have a C++ preprocessor written like this:

  #ifdef cpp_variable
   //x+y;
  #endif

Can anyone tell me how to define this in Makefile.

vikrant rana
  • 4,509
  • 6
  • 32
  • 72
Joel
  • 581
  • 1
  • 4
  • 4

6 Answers6

56

This is compiler specific.

GCC uses -Dcpp_variable=VALUE or just -Dcpp_variable

Microsoft's compilers use /D

Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
37

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.

eddi
  • 49,088
  • 6
  • 104
  • 155
fouronnes
  • 3,838
  • 23
  • 41
  • 2
    Technically, because you can do it from the commandline, it can be done from the makefile -- just put the relevant command in the makefile. (pedantry aside, this comment may not have actually been correct when this answer was written) – Nic May 09 '16 at 12:47
12

Add to Makefile:

CPPFLAGS = -Dcpp_variable
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
5

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

Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51
4

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) $<

Akhil Pathania
  • 542
  • 2
  • 5
  • 19
0

if you want to do below in c/cpp file

#define VARIABLE_NAME 2 

then, in make file, use below lines:

CDEFS += -DVARIABLE_NAME = 2
Pawan Kumar
  • 114
  • 9