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...
- Always use angle brackets, in line with my assumption stated above? e.g.
#include <foo/second.hpp> #include <foo/bar/third.hpp>
- Never use angle brackets, e.g.
or#include "second.hpp" #include "bar/third.hpp"
#include "../first.hpp"
- 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).