C ++ adds debugging code that only runs when debugging

How the question is explained: I would like to add debugging code that runs only when the program is connected to the debugger. I would suggest that this variable flageither pre-processorwill be different for each compiler ...

In my case, I am using Microsoft Visual Studio 2010 with C ++.

I also use Eclipse on another computer at home running Ubuntu 10.4 and again in C ++.

+3
source share
4 answers

This question may mean 1 out of 2 things:

  • Code that only works based on build configuration (e.g. Release vs. Debug)
  • ,

, , (, _ DEBUG Win32 CRT).

,

.

, , - , false, :

bool gDebug = false;

, gDebug true . , , , true:

if (gDebug)
{
    // Debugger is attached, so run this code
    // ...
}

DWORD, 0, 1 .

, 1.. , , , .

+2

, - debug/release, debug ( , MSV++ CRT - ) _DEBUG, , /.

, IsDebuggerPresent API, , .

, 100% , . , / , (, , OutputDeubgString ..). , , , .

, , , "" . , , (, Windows), .

+2

in_debugger - false . true. true.

0

Source: https://habr.com/ru/post/1775315/


All Articles