Skip to content

Allow code to make #include optional #1650

Open
@KurtE

Description

@KurtE

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions