0

The question is as the title, I'm compiling a quite big code and want to have a debug option only when compiled with such option.

I'm using make to compile and I add the compile option on my debug rule:

debug: main
$(eval CFLAGS := -D_DEBUG_)

What I could not do was use this _DEBUG_ macro inside the code to then print some debug info.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ACoelho
  • 341
  • 1
  • 11
  • Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/q/1449181) – Jonathan Leffler Nov 10 '20 at 17:20
  • What do you mean by "I could not do"? Did the compiler issue an error? Please always include the actual system message(s) of your problem as described in [MCVE]. – Vroomfondel Nov 11 '20 at 12:53

1 Answers1

4

Passing -Dmacro to a compiler is equivalent to defining this symbol in the code:

#define macro

So "using" it in the code is similar as well - you can use preprocessor conditional directives:

#ifdef macro
// Your conditional code
#endif

(in your specific case the symbol name is _DEBUG_)

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61