Shortly, I want to know if gcc (or g++. I need it in C, but also am curious about c++) defines any special symbols if -g
is enabled. Does it? If so, what symbols?
In the search process I found out that:
_DEBUG
is defined manually (by manually I mean-D_DEBUG
) and is a habit taken from Visual C programmers (because VC defines_DEBUG
when compiling in debug mode)NDEBUG
is defined if NOT in debug mode. Although I found a few places saying this, I tried with my gcc and g++ in .c and .cpp files and in none of them with or without-g
no such symbol was defined!
Edit: Let me demonstrate why I don't want to use a non-standard symbol:
Imagine a kernel module that does something and also provides header files to be included in other kernel modules so that they can connect to this one.
Now as a facility, in one of the header files I have:
#ifdef DEBUG <-- This is what I need
#define LOG(x, ...) printk("Some extra info"x, ##__VA_ARGS__);
#else
#define LOG(x, ...) printk("Without extra info"x, ##__VA_ARGS__);
#endif
Note that the name is not really LOG
, that's an example.
Now, I can use any symbol for DEBUG
myself, but then if someone includes my header, they may not define that symbol. Of course I can tell them "by the way, to get the headers in debug mode, define this other symbol", but that just doesn't sound right to me.
I could define the symbol in a header and include it in all header files. This way, if they include one of my headers, they get the debug symbol too. The problem now is that, if they don't want to compile in debug mode, my headers still think they are in debug mode.
So what I thought would be best was to use a symbol that is defined when -g
is used, if there are any!
Update
So far I came to the conclusion that I could do something like this:
how_to_build.h
#if !defined(NDEBUG)
#define MY_DEBUG
#endif
Usage:
#include "how_to_build.h"
#ifdef MY_DEBUG
// rest of the story
This way, the common option of NDEBUG
removes my definition also. It still requires me to tell them to define it if they don't want to get the headers in debug mode.