How to add an include file to IDE build path

Hi! I'm an experienced C programmer but new to Arduino (including IDE 2.2.1).
In doing my projects I have developed a number of utility macros and functions that I commonly want to use, and am getting pretty tired of copy/paste :slight_smile:

The IDE documentation I've found is targeted for beginners it seems. I was unable to find answers to these questions:

  1. How can I add a .h include file or directory/folder to the path the IDE searches? I'd like to create/use a new directory/folder to avoid messing with the "standard" ones.

  2. Can anyone please point me to documentation for how to create a library module for the IDE and add it to the library search path?

For the time being I'm using Windows (yuck) but will eventually probably switch to my Linux environment.

Thanks for any tips!

-Bob

That's because the IDE is targeted for beginners. There are other options available (based on Eclipse or VS Code) that provide the ability you're looking for. I'm not sure about Arduino IDE 2.x.

The simplest solution (without creating a library) is to copy your .h file to the project folder. Then you include it with

#include "myfile.h"

Unlike many IDEs, the Arduino IDE does not give the programmer much access to the command line used to compile and link files, so it is tricky to add a new include path (for example.) (it IS possible, but...)

Instead, the Arduino IDE has a standard location for libraries, and a standard directory structure for informing the IDE about each library. See Library specification - Arduino CLI

You can add sources also in a subfolder called "src" inside the sketch's folder itself.

The IDE is still painful, but at least it works and you don't have to put everything on the same level as the .ino file

Do I have to add the new library via a ZIP file, or can it be created in-place within the library directory? I tried creating it in-place but the IDE isn't recognizing it. Do I have to force a re-index or something like that?

This is exactly what I did. But then
#include <mylib.h>
is not finding the file. That's why I was wondering if the IDE needs to reindex or something similar.

Header file is Arduino/libraries/Utility/src/bur.h

Right now there's just a #define macro in the file.

ino file is Arduino/Test/Test.ino
it contains the line
#include <bur.h>

When I build it, it says

fatal error: bur.h: No such file or directory"
 #include <bur.h>
          ^~~~~~~
compilation terminated.

exit status 1

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

I tried restarting the IDE but no change.
This is on Windows 11, IDE 2.2.1

It works if I move bur.h into the same directory as the ino file (and change the #include to use "bur.h" rather than <bur.h>).

Yeah, sorry typo in my post, but it's in the right place - the same area as the other libraries:
Arduino/libraries/Utility/src/bur.h

In spite of this, it's not finding the file.

PS C:\Users\bobsm\Documents\Arduino\libraries\Utility\src> ls


    Directory: C:\Users\bobsm\Documents\Arduino\libraries\Utility\src


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        11/29/2023  11:24 AM             85 bur.h

At the moment, I don't have anything else in the Utilities directory except for the src subdirectory containing bur.h
Do I have to create a library.properties and keywords.txt file to get it recognized, even if it's just a .h header file?

If you put the header file under the src subfolder of the library (known as the "recursive" library layout), the library.properties file is mandatory.

If you put the header file in the root of the library (known as the "flat" library layout), the library.properties file is optional.

You can learn more about this from the Arduino Library Specification:

https://arduino.github.io/arduino-cli/latest/library-specification/#layout-of-folders-and-files

The keywords.txt file is completely optional regardless of the layout of your library. It is only used to provide keyword highlighting in Arduino IDE 1.x (Arduino IDE 2.x does not use the keywords.txt file).

I just can't get the "flat" approach to work. I presume that by "root of the library" you mean the Arduino/libraries directory.

When I put the file there it isn't found, whether I include "bu.h" or <bu.h>, even if I restart the IDE to force reindexing. It only works if the file is in the sketch directory.

Finally!
That works great. Thanks so much. No more copying the header into every project I write :smile:

I appreciate the support.