Understanding include_paths and relative paths?

I externalized certain functions into a .h and a .cpp file.
Now i want ot include this from two different projects.
However, i don’t know how to include this, as the compiler won’t allow absolute paths?
#include </User/Julian/Code/sparkles/sparkles-main-config/src/messaging.h>
gives me

src/main.cpp:10:10: fatal error: /User/Julian/Code/sparkles/sparkles-main-config/src/messaging.h: No such file or directory

for explanation: ~/Code/sparkles is my project folder.
sparkles-main-config and sparkles-client-config are the two sub-projects (one goes on one device, the other on a few others)

I optimally would like to just put the files in a separate libraries-folder and include them with #include <…/sparkles-libraries/messaging.h>, but platformio complains that if i want to add the libraries as third platformio project to my workspace, it needs a platformio.ini which then will cause subsequent problems.

I guess the “easiest” way would be to include the libs as external library, but then i still can’t seem to include it to my workspace…

Somewhere along this, my thinking is wrong. Where?

Have a look at lib_extra_dirs. I used it. You add it to the projects in file and you can load “libraries” from there.

The structure is the same as the projects lib directory:

+--- lib_name
    +--- lib.cpp
    +--- lib.h

In the ini file the path can be relative or absolute.

This should work for what you described. I use it as a “global” library directory where I keep all my own libraries etc that I don’t want duplicated all over my various projects.

Now, just include the header file name, in quotes, without a path.



thanks. my problem with a global library directory is that my libs make use of stuff like arduino.h and therefore won’t compile unless i make the global lib director its own platformio project and have a main.cpp with a void loop() and everything…
do you have a solution for this?

If you are using the Arduino framework then if you have a main() function in your code, somewhere, then the need for loop() and setup() goes away. This also works in the Arduino IDE too.

Here’s a sketch I just built.

My ini file:

platform = atmelavr
board = uno
framework = arduino

And src/main.cpp:

#include "Arduino.h"

int main() {

    // Setup

    // Loop
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(LED_BUILTIN, LOW);

Of course, if you are not using the Arduino framework, you’ll not find the Arduino.h file, so adding an extra search directory should help:

-I/home/norman/.platformio/packages/framework-arduino-avr/cores/arduino -I/home/norman/.platformio/packages/framework-arduino-avr/variants/standard

Unfortunately, while this will compile, the linker will complain and unless you have a prebuilt libFrameworkArduino.a and/or libFrameworkArduinoVariant.a—which you can get from any build with the Arduino framework—lying around to add into the linker, then it will be a bit more complicated to get the linker to work. IN that case, this addition should work, for an Uno board:

build_flags = -I/home/norman/.platformio/packages/framework-arduino-avr/cores/arduino -I/home/norman/.platformio/packages/framework-arduino-avr/variants/standard -Lsrc -lFrameworkArduino -lFrameworkArduinoVariant



It is better to use relative paths.

I use the following directory structure:

|  |--LibA
|  |  |--src
|  |  |  |-LibA.h
|  |  |  |-LibA.cpp
|  |
|  |--LibB
|     |--src
|     |  |-LibB.h
|     |  |-LibB.cpp
|     |--.pio
|     |--.vscode
|     |--include
|     |--lib
|     |--src
|     |  |-main.cpp
|     |-platformio.ini
|     |...

To make it perfect, provide each library with a library.json (which is located at the same level of the src folder of the library).

Only one setting needs to be added in the platformio.ini in the projects:

lib_extra_dirs = ../libStorage

There is no problem with the inclusion of Arduino.h in the libraries.
The libraries are only compiled with the actual project.

1 Like