einpoklum
einpoklum

Reputation: 132128

Should I use quotes or angle brackets for headers within same library?

Suppose I'm writing a library named foo, in C++. When installed to /some/where, it presents include files in /some/where/include/foo, and I expect the user to compile with -I/some/where/include.

Suppose foo has header files foo/first.hpp, foo/second.hpp and foo/bar/third.hpp (and they have these relative locations both before and after installation).

Now, within these files, when including other foo header files, should I...

  1. Always use angle brackets, in line with my assumption stated above? e.g.
    #include <foo/second.hpp>
    #include <foo/bar/third.hpp>
    
  2. Never use angle brackets, e.g.
    #include "second.hpp"
    #include "bar/third.hpp"
    
    or
    #include "../first.hpp"
    
  3. Use angle brackets in some cases, and quotes otherwise?

I've noticed that Boost seems to take approach (1.), and I like it, because it clarifies these include files are part of the foo library. However, the accepted answer to this SO question suggests approach (2.) (albeit for the C language).

The C++ coding guideline SF.12 is actually somewhat ambiguous about this matter. On the one hand, it says:

use the quoted form for including files that exist at a relative path to the file containing the #include statement (from within the same component or project) and to use the angle bracket form everywhere else

but then also

Library creators should put their headers in a folder and have clients include those files using the relative path #include <some_library/common.h>

but the library is also a client of its own include files; and if you've put the headers in a library-specific folder, then perhaps this rule applies (as the rationale for the earlier rule does not seem to hold when a library-specific folder is used).

Upvotes: 4

Views: 1294

Answers (1)

einpoklum
einpoklum

Reputation: 132128

After mulling this over for a while I'd decided it's gotta be quotes and relative paths.

You don't want choices of include paths to make you include the wrong file by mistake; a quoted include path is guaranteed to get you the right include regardless of anything else.

(Caveat: This is assuming that the relative path between the includer and includee files in the library remains the same after installation, otherwise it's a different ballgame.)

Upvotes: 0

Related Questions