Different syntax for #include

Hi,

Just a general question about #include statements. Having browsed through 1000's of lines of code recently, there seems to be three different ways of including a library....

Method 1.....

#include "my_lib1.h"

Method 2......

#include <my_lib2.h>

Method 3.....

extern "C" {
  #include <my_lib3.h.h>
}

Qu1 - What's difference (if any) between these 3 methods? (I've looked on the forum but couldn't find any answers)
Qu2 - When writing your own library, why is it necessary to include reference to the header file for itself?
Qu3 - When writing a library that includes other librarys should the include go in the .h file, the .cpp file, or both?

  1. The difference between the first two (" vs. <) has to do with where the include files are looked for. One indicates that the include file is local (") while the other indicates that the include file is in a standard location (<,>).

In general, compilers seem to blur that distinction.

The 3rd version is how to include a header file for C code in a C++ file, to avoid the name-mangling that C++ performs/expects.

  1. The header file for the class needs to be included in the source file for the class, so the compiler knows what functions should be present, and what the arguments to each function are.

  2. If the definition of the library is needed in the class definition, the header file should included in the .h file. If not, it should be included in the .cpp file (and the sketch).

If the header file is included in the .h file, it is not necessary to include it in the .cpp file, too.

1 Like

Thanks Paul!

In general, compilers seem to blur that distinction.

Naw. The difference is the person using it. Often "." is present in the include path which makes the two identical.