54

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,163
  • 4
  • 29
  • 60
Joel
  • 541
  • 1
  • 4
  • 4

5 Answers5

52

This is compiler specific.

GCC uses -Dcpp_variable=VALUE or just -Dcpp_variable

Microsoft's compilers use /D

Itay Grudev
  • 6,684
  • 4
  • 50
  • 84
Reed Copsey
  • 539,124
  • 75
  • 1,126
  • 1,354
36

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
  • 48,392
  • 6
  • 100
  • 153
fouronnes
  • 3,628
  • 21
  • 39
  • 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
  • 12,827
  • 3
  • 65
  • 56
5

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

Eugen Constantin Dinca
  • 8,813
  • 2
  • 31
  • 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
  • 622
  • 2
  • 5
  • 15