5

I know that in C++, to include a library, you either put the library name in <x> or "x". Now, I believe that this was because of a major change in the C++ language a some time ago.

Now, in Arduino, there are 2 types of Wire (I2C) library : <Wire.h> and "Wire.h".

Which one is the right one?

I've seem people using both, and they both seem to work.

2 Answers 2

4

The proper distinction is exactly what paths are searched for the include files.

<...> searches the "system" paths, while "..." searches both the system an "local" paths.

From the GCC manual:

-I dir

Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated . If dir begins with =, then the = will be replaced by the sysroot prefix; see --sysroot and -isysroot.

-iquote dir

Search dir only for header files requested with #include "file"; they are not searched for #include <file>, before all directories specified by -I and before the standard system directories. If dir begins with =, then the = will be replaced by the sysroot prefix; see --sysroot and -isysroot.

Note that the Arduino IDE only uses -I, so there is little to no distinction between <...> and "...".

Commonly the main usage of the distinction is to use <...> to include system or library headers, and "..." to include headers that are part of the actual sketch / application, since the "current directory" is automatically added as a local include path.

3
  • So both "Wire.h" and <Wire.h> would work?
    – Dat Ha
    Dec 2, 2016 at 21:11
  • Yep. I commonly see both being used, though I for one only ever use <Wire.h>.
    – Majenko
    Dec 2, 2016 at 21:12
  • Specifically, "file.h" searches the directory containing the current file first. Thus, "file.h" would work for both, finding your local version of file.h before searching the library. However I write "file.h" or <file.h> to explicitly specify - to the reader - which one I mean to include. See [this page describing the gnu compiler's "include" syntax]{gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html)
    – JRobert
    Dec 2, 2016 at 23:25
4

http://en.cppreference.com/w/cpp/preprocessor/include

#include <filename>

The intent of this syntax is to search for the files under control of the implementation. Typical implementations search only standard include directories. The standard C++ library and the standard C library are implicitly included in these standard include directories. The standard include directories usually can be controlled by the user through compiler options.

#include "filename"

The intent of this syntax is to search for the files that are not controlled by the implementation. Typical implementations first search the directory where the current file resides and, only if the file is not found, search the standard include directories as with (1).

So basically it's intent is to give some idea of where the file should be. I, myself use "" for files that are inside the directory of my program. And <> for files or libraries that are in my path or other external locations.


This question is also more related to programming in general as Arduino specific.

Also see: https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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