Modify a library for a specific sketch only


Most of my sketches use the libraries just like they were developed by the Community. However, for some I need slight modifications. Is there any way to keep such modified versions of the libraries specifically for a given sketch?

I imagined this could be achieved by creating the "libraries" folder in the sketch's folder, but libraries are not detected by the IDE/compiler there.


When you put library files in the sketch folder they are all shown as tabs in the Arduino IDE. This may be desirable since it allows you to view and modify the library source. However, it also clutters up the Arduino IDE if you only want to work on the sketch and if you do this with multiple libraries the sketch folder will get very messy. Also, that won’t work with libraries that have source files in separate subfolders.

Another option is to put the libraries under the src subfolder of the sketch folder. For example, say you have a sketch named foosketch and a library named foolib, the folder structure would look like this:


Include in the sketch like this:

#include "src/foolib/foolib.h"

Note this usage is only supported in recent versions of the Arduino IDE.

When you put libraries in the sketch folder, whether in the root or src subfolder, you will encounter a compilation error if any of the library files use the #include <foo.h> syntax to include internal library files. Although incorrect, this works when the library is located in a standard library folder so it’s a common problem. The solution is to simply modify the library source to use the #include "foo.h" syntax.

You are asking for trouble having different versions of the same library still under the same name, just in different folder locations.

Better to derive a new libary with a new name from the original library, make the desired mods to it and reference it under the new name in the #inclue statement the main .ino file.

I added functionality to a published Sparkfun ESP8266 library in this way, then use it instead of the original. Works great!