How does #include know which directory to use?

I see all the libraries are in various directories in the libraries/ directories.

libraries/Servo
libraries/Sprite
libraries/Stepper

etc.

If I #include <foo.h> I do not have to specify a path. Does the arduino software just scan all the directories in their looking for a match for foo.h? What if I have foo.h in two different directories? Say I had two different versions of an ethernet library?

Technically, this is up to the compiler, but most use the INCLUDE environment variable in effect when the compiler runs, and then the compiler can also have additional directories specified on the command line. The Arduino shell executes the compiler with these things all set up, having already scanned your source code for the libraries and header files it depends on.

If you have two drop-in equivalent header files like you describe, you should probably move one of them outside your usual Arduino library folder, or rename it, so that there is no ambiguity.

Halley is right but its a little confusing how they put it. This explains how it looks for include files.
Say you used #include <foo.h>
then the arduino ide would check in the “~/arduino-0018/libraries/” folder
and then look for a folder “foo”
“~/arduino-0018/libraries/foo”

then it would get “foo.h”
“~/arduino-0018/libraries/foo/foo.h”

so the name of the library, is the same as the library folder name, in the arduino “libraries” folder

so if foo.h was in two different directories it would only use the one in the folder named “foo”

remember there is a difference between
#include <foo.h>
// this one looks in the arduino libraries folder
“~/arduino-0018/libraries/foo/foo.h”

I read somewhere that <> can check in both directories while “” only checks in one.
Which would be good if you want to use a custom one for what you’re doing.

and

#include “foo.h”
//this one looks in the sketch folder for the include
“sketchbook/RFID_Potato/foo.h”
its the same directory as the “RFID_Potato.pde”

hope that clears it up more for you and anyone else. :smiley: