1.6.1.  #include: Finding Header Files

[ fromfile: qmake.xml id: findingheaders ]

The three ways to #include a library header file are

  #include <headerFile>
  #include "headerFile"
  #include "path/to/headerFile"
  

The angle brackets (<>) indicate that the preprocessor must look (sequentially) in the directories listed in the include path for the file.

A quoted filename indicates that the preprocessor should look for headerFile in the including file's directory first. A quoted path indicates that the preprocessor should check the path directory first. The path information can be absolute or relative (to the including file's directory). If no file is found at the specified location, the directories listed in the include path are searched for headerFile.

If versions of the file exist in more than one directory in the include path, the search stops as soon as the first occurrence of the file has been found. If the file is not found in any of the directories of the search path, then the compiler reports an error.

When the compiler was installed it was told where to find the header files in the C++ Standard Library. For other libraries, you can expand the search path by adding the switch -I/path/to/headerfile to the invocation of the compiler.

If you use an IDE, there will be a Project->Settings->Preprocessor, or Project->Options->Libraries configuration menu that lets you specify additional include directories, which get passed as -I switches to the compiler at build time.

With qmake, as you will soon see, you can add INCLUDEPATH += dirName lines to the project file. These directories end up in the generated Makefile as INCPATH macros, which then get passed on to the compiler/preprocessor at build time.

[Tip] qmake -r

Some project files are SUBDIRS, which means they run both qmake and make recursively down the tree. If you have an old Makefile from a previous version of Qt and want to force all Makefiles to be regenerated immediately, invoke qmake -r, to recursively generate all Makefiles down the file tree.

[Tip] Tip

In general, it is a good idea to #include non-Qt header files after Qt header files. Because Qt defines many symbols (for the compiler and for the preprocessor) this can make it easier to avoid (or locate) name clashes.

For more information about the preprocessor and how it is used, see Section C.2.