How to include a common.h for different sketches?

Hi, I have a file with a lot of standard stuff in that I want to use in a bunch of different sketches.

I started by saving it in the current sketch folder and using
#include “common.h”
and that worked.

But now I have several more sketches I want use it in and I don’t want to make multiple copies in every sketch folder. I tried putting it in the Arduino folder, the base and extra library folders and it doesn’t find it and I get “fatal error: common.h: No such file or directory”

I changed the include to…
#include <common.h>

and it still can’t find it.

Is there a setting somewhere that defines the Source search path and/or a common folder that is always searched?

In your sketchbook folder, where the folders with all the sketches are, there should be a folder called Libraries. If there’s not one, then make one. That’s where you put libraries that you download or write yourself. Then use the <> version of include. Don’t put them in the folder where the Arduino core is installed.

Thanks, but as I have stated, it is just a single header file and not a complete library.

Anyway, moot point, I have figured it and all working as I need.

WaitSome:
Thanks, but as I have stated, it is just a single header file and not a complete library.

Yeah, it still goes in the same place. What do you think a library is if not a header and possibly a .cpp to go with it?

Delta_G:
What do you think a library is

I suspect there is a common misconception that a library is a very complex thing and bears no relation to a single .h file. And that the libraries directory is where you put libraries that you download (assuming the person has not advanced his/her skills to the point where s/he knows how to create his/her own libraries.

I confess it had never occurred to me to put a common.h file in the libraries directory - it does not seem (to me) a natural place to put a file that is specific to a single project. And, presumably you have to put it in a directory called common ?

@WaitSome, if for any reason you don’t want to put the common.h file in the library directory you can put it anywhere you like as long as you refer to it by its full path name

#include "full/path/to/common.h"

…R

Robin2: I confess it had never occurred to me to put a common.h file in the libraries directory - it does not seem (to me) a natural place to put a file that is specific to a single project. And, presumably you have to put it in a directory called common ?

If it is only going to be used in one sketch then in the folder with the sketch seems like the most reasonable place. But the OP was asking where to put it so that it would be available to multiple sketches. That sort of hits the concept of the library right on the head.

Delta_G:
That sort of hits the concept of the library right on the head.

I guess we started out with very different concepts of what the library directory was intended for. I don’t think the Arduino documentation is very helpful on this point.

I still have the notion that the library directory should be the location for code that is used in many unrelated projects (eg the Servo library - yes, I know it is not stored there). And the shared code for a particular project (eg data shared between a wireless Tx and Rx) should just be in a sub directory of the project.

I would go even further and say that I would prefer to copy the Servo library into my project directory so that I could be sure when I next come to compile the project there is absolutely no risk of the library having been changed

…R

Robin2: I still have the notion that the library directory should be the location for code that is used in many unrelated projects

That's exactly what the OP is asking for is it not? Where can he put the code so that it can be included in various projects without needing a separate copy in each one.

WaitSome: Hi, I have a file with a lot of standard stuff in that I want to use in a bunch of different sketches.

...

Is there a setting somewhere that defines the Source search path and/or a common folder that is always searched?

Delta_G: Yeah, it still goes in the same place. What do you think a library is if not a header and possibly a .cpp to go with it?

But I had tried that ("base and extra library folders") and it didn't find it there. I eventually put it in with the main Ethernet.h/.cpp which I use in all sketches for this project.

Before asking for help, I had also tried creating a Library folder and put it there but the IDE complained "not a valid library" (or something close to that)

There are some rules to libraries with the IDE. The folder has to have the same name as the .h file you are including for instance. And IIRC it can't have any dashes or slashes in the name.

So for instance, all my sketches go into a folder at:

C:/DavidsDocs/Arduino

There is a folder in there called

C:/DavidsDocs/Arduino/Libraries

and in that folder are a bunch of folders with .h files (and some .cpp files). Each folder has the same name as the .h inside.

This is completely separate from the core libraries that are where I installed Arduino at:

F:/Programs/Arduino

Nothing there should be changed.

Maybe it is the 1.6.5 version of IDE that is balking at the "valid library," as I had tied that and that's when I get the "not a valid..."

The "extra" Library I referred to is the one similar to your "C:/DavidsDocs/Arduino/Libraries" mine is

D:\ArduinoWork\Libraries

And all the libraries like RF24 etc are in their own folders there. I created a folder "myStuff" in there and then put the "common.h" in it but it balked.

WaitSome: I created a folder "myStuff" in there and then put the "common.h" in it

Delta_G: The folder has to have the same name as the .h file you are including

So you either need to name the folder common or name the .h file myStuff.h

Delta_G: That's exactly what the OP is asking for is it not? Where can he put the code so that it can be included in various projects without needing a separate copy in each one.

Exactly. Thanks again and while I have a clunky work-around it would be nice to know where the IDE Path string is that it refers to when compiling.

I am very new to Arduino but not so for programming in general and IDEs I use all have a place to define their needed local Paths. Even if I did not want to add another Path to the list I could see where it was currently looking and put a .h file there.

Just another -missing- brick in the wall. :)

WaitSome:
I am very new to Arduino but not so for programming in general and IDEs I use all have a place to define their needed local Paths. Even if I did not want to add another Path to the list I could see where it was currently looking and put a .h file there.

The issue is that the Arduino IDE is for the raw beginners so they hide out all that sort of advanced stuff so as not to confuse them when they are just starting out. If you want a more functional IDE then Eclipse has a plugin for Arduino that I like a lot. Personally, the only time I open the Arduino IDE is when I am here trying to answer a question about it. For my code I use Eclipse.

Well, if it was real Unix, the library object code would go someplace like /usr/local/lib/common/libcommon.a, and the .h file would go in /usr/local/include/common.h, right? Which is essentially just as arbitrary...

Delta_G:
Eclipse has a plugin for Arduino that I like a lot.

Thanks, is that that the Baeynts one? I have been trying to install it but all the current downloads (x64, x86) have a broken file and 7zip balks with “file broken.” Can’t seem to find a way to contact guy without joining on Patreon. A bit of a jerk move asking for money before I can try it. Good work if you can get it. :slight_smile:

Delta_G:
So you either need to name the folder common or name the .h file myStuff.h

Aaarrggghhh OK, that worked. The sooner I can get away from this IDE the better.

WaitSome:

Delta_G: That's exactly what the OP is asking for is it not? Where can he put the code so that it can be included in various projects without needing a separate copy in each one.

Exactly.

I guess I had misinterpreted this piece from the original post

I have a file with a lot of standard stuff in that I want to use in a bunch of different sketches

insofar as I assumed the different sketches had some connection with each other (the reason for them needing to share the common.h file) as part of a larger project.

...R