IDE compiler debug option

Hello,

I would love it if the IDE had different 'compile' buttons that would compile the sketches in different debug modes. For example, there could be 3 compile buttons: 'debug off' , 'debug 1' and 'debug 2'. These three buttons would compile the sketch and work with tags in the code to include or omit sections of code. This way, I could write in Serial.print or other behaviors that are only included if I compile with the associated debug button. This would make testing code way easier and reduce the amount of rewriting required in order to check things out and then produce a final product.

Even just having one debug compile button would be hugely useful for most people. But for more advanced troubleshooting, multiple options could be useful too.

Thoughts, ideas?

Cheers!

The arduino programming language is based on standard C. All the C compiler directives work.

You could do something like this:

// #define dbg

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  #ifdef dbg
    Serial.println("Debugging...");
  #else
    Serial.println("Not debugging...");
    delay(5000);
  #endif
}

Leave the #define line commented for no debug. Un-comment it for debug. You could have multiple #define statements.

#define someDebug
#define moreDebug
#define oodlesOfDebug

Then, use #ifdef followed by one of the values, to get different levels of debug. Comment or un-comment as desired.

I was thinking of doing that and just flip the variable to turn it on and off or set a level. But that means the compiled code will be bloated with unused 'if' sections. (I seem to recall reading in a post on this forum that 'if' statements are pretty intensive to process.) Please correct me if the way you have described is not like what I've said - I'm not an experienced programmer.

What I'd like is for the compiler to omit those sections so they aren't in the binary image at all. That way, I can stuff all kinds of things in to run tests, but then have a nice lean binary when I want to run it for real. This will really help in time-sensitive sketches. Also, it could allow one to test on a larger uC and then run the final version on a smaller one.

Maybe I'm just splitting hairs. But it feels like a cleaner way to go.

Maybe I'm just splitting hairs. But it feels like a cleaner way to go.

That of course sounds correct and it certainly makes reading your source easier with out a ton of debug if statements. However I seem to get the impression that the compiler optimzation routines will strip out code that can't be reached in a running program, if for instantance the debug define statement is not active? I could be over stating that feature but maybe more experianced GCC programmers could comment on that.

Lefty

The statemenst prefixed witn the hash sign such as "#ifdef" are collectively referred to as preprocessor directives. They are processed before the code gets compiled and so code blocks between false statements will never make it to your final program. In short this seems to be exactly what the original poster asks for. These features are not gcc specific and are typically available with any C/C++ compiler and most other lanugages in some shape or form as well.

It is true that some IDE's offer a debug compile, but typically this feature is implemented on top of "#ifdef"'s and the only difference is that the "#define" is passed on as an argument to the compiler. You will of course still have to code (and obscure your program) for the various trace/debug scenarios you need. Stiill I think this is a powerful and useful mechanism. The benefit of having separate buttons for release/debug compile is limited versus maintaining the same in a header file, but it ceratinly would not harm.

Some IDE's have additional features where the "intelligent" editor either collapses/folds/color code diferently as to whether it will be included or not. Such features make reading/working with the code easier.

They are processed before the code gets compiled and so code blocks between false statements will never make it to your final program.

This is exactly what I'm looking for. I'll review the example above and start using hash syntax for debugging.

Thanks everyone!