EddieV223
EddieV223

Reputation: 5313

Is there a cross platform way to detect debug mode compilation?

Is there a cross platform way to detect debug mode compilation? If not, then how to do it for the top compilers; MSVC, GNU & MINGW, mac, clang, intel.

For example MSVC you can detect debug mode like the following.

#if defined _DEBUG
// debug related stuff here
#else
// release related stuff here
#endif

Upvotes: 4

Views: 2280

Answers (2)

roachsinai
roachsinai

Reputation: 536

Below code should be work:

#ifdef defined DEBUG || defined _DEBUG

#else

#endif

Upvotes: -1

Adam H. Peterson
Adam H. Peterson

Reputation: 4601

For many or most compilers, "debug" mode is a multifaceted concept that includes several orthogonal settings. For example, with gcc, you can add debugging symbols to the output code using -g, enable optimizations using -O, or disable assert() macros using -DNDEBUG (to define the NDEBUG macro). In my work, we have deployed production code with many combinations of these enabled or disabled. We have left -g on in order to attach to running processes and troubleshoot them using gdb (in which case we usually have to fight with the spaghetti -O produced), left assertions on to get more information about persistent errors across releases, and disabled optimizations for legacy codebases written under a more permissive interpretation of "undefined behavior" (until we could fix/replace it).

Since the NDEBUG macro actually affects the semantics of the generated code (and some libraries change their ABIs when the macro is defined or not), that's probably the most portable answer to your question. However, if you're using that macro to detect optimized builds portably, you'll probably have mixed success.

Upvotes: 6

Related Questions