PlatformIO Community

Code *.cpp under lib_extra_dirs not found without matching *.h

I don’t understand how lib_extra_dirs is supposed to work…

I have a project which I want to build for multiple different hardware targets.
Imagine I want to read some GPIO inputs, so I have a class in inputs.h which is common to all hardware targets, as the .h file only contains the ‘what’ this class can do.
So I put this inputs.h under /lib/inputs

Then to deal with different hardware targets, in platformio.ini, I set up multiple [env:xx] environments and under each environment I have a lib_extra_dirs pointing to a folder with a copy of inputs.cpp dedicated to the respective hardware. So each inputs.cpp contains an implementation, (the ‘how’) for 1 type of hardware
Example platformio.ini

[env:hardware1]
lib_extra_dirs = lib_target/hardware1

[env:hardware2]
lib_extra_dirs = lib_target/hardware2

Example of folder/files

/lib
  /inputs
    inputs.h
/lib_target
  /hardware1
    /inputs
      inputs.cpp
  /hardware2
    /inputs
      inputs.cpp

The problem I have is that the inputs.cpp file is not found during the build (linker error)
If I put the inputs.h file also under lib_target/hardware1/inputs, then the build succeeds.

So it looks like the LDF only includes the source code in lib_extra_dirs for compilation when there are .h header files, not if there are only .cpp files.

Questions

  • Why is this required ?
  • Is it not a valid practice to have one common inputs.h and several inputs.cpp depending on different hw implementations ?
  • What could be a workaround without duplicating inputs.h for each different hardware ?