Skip to content

Allow compilation in absence of RS485 library #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Arduino_POSIXStorage.h"
#include <iostream>

#if !defined(HAS_SERIAL) && defined(HAS_RS485)
#if !defined(HAS_SERIAL) && defined(HAS_RS485) && __has_include(<ArduinoRS485.h>)
Copy link
Contributor

@per1234 per1234 Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately __has_include(<ArduinoRS485.h>) will evaluate as false even when the library is installed. the reason is that __has_include checks whether the file is present in the compiler's "search path". Libraries are added to the search path by the "library discovery" phase of the Arduino sketch build system, which discovers libraries based on the #include directives in the code. So there is a "Catch-22" situation when attempting to use __has_include for conditional inclusion of headers from libraries:

arduino/arduino-cli#1650

You can check this with the following code:

#if __has_include(<ArduinoRS485.h>)
#pragma message "__has_include(<ArduinoRS485.h>) evaluated to true"
#else
#pragma message "__has_include(<ArduinoRS485.h>) evaluated to false"
#endif

When you compile, you will see the "__has_include(<ArduinoRS485.h>) evaluated to false" message in the output even when the ArduinoRS485 library is installed.

The above is true for Arduino CLI and Arduino IDE. I don't have any experience with Arduino PLC IDE, so I can't say for sure but I would assume it is also true there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A workaround would be to add instructions for the user that, if they want to use the RS-485 debugging capabilities, they must add an #include directive for ArduinoRS485.h to their sketch above the #include directive for the Arduino_UnifiedStorage library:

#include <ArduinoRS485.h>
#include <Arduino_UnifiedStorage.h>

This will cause the ArduinoRS485 library to be discovered and its path added to the search path before the evaluation of __has_include(<ArduinoRS485.h>), which will cause it to evaluate as true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@per1234 Well spotted! Your suggestion makes sense. Either we go with that, or we add ArduinoRS485 to the dependencies. I'd prefer not to because this dependency doesn't make sense for all boards. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to because this dependency doesn't make sense for all boards. Wdyt?

Since the library's "debugging output" feature is completely supplemental and likely to be used by advanced users who won't have difficulty installing the ArduinoRS485 library on demand, I think your perspective regarding avoiding the introduction of an additional dependency is reasonable.

#include <ArduinoRS485.h>


Expand Down
Loading