Joel
Joel

Reputation: 621

How to define a C++ preprocessor variable in Makefile

I have a C++ preprocessor written like this:

  #ifdef cpp_variable
   //x+y;
  #endif

How can I define this in Makefile?

Upvotes: 62

Views: 87466

Answers (6)

Pawan Kumar
Pawan Kumar

Reputation: 309

If you want to do the below in a C/C++ file,

#define VARIABLE_NAME 2

then, in the Makefile, use the below lines:

CDEFS += -DVARIABLE_NAME = 2

Upvotes: -1

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

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

Upvotes: 5

fouronnes
fouronnes

Reputation: 4038

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.

Upvotes: 40

Reed Copsey
Reed Copsey

Reputation: 564861

This is compiler specific.

GCC uses -Dcpp_variable=VALUE or just -Dcpp_variable

Microsoft's compilers use /D

Upvotes: 62

Akhil Pathania
Akhil Pathania

Reputation: 752

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

Upvotes: 4

Peter Tseng
Peter Tseng

Reputation: 14003

Add to Makefile:

CPPFLAGS = -Dcpp_variable

Upvotes: 12

Related Questions