Where does Qt find header files?

This seems an old topic. If you ever learned C programming language, you must know the search rules for the header files. The search rules of gcc for headers are simple: for quote form of include directives, gcc looks up first in the same directory as the file that contains the include directive, if not found, gcc looks up in system defined directory. The system default directories are coded into the binary of gcc, which can be seen by running the command “cpp -v”. The result looks like this:

  • ../lib/gcc/x86_64-w64-mingw32/x.x.x/include
  • ../lib/gcc/x86_64-w64-mingw32/x.x.x/include-fixed
  • ../lib/gcc/x86_64-w64-mingw32/x.x.x/../../../../x86_64-w64-mingw32/include

So, these are relative paths. The real paths depend on the installation directory of gcc/cpp.

For angle-bracket form of #include directives, gcc only searches the headers in the default system directories.

However, if you only remember the above two rules, you must be confused about how qt finds the header files in this #include directory:

#include <QCoreApplication>

The file QCoreApplication is apparently not in the above system default search paths. It is a Qt header file instead of a mingw header file. To understand why Qt can find it, you should know the third rule for searching the header files: if you provide -Idir at the gcc command line, the dir is searched before the standard system directories. This is true for both  #include “” and #include <>. But for #include “”, the current directory is searched before the -Idir.

When you build your project in Qt Creator, qmake will add some -Idir in the generated Makefile. For example, you are building the debug version of your project, the -Idir would be:

-I..\myapp -I. -IC:\Qt5.8.1\5.8.1\mingw73_64\include -IC:\Qt5.8.1\5.8.1\mingw73_64\include\QtGui -IC:\Qt5.82.1\5.8.1\mingw73_64\include\QtANGLE -IC:\Qt5.8.1\5.8.1\mingw73_64\include\QtCore -Idebug -IC:\VulkanSDK\\include -IC:\Qt5.8.1\5.8.1\mingw73_64\mkspecs\win32-g++

in which ..\myapp is your project directory, . is the building directory, debug is the directory under the building directory that is used to store the building outcomes(the .o and the .exe files). C:\Qt5.8.1\5.8.1\mingw73_64\include\QtCore, etc. are in the Qt installation directory. The file QCoreApplication is right in C:\Qt5.8.1\5.8.1\mingw73_64\include\QtCore so the compiler can find it without problem.

With more and more modules are added to .pro file, qmake will add more -Idirs. For example, if you add “QT += widgets” in .pro file, you will get one extra -Idir:”-IC:\Qt5.8.1\5.8.1\mingw73_64\include\QtWidgets”.

Note the search order: the current directory, -I dir, standard system directories. If gcc finds the header in one of the directories, it stops searching further, which means you can provide a header file to over-ride another header with the same name in the latter directories.


If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply