#include needs to be in main file?

I have a project with multiple files, and got a "OneWire.h: No such file or directory" error. However, it did show up in the arduino import library menu. After internet searches, restarts and reinstalls proved unhelpful, I started experimenting on my own because I did have another project that didn't give the error.

I found out that I need to #include it both in the main project file (even though I don't use it there) and in the (.cpp) file that I did use it in. Now my question is: why is this? It is very confusing and non-obvious, and the error messages weren't particularly helpful either (only the no such file one).

Patrick

Now my question is: why is this?

The IDE determines what to compile by scanning the .pde or .ino file for include files. It copies the .pde or .ino files, the header files referenced by the .pde or .ino files, and any other .h and .cpp files in the sketch directory to a temporary directory.

It does not recursively scan header files, nor does it scan header files in the sketch directory.

So, to get OneWire.h copied to the build location, it must be referenced by the sketch.

It is very confusing and non-obvious, and the error messages weren't particularly helpful either (only the no such file one).

True, but is has tripped people before, and there are multiple forum posts about the "problem" and the solution.

If you think about it, a library included by a sketch should NOT include other libraries without the sketch being aware of it. There is nothing more frustrating than finding a sketch that does exactly what you want, only to find that there is a dependency on a library that you don't have, and then discovering that there is a dependency in that library on another library that you don't have, and then discovering that there is a dependency in that library on another library that you don't have.

By making it obvious in the sketch what libraries will be needed, that frustration is reduced.

It also makes it (more) obvious how big the sketch is going to be, and how many (and what) resources are going to be needed.

Hiding the fact that your library needs an ethernet shield is not doing you potential users any favors.

Or as I’d put it the IDE has a bug with #includes! Its annoying in one way - however it does mean you can look at a sketch and see which libraries are actually used without having to trace through all their .h files…

I think the original question was about multi-file projects, not libraries.
If you just want to split out some of your sketch into another file, you can name your files *.ino and you don’t need to put the includes in them, as the *.ino files are included in your main sketch.