7

Is there a known benefit of using #include "..." instead of #include <...> for include directives placed into public headers?

For example, given a public header file which needs to include a file besides it, should it use:

#include "file.h"

or

#include <mylib/file.h>

(assuming the headers are installed in the mylib subdirectory of the include install directory)?

Amongst the common practice, libxml2 uses angle brackets while curl uses double quotes.

eepp
  • 7,255
  • 1
  • 38
  • 56
  • If you use angle brackets it probably won't work unless these are system headers. – Barmar Dec 05 '18 at 22:36
  • 4
    Possible duplicate of [What is the difference between #include and #include "filename"?](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) – Jongware Dec 05 '18 at 22:37
  • 2
    @usr2564301 I know the technical difference, I'm looking for the good practice when writing a library's public headers. – eepp Dec 05 '18 at 22:38
  • Related question, perhaps a duplicate (there should be no difference between C/C++ regarding this question): https://stackoverflow.com/questions/53410038 – Emile Cormier Apr 29 '22 at 23:26

2 Answers2

2

The general rule is to use angled brackets to include header files that are part of the system includes and quotes for everything else.

Don Hosek
  • 981
  • 6
  • 23
0

If you use angle brackets and there are to files at (in different include directories) you can get the wrong one. If you use "file.h" you have a better chance of getting the one you expect.

Double-quotes may also make it possible for people to use your library without adding any include dirs.

Basically, if you have a choice I'd recommend double quotes.

nemequ
  • 16,623
  • 1
  • 43
  • 62
  • 1
    _If you use angle brackets and there are to files at (in different include directories) you can get the wrong one._ I don't agree. The search path is always the same for a given compiler instance, so you will always get the same one. For example, with my question's example, `` will be located in the same `mylib` directory that is used when including the file which has this directive (through ``). – eepp Dec 05 '18 at 22:43
  • 2
    That assumes your library was included with ``. You can tell people to do it that way, but they don't always listen. Sure, then their code is broken, but odds are good your going to have to explain how to fix it, and there isn't really any benefit in doing it that way… – nemequ Dec 05 '18 at 23:12