I have been using something like this:
int main(int argc, char *argv[]) { #ifdef DEBUG printf("RUNNING DEBUG BUILD"); #else printf("Running... this is a release build."); #endif ...
However this requires me to compile with -DDEBUG for the debug build. Does GCC give me some way for me to determine when I am compiling with debug symbols (-g flag) such as defining its own preprocessor macro that I can check for?
#ifdef simply tests if the symbol's been defined. #if tests the VALUE of the symbol. so #define FOO 0 will make #ifdef FOO be true, but #if FOO be false, because it's doing #if 0 .
The macro NDEBUG denotes not using debugging information. If NDEBUG is defined, then assert is defined as an expression which does nothing. Otherwise assert will print debugging information if the expression it tests is false.
Answer is no. Usually these macros (DEBUG, NDEBUG, _DEBUG) are set by the IDE/make system depending on which configuration (debug/release) you have active. I think these answers can be of help:
C #define macro for debug printing
Where does the -DNDEBUG normally come from?
_DEBUG vs NDEBUG
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With