I know the title is a little obtuse, let me try to explain: I am trying to create a library of simple functions for use with my TFT Panel.

The Panel has an include file (#include "SSD1306Wire.h")

I have a sketch and I have the above #include file referenced in the sketch I also want to use my library tftHelper.h but that library needs to reference SSD1306Wire.h as well to get the helper functions to work.

Is there a way to reference SSD1306Wire.h one time and be able to use the reference in the functions in my helper library?

I guess, more importantly, what is the proper way to handle a situation like this?


The proper way to handle this is to have an #include guard in the header file to prevent the content from being included more than once.

In your header file filename.h you would add something like this:

#ifndef _FILENAME_H_
#define _FILENAME_H_

// Your header code here

#endif /* _FILENAME_H_ */

The #ifndef block will prevent the code from being included more than once. Best practice is to always put include guards in header files.

| improve this answer | |

A library has no way of knowing what is in your sketch. The two things are completely separate and there is no way to bridge that gap.

You can't include library A into the sketch and expect library B to know about it. Library B has to explicitly have library A included in it for library B to even have a clue what library A is.

There are two important parts to including a library. Not only does it tell the translation unit that you include it in what the library has in it, but it also tells the IDE that you want to use that library and to compile the source code for it.

You see, a library usually contains two distinct things - a header file (or a number of header files) and source code files. The header file describes the library - what classes it has, any exported functions or variables, etc. The source code implements the library. When you include a library it just inserts that definition of the library (the header file) into your source code at the point of the #include. Just like a copy-and-paste.

The IDE does a little extra magic and associates that header file with a directory which has other files in it. It then finds the source code files in that location and compiles them.

Every file that gets compiled gets compiled separately. This is called a translation unit (TU) and each one is completely separate from all the others. None can know what any other file has in it, unless you explicitly tell that file to find something from somewhere else (though you don't tell it where, only that it "exists somewhere else"). And that is what the header files do - they tell the translation units what things are available elsewhere.

So if TU A needs resource F it needs to be told. If TU B needs resource F it also needs to be told.

And because of the IDE's extra magic your main sketch has to also be told about every library in use - whether that library is directly used by the sketch or not - so that it knows to go and find and compile the source code for that library.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.