How can I change the value of a boolean macro when I run my program through the command line? For instance, suppose I have the following macro in my cpp file, call it MyCpp.cpp
#define DEBUG 1
How can I change this when I run my program? through the command line:
g++ -Wall -Wextra -o MyCpp MyCpp.cpp
I am pretty sure you specify some kind of command line option, does this ring any bells?
Also, I do NOT want to use argv[]
First, change your source code:
Now you can say on the command line:
The command line argument
-DFOO=barhas the same effect as putting#define FOO barin your source code; you need the#ifndefguard to avoid an illegal redefinition of the macro.Sometimes people use an auxiliary macro to prevent the definition of another macro:
Now say
-DSUPPRESS_FOOto not defineFOOin the code…