Arduino Libraries outside Libraries Folder

Hi,

I'm on a mac, and for organizational reasons I moved a few libraries into a subfolder within libraries. So my relevant libraries are in Libraries > folder1 >libfolder. I thought it would be as simple as, in my sketch, amending:

#include "MyLib.h"

to

#include "folder1/libfolder/MyLib.h"

The IDE doesn't like this though. I've tried a lot of permutations but no good. Is there a way to access libraries that are within a subfolder in the Libraries folder?

Thank you!

You could get around this by adding a blank file named folder1.h to the folder1 folder and then adding the line:

#include <folder1.h>

above the other includes for that folder, which will cause folder1 to be added to the search path for all includes after that line. Then this should work:

#include <folder1.h>
#include <libfolder/MyLib.h>

Note that you should not add folder1 to the include path.

Unfortunately quite a few Arduino libraries use the incorrect include syntax for including other files of the library and this may cause some libraries to be incompatible with your folder structure until you fix the syntax.

Another option is to specify the absolute path in the include. That's not an ideal solution because it's not very portable.

If this organizational problem is from multiple projects, you can create another sketch folder. You can have a new library folder to work with. Change the sketch folder in the preferences.

pert:
Another option is to specify the absolute path in the include. That's not an ideal solution because it's not very portable.

Fortunately I'm not too concerned about portability for this specific project. Embarrassingly enough, I can't figure out how to specify absolute project path on my mac. There's no C:/, so to speak. Figured out the absolute path and I think this workaround will work for now.

Thank you! I see what you mean about the problem of portability though. I moved these select libraries into a subfolder so that I could make that subfolder a repository of project-specific libraries on GitHub. I didn't want my entire Arduino Library folder to be the repository. Clearly, I'm new to Git. What might make more sense it to make my entire Libraries folder the Git repository, but then only include the relevant library folders?

Sorry guys I thought all was well because with the header included it compiled. But when I try to use my remote library (TimerOne in this case) I still get an error.

Arduino: 1.6.7 (Mac OS X), Board: “Arduino/Genuino Uno”

sketch/test_zcam.ino.cpp.o: In function setup': /Users/ryo/Documents/Arduino/test_zcam/test_zcam.ino:29: undefined reference to Timer1’
/Users/ryo/Documents/Arduino/test_zcam/test_zcam.ino:29: undefined reference to Timer1' /Users/ryo/Documents/Arduino/test_zcam/test_zcam.ino:29: undefined reference to TimerOne::initialize(long)’
collect2: error: ld returned 1 exit status
exit status 1
Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

at the moment I haven’t included a few functions which I’m 99.5% sure are not the problem. Here is my relevant include, setup, and loop. I’ve used this same TimerOne library extensively before, I know I’m calling the functions correctly (FYI, TimerOne is pre-instantiated within the library). So it’s still having a problem with something, even though it can find the header file.

#include "/Users/user1/Documents/Arduino/libraries/rsf/retool/TimerOne-r11/TimerOne.h"

#define DIR 8
#define PULSE 9
#define LS 13
#define DIRTOG 7
#define MOVEONE 6
int i = 0;

int state_ls=0;
int state_dirtog=0;
int state_moveone=0;
bool calibrated = false;
int buttonState;
int laststate_dir = 1;
int laststate_move = 1;
unsigned long lastDebounceTime = 0;
unsigned long debouncetime = 20;
boolean press_ls = false;
boolean press_dir = false;
boolean press_move = false;
bool motorward_z = true; //high

int location =0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Timer1.initialize(100);
//  Timer1.attachInterrupt(CheckState);
//  Timer1.resume();
  
  pinMode(DIR, OUTPUT);
  pinMode(PULSE, OUTPUT);
  pinMode(LS, INPUT_PULLUP);
  pinMode(DIRTOG, INPUT_PULLUP);
  pinMode(MOVEONE, INPUT_PULLUP);

  digitalWrite(DIR, motorward_z);
  digitalWrite(PULSE, LOW);
}

void loop() {
  while (calibrated != true){
    analogWrite(PULSE,10);
    CheckLS();
  }
  CheckDIRTOG();
  CheckMOVEONE();
}

You could try adding:

extern TimerOne Timer1;

to your test_zcam.ino file under the include directive for the library.

your path to the library may be causing an issue with the linker.

Otherwise put a copy of the lib in your test_zcam.ino directory, sort out the path in the include directive accordingly and try to compile.

I put my entire Arduino folder on github as a giant playground.. Very handy for working from different locations. Go to work in the AM sync up and go. Get ideas at night, sync up make changes and re-sync.

Both Macs too.

-jim lee

Sorry, more of a Git question, but if I gave up on the subfolder and set the repository to my Libraries folder, can I select the 8 or so project-relevant libraries to be the only ones synced to github? My coworkers don't need to parse through my many random arduino libraries of the last 3 years.

