marcwolf:
Can I put extra files in a sub directory under the My_Project directory. How can I reference them.
You can, but recursive compilation is restricted to the src subfolder of your sketch folder. So you can create a folder structure like this:
My_Project
|_My_Project.ino
|_src
|_X
|_X.h
|_X.cpp
Then in the sketch your #include directive would look like this:
#include "src/X/X.h"
You will find that some Arduino libraries use the incorrect #include directives for their internal files. Let's say in X.cpp was this line:
#include <X.h>
Although not best practices, that will work fine when the library is installed normally. However, when you bundle a library that uses this syntax with a sketch you get a "no such file or directory" error, because the library is not installed in the include search path. The solution is to use the correct #include syntax:
#include "X.h"
Similarly, if you bundle multiple libraries that have dependencies on each other with the sketch, you'll need to modify those #include directives to use the relative path. For example, if the X library had an #include directive for Y.h, which was in bundled library Y, you would change the #include directive in the X library from:
#include <Y.h>
to:
#include "../Y/Y.h"
The advantages of putting the bundled libraries in the src subfolder of the sketch folder rather than in the root of the sketch folder:
- The Arduino IDE displays all source files in the root of the sketch folder as tabs. If you don't intend to modify the library code while working with the sketch, these tabs may just act as clutter or confuse the user.
- You might prefer to keep each bundled library in a separate folder, rather than jumbling their files all in together in the same folder.
On the other hand, if you want to be able to easily edit or browse the library source code via the Arduino IDE, you might to have the library source files shown as tabs in the Arduino IDE.
marcwolf:
What is the precedent for the compiler to search for files?
It's a bit complex because the Arduino IDE tries to be smart about picking the best library when multiple libraries have a file matching the #include directive. For the purpose of this discussion, you probably only need to know that when you use the #include <X.h> syntax only the include path (the libraries folders and a few other locations in the hardware core and toolchain) is searched. When you use the #include "X.h" syntax, the local folder is searched, followed by the include path.
If you are interested to know more details about the search precedent, I can provide some additional information on that topic.