Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Newbie: #include directive
Qt 6.11 is out! See what's new in the release blog

Newbie: #include directive

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CharlesC
    wrote on last edited by
    #1

    Newbie questions
    May I know the difference between

    #include <xxxx>

    against

    #include <xxxx.h>

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      JKSHJ 1 Reply Last reply
      2
      • mrjjM mrjj

        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.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Mike NeuhausM Offline
            Mike NeuhausM Offline
            Mike Neuhaus
            wrote on last edited by Mike Neuhaus
            #5

            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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved