Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to separate debug and release mode code

During debug mode or while I am doing testing, I need to print lots of various information, so i use this method:

#ifdef TESTING
// code with lots of debugging info
#else
// clean code only
#endif // TESTING`

Is this a good method, or is there any other simple and elegant method ?

But this way, I am repeating the same code in two places and if anything is to be changed later on in the code, I have to do it in both places, which is time consuming and error prone.

Thanks.

I am using MS Visual Studio.

like image 980
john Avatar asked Mar 24 '10 15:03

john


People also ask

How do I change debug to release?

On the toolbar, choose either Debug or Release from the Solution Configurations list. From the Build menu, select Configuration Manager, then select Debug or Release.

How do I turn off just my code debugging?

To enable or disable Just My Code in Visual Studio, under Tools > Options (or Debug > Options) > Debugging > General, select or deselect Enable Just My Code.

How do I get out of debug mode in Visual Studio code?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

Is release build faster than debug?

Lots of your code could be completely removed or rewritten in Release mode. The resulting executable will most likely not match up with your written code. Because of this release mode will run faster than debug mode due to the optimizations.


1 Answers

You could use a macro to print debug information and then in the release build, define that macro as empty.

e.g.,

#ifdef _DEBUG
#define DEBUG_PRINT(x) printf(x);
#else
#define DEBUG_PRINT(x)
#endif

Using this method, you can also add more information like

__LINE__ 
__FILE__

to the debug information automatically.

like image 118
Tuomas Pelkonen Avatar answered Oct 15 '22 10:10

Tuomas Pelkonen