referencing libraries

Why is it that you might see in a sketch the reference in brackets like #include <RTClib.h>, and sometimes you see quotation marks: #include "RTClib.h". I see no difference in performance. Can anybody explain?

StackOverflow: What is the difference between #include and #include "filename"?

Use <> for libraries and "" for including “tabs” in your sketch.

Pieter

The angle brackets syntax causes the include paths to be searched for the file. These paths are the library installation folders, the core library, and the toolchain.

The double quotes syntac causes the local path to be searched for the file, then the include paths if the file is not present in the local path.

So if you wanted to #include a file that was in your sketch folder, you must use double quotes and using angle brackets will result in an error. However, if you are #including a library then either one will work.

However, I think it's best practices to always use the most correct syntax. For one, it can convey meaning to the reader as well as to the build system. It also does cause problems in some cases. Unnecessary use of quotes syntax opens up the possibility for filename collision between the intended library file and a sketch file that just happens to have the same name. A more common problem is the use of the angle brackets syntax in libraries for #includes of local files. This works fine when the library is installed normally, since it is in the include path, but if you try to bundle that library with a sketch to create a self-contained package for dependencies management or distribution, those unnecessary angle brackets break the library and you have to go through and change them all.

Too often, I see Arduino code using a random mishmash of both sytaxes without any rhyme or reason, which I'm sure makes things more confusing for beginners who will assume there is some logic to them (e.g.: "why did they use quotes here and braces there?").

I suspect there is a small increase in the compilation time caused by using the double quotes syntax in place of the angle brackets syntax, since it causes the local directory to unnecessarily be checked for the file. Probably it's not noticeable unless you have a project with a large number of these.

PieterP:
StackOverflow: What is the difference between #include and #include "filename"?

Use <> for libraries and "" for including “tabs” in your sketch.

Pieter

Tx Pieter for your reaction, but I have no idea what you mean by "including tabs".

rnieuwland:
I have no idea what you mean by "including tabs".

Although it's common for Arduino sketches to consist of only a single .ino file, you are able to create sketches made of multiple files. The Arduino IDE displays each file of the sketch as a tab. If you click the downward pointing triangle button on the right side of the tab bar in the Arduino IDE, you will get a menu for controlling the tabs.

As you probably know already, sketch files can use the .ino file extension, which causes them to be compiled as Arduino Language. You also can use other file extensions to indicate different language types: .cpp, .c, .S. You can also add header files with the .h file extension. In this case, you would want to add #include directives to your sketch for the .h files in order to have access to the declarations in the file.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.