8
votes

I'm currently writing an Arduino library and I want to include files in a subdirectory within the library folder. More specifically, I want the files to be accesible from the Arduino sketch.

This is my directory structure:

MyLib/MyLib.cpp
MyLib/MyLib.h
MyLib/Extra/SomeNiceFunctions.cpp
MyLib/Extra/SomeNiceFunctions.h

This is how I'm trying to include the file:

#include <MyLib.h>
#include <Extra/SomeNiceFunctions.h>

Obviously this is wrong because of the way the IDE is including the library folders. What else should I try? I could split the Extra folder to another "Library" (another folder) but that's not what I'm after.

EDIT: This is the error I'm getting undefined reference to 'font8x8'. This is defined in Extra/SomeNiceFunctions.h.

4

4 Answers

3
votes

Don't use

#include <MyLib.h>
#include <Extra/SomeNiceFunctions.h>

instead use

#include <arduinolib.h>
#include "MyLib.h"
#include "Extra/SomeNiceFunctions.h"

Using angle-brackets, the compiler looks in the standard-folders for include-files. You want your custom files in your working directory.

0
votes

Regarding including Arduion/Energia libraries that are in Arduino/Energia subfolders: Look where your main include file is (eg. arduino.h / energia.h) and go up in the directory structure with ..\ or downwards until you get to the desired h file you want to include. eg. I have: ..hardware\cores\cc3200\energia.h ..hardware\libraries\SPI\SPI.h

In order to use in a .cpp file the SPI.h functions, I have to add: #include <energia.h> followed by #include <..\..\libraries\SPI\SPI.h

0
votes

I had some an issue with this. I just wanted a simple solution and while writing a library in CPP is the correct way - I took a sketch - say robo.ino file and added it to a simple library include - so that there was just one file in the library under a folder called Robot - robo.h

├── sketchesfolder
├── libraries
│   ├── robot
│   │   ├── robot.h
├── robotsketch
│   ├── Move.ino

Now in the Move.ino - I just include the file

#include <robo.h>

The only thing you need to add for simple functions is a prototype declaration - or make sure your functions are ordered by first use.

So for example - put Setup() at the bottom and a function like:

 void halt(int wait); 

as a prototype definition at the head of the robo.h file

It allows you to share a sketch as common code without converting it to a CPP lib. But you should really do that of course.

-1
votes

Borrowing from this stack overflow question,

The include path includes the sketch's directory, the target directory (/hardware/core//) and the avr include directory (/hardware/tools/avr/avr/include/), as well as any library directories (in /hardware/libraries/) which contain a header file which is included by the main sketch file.

Try

// in myProject.ino
#include <arduinolib.h>
#include "MyLib.h"

// in Mylib.h
#include "./extra/SomeNiceFunctions.h"