Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a debug only statement

In C# I can use the following code to have code which only executes during debug build, how can I do the same in Xcode?

#if DEBUG {     // etc etc } #endif 
like image 996
Anthony Main Avatar asked Jan 26 '09 11:01

Anthony Main


People also ask

How do I debug code?

Using a print statement might be the simplest way to debug code. All programming languages have one or more commands that you can use to print values out on the console when the software is running.

How to debug code in Excel VBA?

This example teaches you how to debug code in Excel VBA. By pressing F8, you can single step through your code. The is very useful because it allows you to see the effect of each code line on your worksheet.

What is a Visual Studio debugger?

To help us in our fight against bugs, debuggers were developed. They are nothing more than programs that are able to read other programs and go through them line by line, checking whatever information we want along the way (such as the value of variables, for example). The first example we're going to see is Visual Studio debugger.

Why do we need to debug code?

It's a mechanism that helps us save energy and do things quicker. But when debugging, we need to enforce our brain to work with us and be as present as possible on every line of code. As your codebase get's bigger, it will be hard to analyze every line of code in the search for your bug.


2 Answers

You can use

#ifdef DEBUG     .... #endif 

You'll need to add DEBUG=1 to the project's preprocessor symbol definitions in the Debug configuration's settings as that's not done for you automatically by Xcode.

I personally prefer doing DEBUG=1 over checking for NDEBUG=0, since the latter implies that the default build configuration is with debug information which you then have to explicitly turn off, whereas 'DEBUG=1' implies turning on debug only code.

like image 96
Alnitak Avatar answered Oct 08 '22 23:10

Alnitak


The NDEBUG symbol should be defined for you already in release mode builds

#ifndef NDEBUG /* Debug only code */     #endif  

By using NDEBUG you just avoid having to specify a -D DEBUG argument to the compiler yourself for the debug builds

like image 26
ShuggyCoUk Avatar answered Oct 08 '22 22:10

ShuggyCoUk