Newbie: #include directive
-
Hi and welcome to the Qt forums
There really is no difference except including the extention (.h) or not.
The file must exists and be named like shown in the include.You see it in Qt like this
#include <QApplication>
and if u look inside that file
#include "qapplication.h"There is however, difference between
#include "xxx.h"
#include <xxx.h>
In terms of what folders are searched for the file.
"xxx.h" is for project files
<xxx.h> is for system files. -
@mrjj said in Newbie: #include directive:
There really is no difference except including the extention (.h) or not.
Agreed. In Qt libraries, <QApplication>
and
<qapplication.h>` are 2 different files that lead to the same code.However, I recommend
#include <QApplication>
. This is easier to remember: Just include the class name.The file must exists and be named like shown in the include.
You see it in Qt like this
#include <QApplication>
and if u look inside that file
#include "qapplication.h"To clarify @mrjj's point, there is a nice shortcut in Qt Creator to view header files. When you type
#include <QApplication>
in your code, put your mouse cursor over that line and press F2. Qt Creator will then open the header file for you. You can explore Qt code this way. -
Hi,
In addition to what my fellow wrote, using the
#include <QSomething>
notation also allows the "real headers" to be moved if necessary without breaking sources. -
When using an include statement with double quotes (for example, #include "mylibrary.h") it might be worth noting that the preprocessor will look for the specified file from the current working directory, e.g. the parent directory of the file where you specified the include statement. For example, you have a work directory '/home/project/source' containing a mylibrary.cpp and mylibrary.h file. An include statement in the source (.cpp) file would look like the following:
#include "mylibrary.h"
When the preprocessor searches for the file mylibrary.h it will do it from the current work directory of the file mylibrary.cpp (/home/project/source).
If you then decide to place the header file (.h) in a separate directory, e.g. /home/project/include the include statement could be changed into:
#include "../include/mylibrary.h"
Now the preprocessor will search for the mylibrary.h file in the following directory /home/project/source/../include/, which can shortened to /home/project/include.
If in any case the preprocessor fails to find the specified file from the current working directory, it will search again in the same default place the angular brackets uses.