pert:
You could get around this by adding a blank file named folder1.h to the folder1 folder and then adding the line:

#include <folder1.h>

above the other includes for that folder, which will cause folder1 to be added to the search path for all includes after that line. Then this should work:

#include <folder1.h>

#include <libfolder/MyLib.h>



Note that you should **not** add folder1 to the include path.

Unfortunately quite a few Arduino libraries use the incorrect include syntax for including other files of the library and this may cause some libraries to be incompatible with your folder structure until you fix the syntax.

Pert, I thought this might be a good workaround but I haven’t had luck. I created an empty folder1.h within folder1. Then I added it above the others in a test library, and tried #include <libfolder/MyLib.h>. But I get the error:

Arduino: 1.6.7 (Mac OS X), Board: “Arduino/Genuino Uno”

/Applications/Arduino.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware “/Applications/Arduino.app/Contents/Java/hardware” -hardware “/Users/user1/Library/Arduino15/packages” -tools “/Applications/Arduino.app/Contents/Java/tools-builder” -tools “/Applications/Arduino.app/Contents/Java/hardware/tools/avr” -tools “/Users/user1/Library/Arduino15/packages” -built-in-libraries “/Applications/Arduino.app/Contents/Java/libraries” -libraries “/Users/user1/Documents/Arduino/libraries” -fqbn=arduino:avr:uno -vid-pid=0X2A03_0X0042 -ide-version=10607 -build-path “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp” -warnings=none -prefs=build.warn_data_percentage=75 -verbose “/Users/user1/Documents/Arduino/lib_test/lib_test.ino”
/Applications/Arduino.app/Contents/Java/arduino-builder -compile -logger=machine -hardware “/Applications/Arduino.app/Contents/Java/hardware” -hardware “/Users/user1/Library/Arduino15/packages” -tools “/Applications/Arduino.app/Contents/Java/tools-builder” -tools “/Applications/Arduino.app/Contents/Java/hardware/tools/avr” -tools “/Users/user1/Library/Arduino15/packages” -built-in-libraries “/Applications/Arduino.app/Contents/Java/libraries” -libraries “/Users/user1/Documents/Arduino/libraries” -fqbn=arduino:avr:uno -vid-pid=0X2A03_0X0042 -ide-version=10607 -build-path “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp” -warnings=none -prefs=build.warn_data_percentage=75 -verbose “/Users/user1/Documents/Arduino/lib_test/lib_test.ino”
WARNING: Spurious .github folder in ‘Adafruit MAX31855 library’ library
“/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino” “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard” “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp/sketch/lib_test.ino.cpp” -o “/dev/null”
“/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino” “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard” “-I/Users/user1/Documents/Arduino/libraries/folder1” “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp/sketch/lib_test.ino.cpp” -o “/dev/null”
“/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino” “-I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard” “-I/Users/user1/Documents/Arduino/libraries/folder1” “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp/sketch/lib_test.ino.cpp” -o “/var/folders/12/fgz7vytx34gcvg9f2xkk74300000gn/T/build00eddc34516010264014c5f3f98e4bd8.tmp/preproc/ctags_target_for_gcc_minus_e.cpp”

/Users/user1/Documents/Arduino/lib_test/lib_test.ino:3:25: fatal error: libfolder/MyLib.h: No such file or directory
#include <libfolder/MyLib.h>
^
compilation terminated.
Using library folder1 in folder: /Users/user1/Documents/Arduino/libraries/folder1 (legacy)
exit status 1
Error compiling.

It doesn’t have any problem with the blank header file.

projectzero:
Sorry guys I thought all was well because with the header included it compiled. But when I try to use my remote library (TimerOne in this case) I still get an error.

Hmm. I haven’t used the absolute path in includes before but I have seen posts from a couple of forum regulars about doing it. It doesn’t appear to work, even in old versions of the IDE (1.6.5 and 1.0.6). Maybe someone else can explain how they successfully do that.

projectzero:
Sorry, more of a Git question, but if I gave up on the subfolder and set the repository to my Libraries folder, can I select the 8 or so project-relevant libraries to be the only ones synced to github? My coworkers don’t need to parse through my many random arduino libraries of the last 3 years.

You can add any folders you want to exclude to the .git/info/exclude file. You can also probably do this with the .gitignore file but that file will be included in the repository, which is pointless in this case, so I think the exclude file would be more appropriate. Much better, in my opinion, would be to just create a separate repository for each library.

projectzero:
Pert, I thought this might be a good workaround but I haven’t had luck. I created an empty folder1.h within folder1. Then I added it above the others in a test library, and tried #include <libfolder/MyLib.h>. But I get the error:

I tried it with Arduino IDE 1.6.7 and it works for me. Is your folder structure like this:

libraries
|_folder1
|_folder1.h
|_libfolder
|_MyLib.h