How to include from subfolder of sketch folder

Arduino IDE knows how to include .h and .cpp files from subfolders of library folders, e.g. <Fonts/*>.

But I have not been able to include such files from a subdirectory of the sketch folder.

Maybe I need to add to the include path, but don't know where.

1 Like

Including files from the libraries folder, and subfolders, uses <> around the path/file name.

Including files from the sketch folder, and subfolders, uses "" around the path/file name.

1 Like

(deleted)

For some reason unknown to me, this does not work in my environment with Arduino 1.6.10

/***************************************************
  This is our GFX example for the Adafruit ILI9341 Breakout and Shield
  ----> http://www.adafruit.com/products/1651

  Check out the links above for our tutorials and wiring diagrams
  These displays use SPI to communicate, 4 or 5 pins are required to
  interface (RST is optional)
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

// this works, but needs copying to .../Arduino/libraries
//#include <GTFT_IO.h>
//#include <Adafruit_GTFT_ILI9341.h>

// this does not compile with my Arduino 1.6.10
//#include "GTFT_IO/GTFT_IO.h"
//#include "Adafruit_GTFT_ILI9341/Adafruit_GTFT_ILI9341.h"

// this does also not compile with my Arduino 1.6.10
#include "GTFT_IO\GTFT_IO.h"
#include "Adafruit_GTFT_ILI9341\Adafruit_GTFT_ILI9341.h"

GTFT_IO_SPI9 GTFT_IO(D4);

// For the Adafruit shield, these are the default.
#define TFT_DC D4
#define TFT_CS D8

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_GTFT_ILI9341 tft(TFT_CS, TFT_DC, -1, &GTFT_IO);

GTFT_graphicstest.zip (28.3 KB)

Since you clearly didn't write the libraries that you are using, why are they not in the libraries folder?

Just toss the Adafruit stuff in your library folder. Works fine in there.

..least it does for me. I have the same stuff in mine.

-jim lee

spycatcher2k:
Example:

#include "mysubdir\mylibrary,h"

That is a backwards slash.
While it may work on Windows, it is incorrect.

--- bill

1 Like

In recent versions of the Arduino IDE (including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder.

For example:

Blink
|_Blink.ino
|_src
   |_BlinkLib
      |_BlinkLib.h
#include "src/BlinkLib/BlinkLib.h"
1 Like

pert:
In recent versions of the Arduino IDE(including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder. For example:

Blink
|_Blink.ino
|_src
|_BlinkLib
|_BlinkLib.h

#include "src/BlinkLib/BlinkLib.h"

There was lots of discussion about this in a github issue related to this.
See this github issue for the gory details: Subfolders can no longer be included · Issue #5186 · arduino/Arduino · GitHub
The IDE copies the user sketch directory and files over to a working directory where it does the build and recently the IDE was modified to not copy any subdirectories except src so any headers in directories other than under src would not work. However after much discussion the Arduino team decided to change their mind and fixed it so that other sub directories would work again for include files which is the way it used to work.
I've not tested it but think the latest IDE (1.8.0) is now working the way it used to work and sub directories for header files works again, so depending on which version of the IDE it may or may not work.

--- bill

1 Like

Now everything has become clear. Thank you all for the clarification!

Happy New Year

Jean-Marc Zingg

PaulS:
Since you clearly didn't write the libraries that you are using, why are they not in the libraries folder?

Because I try to adapt and enhance libraries for my use and for different hardware, and the Arduino IDE does not allow me to edit sources in subdirectories of .../Arduino/libraries.

ZinggJM

I went through the same thing when I wanted to write my own libraries and the tweak them later. Just use any other IDE or source editor to tweak things in the libraries. As long as you don't forget to save all before telling the IDE to compile & load, it works great.

-jim lee

... And because my libraries folder got cluttered and slows down Arduino IDE startup.
I started to move libraries that I rarely use into .../librariesAside.

Include now works with Arduino 1.8. But I got linker errors. It seems .cpp files don't get added.
But this needs further investigation; for now I have 1.8 on an alternate Notebook.

ZinggJM

It seems .cpp files don't get added.

They do if the header file is included in your sketch. Sounds to me like you are trying to hide the use of a library. THAT is not allowed.

PaulS:
They do if the header file is included in your sketch. Sounds to me like you are trying to hide the use of a library. THAT is not allowed.

You are welcome to check that yourself, as I have attached the files in question.
I know that I should add the copyright notice of the original to two files derived from this original, this will be done, but in the hurry to seek for a solution to that issue, I forgot this before zipping for this topic.

I will now try with all my modified files in one src directory on Arduino 1.6.10.

I will be careful to use "" for all these 4 files.

But before I eventually attaching this again, I need to put in the copyright notice of the SPI library of the ESP8266 package.

I tried 3 versions, v2 and v3 work on Arduino 1.6.10, v4 should work on Arduino 1.8.0 but does not.

GTFT_graphicstest_v2.zip (16 KB)

GTFT_graphicstest_v3.zip (16.7 KB)

GTFT_graphicstest_v4.zip (16.5 KB)

pert:
In recent versions of the Arduino IDE(including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder. For example:

Blink
|_Blink.ino
|_src
|_BlinkLib
|_BlinkLib.h

#include "src/BlinkLib/BlinkLib.h"

Thank you very much.. I am using MS VS Code.. so this was really important - and reason, why my program was not able compiled under Arduino IDE..
Thanks a lot

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

pert:
In recent versions of the Arduino IDE(including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder. For example:

Blink
|_Blink.ino
|_src
|_BlinkLib
|_BlinkLib.h

#include "src/BlinkLib/BlinkLib.h"

I'm using Arduino IDE 1.8.7 and this is still necessary so everything is copied properly. Thanks for your help. Wish more people were helpful as you and not such a ferk as others do.