DennisJohnson
DennisJohnson

Reputation: 68

Makefile g++ compile with -D variable from command line with default value

I'm trying to setup a makefile which will take a command line argument and pass it on to the compiler with the -D flag. Also, set a default value for the variable.

e.g.

$: Make program FOO=12

and this will compile with -DFOO=12

Also, if FOO is not given at the command line how can I set a default value? I am working with c++ so I can do

#ifndef FOO 

#define FOO 12

#endif 

But I would like a way to have a default value inside the makefile.

Upvotes: 0

Views: 1574

Answers (2)

xyz347
xyz347

Reputation: 1

ifeq ($(FOO),)         # check if FOO is not defined
CFLAGS= -DFOO=999      # if not defined, set a default value
else
CFLAGS= -DFOO=$(FOO)   # or use the value from argument
endif

Upvotes: 1

shingo
shingo

Reputation: 27307

Redefined macro is a warning, so if you aren't care about warnings, just add the default value into the flags, and append the user input one next, it will cover the default value.

If you don't like warnings, use -U argument.

-D FOO=1 -U FOO -D FOO=2

Upvotes: 0

Related Questions