Link: http://www.cppblog.com/jialisoft/archive/2012/10/04/192742.html
- # Include <stdio. h>
- IntMain (IntArgc,Char** Argv)
- {
- # Ifdef my_mac
- Printf ("hello-D. \ n ");
- # Else
- Printf ("my_mac was not defined. \ n ");
- # Endif
- Return 0;
- }
The aboveCodeMy_mac macro is used,
[] $ G ++-dmy_mac-O Dtest. c
Execution result:
- [] $./Dtest
- Hello-D.
- [] $
If the-D option is not set, re-compile and execute the result:
- [] $ G ++./Dtest. C-o Dtest
- [] $./Dtest
- My_mac was not defined.
We can use the compiler option to debug ourProgramAO.
What if my macro represents a constant ??
Check the modified Code:
- # Include <stdio. h>
- IntMain (IntArgc,Char** Argv)
- {
- # Ifdef my_mac
- Printf ("hello-D. % d \ n", my_mac );
- # Else
- Printf ("my_mac was not defined. \ n ");
- # Endif
- Return 0;
- }
I output the macro value.
Run the following command on the terminal:
- [] $ G ++-Dmy_mac=5./Dtest. C-o Dtest
- [] $./Dtest
- Hello-D. 5
- [] $
If the program has multiple macros, You can compile them like this.
- [] $ G ++-Dmac1=5 -Dmac2=6Soucefile. c
^ _ ^, Are you very excited ?? In this way, we do not need to modify macro variables in the code ..