1

While this might appear to be a duplicate of this question Loading local libraries it isn't, and for one very simple reason. That question deals with libraries that are expected to be used by multiple sketches, while the problem I have is to do with the architecture of a single sketch.

I have a sketch where it makes a lot of sense to divide the source into multiple subdirectories. To give some idea of the scale I'm talking about it's ~20,000 LOC spread over ~100 .cpp/.h files, along with the obligatory .ino. So I have the following layout:

sketch/
    sketch.ino
    src/
        module/
            module.h
            module.cpp

which while much smaller than the actual project is enough to demonstrate the problem.

Forget about "flattening" it all out into the sketch folder, no sane minded software engineer should ever consider doing that, not with a project the size I've outlined.

If I place the line

#include "module.h"

in sketch.ino I get the following error from the Arduino IDE:

C:\path\to\sketch\sketch.ino:3:10: fatal error: module.h: No such file or directory
    3 | #include "module.h"
      |          ^~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: module.h: No such file or directory

I get the same error when using Arduino-Cli.exe to compile the sketch.

EVERY other C/C++ compiler on planet Earth offers an option akin to the -I of GCC and MSVC to allow me to specify other folders to include in the header search.

How is this done with Arduino, so that I can get module to compile correctly? As noted in the linked question, I could use a full path, but that's (A) ugly, and (B) unnecessary.

Header search paths are a thing, how do I do them with the Arduino toolchain?

dgnuff
  • 111
  • 1
  • It's not in the same folder is it? How about trying: #include "src/module/module.h". However I'm not convinced that will work. The IDE copies your files into a working directory. – Nick Gammon Oct 26 '23 at 09:34
  • 1
    my guess would be #include "module/module.h". src is searched. src/module isn't. https://arduino.github.io/arduino-cli/0.35/sketch-specification/#src-subfolder – Juraj Oct 26 '23 at 09:35
  • Looking at the arduino-cli doc, can't the flag --libraries do the job? – NickB Oct 26 '23 at 13:27
  • @nickb That's a bit of a hack, and might be able to solve the problem for arduino-cli, but it does nothing for the ide itself. – dgnuff Oct 30 '23 at 23:40
  • @dgnuff Then Platformio might better fits your needs. IIRC, you can setup multiple include and libraries per project. – NickB Nov 01 '23 at 12:59
  • 1
    @NickB That's a good suggestion. I finally bit the bullet, and did the transition over to PlatformIO. There were a few teething troubles as I got everything set up, but the end result is a development environment that's vastly superior to the Arduino IDE. I'm able to use Visual Studio as my IDE, and just use an NMAKE project configuration with a call to pio run to build the project. Since GCC does the heavy lifting, adding a -I option is trivial ... – dgnuff Dec 04 '23 at 14:48
  • ... and the icing on the cake is that I can have metadata in platformio.ini that allws me to build for multiple target platforms. Another project I have can be built for either ESP32-S2 or Pi Pico W. PlatformIO handles this flawlessly, while Arduino trips over it's own feet trying to do such a simple thing. – dgnuff Dec 04 '23 at 14:49

0 Answers0