Verbose debug printing in arduino? - debugging

I'd like to have some sort of verbose debug printing in arduino that can be enabled/disabled by a flag. For example I'd like the ability to do something like
#define VERBOSE
#define VERBOSE_PRINT(text) #ifdef VERBOSE Serial.println(text); #endif
Later in code:
VERBOSE_PRINT("Doing something");
if VERBOSE is defined then I should get stuff out over the serial port and if it's not defined then that code won't be compiled. Unfortunately this isn't working. I get the error: "error: '#' is not followed by a macro parameter". So I'm wondering what's the best way to get an optionally compiled print (or anything for that matter). Of course I could manually write out the #ifdefs, but I'd like it streamlined so that it doesn't take up a ton space and so I don't have to write it out each time I'd like to use it. Is a function w/ the #ifdef inside the function the best way to do this?

#define VERBOSE
#ifdef VERBOSE
#define VERBOSE_PRINT(str) Serial.println(str)
#else
#define VERBOSE_PRINT(str)
#endif
VERBOSE_PRINT("Doing something");

Related

Will compiler -D flag take priority over code #define of macro variable?

I have some testing code in Fortran which basically looks like
#define test1 0
#define test2 0
#define test3 0
...
#if test1
call test1()
#endif
#if test2
call test2()
#endif
#if test3
call test3()
#endif
...
At compilation, i want to change those values using -Dtest1=1.
I've seen some answers (like here) where they say you need to put
#ifndef test1
#define test1 0
#endif
But i have a lot of those and i would prefer not to add 40 lines of definition code.
Will compilation command -D flag take priority over my hard definition without the if clause?
EDIT : So i just tested it (which i should have done anyway before asking...) and compiler -D flag does not take priority over my own definition and just pulls out a warning . So any way for it to take priority and always use the compiler flags without passing through ifndef clauses?
-D effectively adds a #define at the beginning, before reading the input. So it would be just like adding an additional #define in your source code, which would naturally produce an error (or warning) about redefining the macro.
If you want to avoid that error, use the #ifndef idiom, just like everyone else does.

No compile error without multiple inclusion guard

Not including the #ifndef guard to prevent multiple header file inclusions, does not throw compile error. Why is that?
// main.c
#include <stdio.h>
#include "declare.h"
#include "declare.h" //Shouldn't I get compiler error here
int main(){
printf("Hello World\n");
}
// declare.h
#define a 1 //just this define in declare.h file
Command used to compile: gcc main.c -std=c89
Including a header multiple times is not an error, either with or without guards.
Guards prevent you from redefining objects and functions, when you do so.
But you haven't done that. You've just redefined a macro with the same value it had before. Which is fine.
You can test this by just compiling the following file:
#define a 1
#define a 1
It's fine.
[C89: 6.8.3]: [..] An identifier currently defined as a macro without use of lparen (an object-like macro) may be redefined by another #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical. [..]
Start putting more complex stuff in that header and you'll see a problem.

conditional compliation based on variable into makefile

Inside my C/C++ code I would like to include or not a file depending on different compilation.
For the moment I use this:
#ifndef __x86_64__
#include <myLib.h>
#endif
this gives me the possibility of doing whether the platform is 32/64 bit but does not give me enough freedom.
I would like to pass a variable to my makefile like
make includeMyLib=1
and depending on this having something like:
#ifndef includeMyLib
#include <myLib.h>
#endif
Do you know if anything like this is possible?
If you use GNU make, you could have something like this in the Makefile:
ifdef includeMyLib
CFLAGS += -DincludeMyLib
endif
This will change the flags used by the compiler to add the #define includeMyLib.

Eclipse CDT syntax error on __attribute__ keyword

I would like to know if there is a way to get rid of CDT syntax error warnings when using gcc's "__attribute__" keyword.
It goes without saying that I would not like to switch off CDT syntax check.
The "ECLIPSE_THINKS_THIS_IS_SET_BUT_GCC_DOESNT" definition (from ams's answer) really extsts and it called __CDT_PARSER__. For example:
#ifdef __CDT_PARSER__
#define __FILE__ "<file>"
#define __LINE__ (-1)
#define __DATE__ "<date>"
#define __TIME__ "<time>"
#endif // #ifdef __CDT_PARSER__
Hope this will be helpful.
I've not tried it, and I've not used Eclipse for some time, but here's an idea:
In the CDT settings for Eclipse (or maybe just your project) set up a predefined macro (I seem to remember you can tell it what the compiler auto-defines) named __attribute__ that takes one parameter, and expands to nothing.
Maybe I haven't explained that right. Let me try again with an example. Basically, the aim is to define a macro that works like this:
#if ECLIPSE_THINKS_THIS_IS_SET_BUT_GCC_DOESNT
#define __attribute__(X) /* nothing */
#endif
but without putting anything actually in your code.
Project->Properties->C/C++ general->Path and Symbols->Symbols
Add->
Name: __attribute__(X)
Value: (leave blank)
Related links: You can use this technique basically with any offending keyword
ziu's answer is also working for XC8 Microchip compilers
Name: __interrupt
Value: (leave blank)
The function prototype now is clean:
void __interrupt ISRs(void);
And Eclipse won't complain about it.

How to undefine a define at commandline using gcc

How do I at compile time undefine a compiler macro using gcc. I tried some compile args to gcc like -D but I can't get to see the "not defined" message.
Thanks
#include <iostream>
#define MYDEF
int main(){
#ifdef MYDEF
std::cout<<"defined\n";
#else
std::cout<<"not defined\n";
#endif
}
You can use the -U option with gcc, but it won't undefine a macro defined in your source code. As far as I know, there's no way to do that.
You should wrap the MYDEF definition in a preprocessor macro, the presence of which (defined on the command line) would then prevent MYDEF from being defined. A bit convoluted to be sure but you can then control the build in the way you want from the command line (or Makefile). Example:
#ifndef DONT_DEFINE_MYDEF
#define MYDEF
#endif
Then from the command line when you don't want MYDEF:
gcc -DDONT_DEFINE_MYDEF ...
http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Preprocessor-Options.html#Preprocessor-Options
The -U options seemed like what you could have needed... but then again you can't override a definition contained in your source code without resorting to more preprocessor directives.
You can resort to filtering source code and give this back to gcc for compilation, like this pseudo code:
grep -v "define MYDEF" yourFile.c | gcc -o yourFile.o -xc -
Hope it helps.
The code use case is not right. As I see, you have hard coded #define in the file. If compiler initially assumes MYDEF undefined, it will define it once it start processing the file.
You should remove the line #define MYDEF. And I hope your test case will work, if you pass MYDEF to -D and -U.
Here is one possibility that doesn't completely cover your use case but which I found to be helpful in my case.
If your MYDEF were #defined in a separate header file #included from the .c file you could force the definition of the #include guard macro with the -D option (thus preventing the MYDEF #definition) then either actively #define (still with the -D option) MYDEF to something else or just leave it undefined.
It is clear that anything else defined in the header file would also be missing but this was for me a solution to forcedly undefine a macro without changing the third-party code.

Resources