Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ determine if compiling with debug symbols without defining a preprocessor symbol

Tags:

c++

c

gcc

debugging

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?

like image 748
Steven Lu Avatar asked Mar 07 '11 17:03

Steven Lu


People also ask

What is Ifdef debug?

#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 .

What is Ndebug?

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.


1 Answers

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

like image 101
JoeSlav Avatar answered Sep 18 '22 17:09

JoeSlav