Precompiler acting out!? #define debug

Hio,

Straight to the point. I’m using define to create debug functionality and it works
in some places and others it does not. I can’t understand why that is.

My sketch is spread across 4 tabs.

Attached CPP is preproc compilation

EDIT, USED LIBRARIES:

#define DEBUGX true

#if DEBUGX
#define DEBUG_PRINTLN(x) Serial.println x
#define DEBUG_PRINT(x) Serial.print x
#else
#define DEBUG_PRINTLN(x) do {} while (0)
#define DEBUG_PRINT(x) do {} while (0)
#endif

example usage:

DEBUG_PRINTLN(F("Setup complete"));
DEBUG_PRINT(F("TTTTTTTTTTTT"));

As seen in snippet below from the .cpp file attached

Serial.println (reinterpret_cast<const __FlashStringHelper *>(
# 96 "C:\\Users\\Adis\\Dropbox\\Arduino\\Global\\Projects\\MeatDryer2\\MeatDryer2.ino" 3
 (__extension__({static const char __c[] __attribute__((__progmem__)) = (
# 96 "C:\\Users\\Adis\\Dropbox\\Arduino\\Global\\Projects\\MeatDryer2\\MeatDryer2.ino"
 "Setup complete"
# 96 "C:\\Users\\Adis\\Dropbox\\Arduino\\Global\\Projects\\MeatDryer2\\MeatDryer2.ino" 3
 ); &__c[0];}))
# 96 "C:\\Users\\Adis\\Dropbox\\Arduino\\Global\\Projects\\MeatDryer2\\MeatDryer2.ino"
 ));

While other places simply show up as this:

{};
{};
{};

WHAT GIIIIIIIVES? .___.

ctags_target_for_gcc_minus_e.cpp (10.2 KB)

MeatDryer2.zip (3.29 KB)

Which version of the Arduino IDE are you using?

Hi, I'm currently on 1.8.8

It is my understanding that the IDE combines all INO files in the order they are presented from the operating system. That is usually going to be sorted roughly alphabetically. If I'm correct then 1_Encoder.ino is first.

Does that bring clarity?

You're right about the combining of .ino files in alphabetical, but for one exception. The .ino file that matches the sketch folder name is first. So the order they are combined in is the same as the order they are displayed in the Arduino IDE:

  • MeatDryer2.ino
  • 1_Encoder.ino
  • 2_DHT.ino
  • 3_Main.ino

Can anyone try compile the project and look at your preproc compilation to see if you get the same errors?

Sketch is used on a arduino nano

Please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded the libraries from. Or if you installed them using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

pert:
Please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded the libraries from. Or if you installed them using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

Ah yes my bad sorry!
Not used to posting for help, will update my post as soon as I’m on a pc

First post edited with relevant libraries!

#define DEBUG_PRINTLN(x) Serial.println x

So, if I type DEBUG_PRINTLN (42); won't this expand to Serial.println 42;?

If you set File > Preferences > Compiler Warnings to "Default" or higher and then examine the contents of the black console window at the bottom of the Arduino IDE window after compilation, you get an extremely useful bit of information:

In file included from E:\arduino\MeatDryer2\2_DHT.ino:1:0:

E:\arduino\libraries\DHT_sensor_library/DHT.h:28:0: warning: "DEBUG_PRINTLN" redefined

   #define DEBUG_PRINTLN(...) {}



E:\arduino\MeatDryer2\MeatDryer2.ino:6:0: note: this is the location of the previous definition

 #define DEBUG_PRINTLN(x) Serial.println x

It's problematic for library authors to define macros in header files using common names. If it was necessary to define it there (rather than in the .cpp file), best practices would have been to use a name less likely of resulting in conflict, like DHT_SENSOR_LIBRARY_DEBUG_PRINTLN. Since they took DEBUG_PRINTLN, you'll have to use a different name for the macro in your sketch.

pert: If you set File > Preferences > Compiler Warnings to "Default" or higher and then examine the contents of the black console window at the bottom of the Arduino IDE window after compilation, you get an extremely useful bit of information:

In file included from E:\arduino\MeatDryer2\2_DHT.ino:1:0:

E:\arduino\libraries\DHT_sensor_library/DHT.h:28:0: warning: "DEBUG_PRINTLN" redefined

  #define DEBUG_PRINTLN(...) {}

E:\arduino\MeatDryer2\MeatDryer2.ino:6:0: note: this is the location of the previous definition

#define DEBUG_PRINTLN(x) Serial.println x



It's problematic for library authors to define macros in header files using common names. If it was necessary to define it there (rather than in the .cpp file), best practices would have been to use a name less likely of resulting in conflict, like DHT_SENSOR_LIBRARY_DEBUG_PRINTLN. Since they took DEBUG_PRINTLN, you'll have to use a different name for the macro in your sketch.

Thank you...! Nailed it. The thought never struck me that I could be redefining it in another library.. Changing the name of the functions made it work as expected. Also thank you for telling me about the compiler settings, funny enough it was set to "nothing".. one of those boxes my eyes have passed a couple of times and never noticed.

It is a bit unfortunate that the default setting of that preference is "None". Compiler warnings can be super helpful. I always have mine set to "All".

There is the phenomenon where beginners tend to not understand the difference between a warning and an error and get distracted by some warning from a library that someone with more experience would know can be safely ignored. Even so, that's a lesson they'll need to learn sooner or later and beginners need helpful hints from the compiler about possible issues in their own code more than anyone.

Developers who don't have warnings turned on are going to be much more likely to write code that generates warnings. It's annoying to me to use a library that generates a lot of spurious warnings because I always pay attention to warnings and so I must wade through the warnings from other people's code in order to check whether there are any for my own code.

So I definitely think the world would be a better place if everyone had warnings on.

In the ESP32 hardware package, they took the interesting measure of setting -Werror=all when you have warnings set to "More" or "All". That way you have no option but to write warning-free code.