Skip to content

Allow code to make #include optional #1650

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
3 tasks done
KurtE opened this issue Feb 2, 2022 · 0 comments
Open
3 tasks done

Allow code to make #include optional #1650

KurtE opened this issue Feb 2, 2022 · 0 comments
Assignees
Labels
topic: build-process Related to the sketch build process topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@KurtE
Copy link

KurtE commented Feb 2, 2022

Describe the request

Code might contain optional functionality dependent on the presence of a library.

It would be nice to have a way to handle this programmatically, based on the presence or absence of the library dependency.

Describe the current behavior

Currently, the user would need to uncomment an #include directive for the library to enable such functionality:

// uncomment if you wish to use. 
//#include <Foo.h>

This is inconvenient and prone to user error.

We would expect to be able to use the __has_include preprocessor operator:

#if defined __has_include
#  if __has_include (<Foo.h>)
#    include <Foo.h>
#  endif
#endif

However, this only works if the header file is present in the compiler's "search path". A library will only be present in the search path if it has already been discovered, so __has_include (<Foo.h>) will evaluate as false even if a library containing Foo.h is installed, unless the library had already been discovered by the sketch build system. Library discovery is done based on #include directives, and the directive will cause compilation to fail if the library containing the header file is not installed.

Describe the solution you'd like
With other setups I would probably do it like:

#if defined __has_include
#  if __has_include (<JPEGDEC.h>)
#    include <JPEGDEC.h>
#  endif
#endif

This actually will work in libraries if someone else explicitly includes it. And can work with other build setups for building Arduino sketches using some other mechanisms such as Makefiles.

Also note: with libraries this does not work if the header file is contained within the sketch folder
That is, if your have a library, that you wish to pass in the user configuration, it won't work to have it try to optionally read some options header file in the sketch (which also would be nice)

Arduino CLI version

Original report

Not specified.

Last verified with

a2eebcd

Operating system

All

Operating system version

Any

Additional context

Related

Issue checklist

  • I searched for previous reports in the issue tracker
  • I verified the problem still occurs when using the nightly build
  • My report contains all necessary details
@KurtE KurtE added the type: enhancement Proposed improvement label Feb 2, 2022
@per1234 per1234 transferred this issue from arduino/arduino-ide Feb 3, 2022
@per1234 per1234 added the topic: code Related to content of the project itself label Feb 3, 2022
@cmaglie cmaglie added the topic: build-process Related to the sketch build process label Mar 17, 2023
@per1234 per1234 changed the title Wish there was a way to optionally include a library in a sketch if I have it installed Allow code to make #include optional Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: build-process Related to the sketch build process topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

No branches or pull requests

3 participants