328

Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?

What is the difference between angle bracket < > and double quotes " " while including header files in C++?

I mean which files are supposed to be included using eg: #include <QPushButton> and which files are to be included using eg: #include "MyFile.h"???

Community
  • 1
  • 1
Sulla
  • 7,631
  • 9
  • 45
  • 71
  • 7
    This is an exact duplicate of http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename, although the top answer in that one is wrong (it does have to do with the locations the preprocessor searches, but the spec DOES NOT DEFINE THOSE LOCATIONS - current directory is a convention, not a requirement). – Nick Bastin Jul 01 '10 at 22:12

2 Answers2

322

It's compiler dependent. That said, in general using " prioritizes headers in the current working directory over system headers. <> usually is used for system headers. From to the specification (Section 6.10.2):

A preprocessing directive of the form

  # include <h-char-sequence> new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

A preprocessing directive of the form

  # include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

  # include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

So on most compilers, using the "" first checks your local directory, and if it doesn't find a match then moves on to check the system paths. Using <> starts the search with system headers.

M_M
  • 1,955
  • 2
  • 17
  • 23
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 9
    What do you mean by "current working directory"? AFAIK the quotes give priority to the directory in which the file that is using such a directive lives. – Craig Wright Jul 04 '10 at 15:08
  • @Craig, yeah you're right. I wrote something different than what I meant - a side effect of the build system I've been working on lately is that the two are always the same. – Carl Norum Jul 04 '10 at 16:08
  • 11
    +1 for quoting the specifications. This make your answer more verifyable. – charmoniumQ Dec 26 '12 at 04:03
  • Microsoft implementation does NOT start with system headers when <> used – Peter K Oct 25 '13 at 22:12
  • I don't understand how `an implementation-defined manner` = `your local directory`. Is it that `manner` != `places` and that `place` is a somehow meant a static place, so not your dynamic working directory? So there's a `manner` for that which searches your working directory? – Adam Nov 09 '17 at 18:53
  • Interestingly, in the excerpts from the spec above, < > doesn't mention anything about searching _files_, it talks about _places_: they don't have to be files in directories. Whereas " " talks about source _files_. So the difference is, the name in a < > doesn't _have_ to refer to a file. – Steve Folly Nov 23 '17 at 15:50
  • @Adam "an implementation-defined manner" means "each compiler gets to make it's own rules". That's why the answer says "on *most* compilers ...", rather than "on *all* compilers", those compilers have *independently chosen* (more or less) the same rules. If you have an esoteric filesystem where those rules don't make sense, you can have different rules, and still be a conforming C++ compiler – Caleth Jun 21 '18 at 08:38
  • I personally like to include in-house headers with "" and 3rd-party headers with <>. This allows me to be able to suppress warnings wholesale coming in from 3rd-party (that I can do nothing about). You can then specify the 3rd-party include directories on the gcc command-line with -isystem (For MSVC: /experimental:external /external:anglebrackets /external:W0). – Deon McClung Feb 18 '20 at 22:38
90

When you use angle brackets, the compiler searches for the file in the include path list. When you use double quotes, it first searches the current directory (i.e. the directory where the module being compiled is) and only then it'll search the include path list.

So, by convention, you use the angle brackets for standard includes and the double quotes for everything else. This ensures that in the (not recommended) case in which you have a local header with the same name as a standard header, the right one will be chosen in each case.

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
  • 4
    I don't fully agree with this convention. If your project is organized into libraries at some point you may want to make a library standalone. You may want to move this library out of the codebase and rely on package system provided by OS. In such case you may have to refactor all the includes. You can avoid this by passing include path to your compiler and using angle brackets even for your local includes. So I would say: use angle brackets when including library headers and double quote for anything else. – mip Feb 03 '22 at 17:08