How to deal with .h project configuration file for libs?

Hey guys,
It’s been a while since I reach this forum… It’s because PIO works perfectly :wink:

However, I have something I need to do and I don’t know how to make it cleanly.

Let’s go to the main subject then :

At Luos we are making libraries, one of them is LuosHAL which is the Hardware Abstraction Layer of our main lib. This LuosHAL lib need a lot of configurations and to make it, we use a .h configuration file with a lot of default define in it: LuosHAL/luos_hal_config.h at master · Luos-io/LuosHAL · GitHub

When someone creates a project using our lib we want them to be able to define their own configs in their projects to overwrite our default ones.

But because Platformio compiles libs “independently”, the LuosHAL lib doesn’t have defined values from the user projet when it’s compiled.
We want to avoid to move all the configurations on the platformio.ini file using “-D*”. Is there any way to make it clean?

Take a look at how the TFT_eSPI handles this with their configuration header file, User_Setups/SetupXX_XXXX.h.

You see the way the above library does it that it instructs users who wish to define a custom configuration file to define a macro and add a -include flag so that a certain file is always included.

The library can then check if that macro is defined to avoid loading the standard configuration.

But there are also lots of other ways to do it.

You can also add the project’s include (or src/) diretory to the library’s buid flags (e.g., dynamically referenced by these environment variables and use

  • __has_include to check for the existance of a specifically named header file, say luos_config_user.h, and then include that instead of the default configuration file.
  • or check for the existance of a macro, say LUOS_CUSTOM_CONFIG_HEADER (must have been added by the user in the platformio.ini), which you can #include then, by the value of that macro (see here).

Thank’s a lot @maxgerhardt,

The -include flag seems to be the obvious solution in our case.
Also thanks a lot for this really good lib example, we will pick some of the good ideas and organization to simplify our lib usage!