Results 1 to 7 of 7

Thread: Difference between own headers and Qt's headers

  1. #1
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Difference between own headers and Qt's headers

    Hi,

    I'm building a Qt project with QtCreator and wanted to use some old self-made headers for often used functions.

    After including them via:
    Qt Code:
    1. #include "myHeader.h"
    To copy to clipboard, switch view to plain text mode 

    I got some freaky errors containing
    undefined reference to...
    undefined reference to vtable...
    Well, I know now, that I have to add the full path of my included headers AND the corresponding source files to the project-file.
    Now, the included headers & sources are also shown in the treeview of QtCreator.

    Well, what I'd like to know is: Why do I have to include these files like "normal" files of the project? I mean, I also include many other headers like <QApplication>, <QMessageBox> and so on. Why doesn't linker throw errors on these files too when I don't include them explicitly like I have to with my own headers/sources?

    If someone could answer me this (simple) question?

    Thank you in anticipation!
    Binary

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Difference between own headers and Qt's headers

    I'm building a Qt project with QtCreator and wanted to use some old self-made headers for often used functions.
    Do you also add sources of those functions to your Qt project ? Now it looks like you have only included declarations from header file and the actual definitions are not compiled and linked into the final executable. Make sure you have also
    Qt Code:
    1. SOURCES += myImplementationFile.cpp
    To copy to clipboard, switch view to plain text mode 
    in project's .pro file.
    Another option is to compile your often used functions into dynamic or static library and link to them via "LIBS += .." directive.

  3. #3
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Difference between own headers and Qt's headers

    Hi,

    thank you for your fast support!

    Well, I did what you explained. That is not the problem. Everything works fine since I wrote that:
    Qt Code:
    1. HEADERS += myImplementationFile.h
    2. SOURCES += myImplementationFile.cpp
    To copy to clipboard, switch view to plain text mode 

    BUT what I'd like to know is why I do not have to write this for EVERY header I include (e.g. QApplication.h, QWidget.h, QSettings.h and so on...)..

    You know what I mean?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Difference between own headers and Qt's headers

    The difference is that the implementation for the Qt classes and functions are provided by respective libraries.
    You can do the same thing by providing your own library and adding the path to its headers to the INCLUDEPATH variable and the library itself to the LIBS variable.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Difference between own headers and Qt's headers

    Quote Originally Posted by Binary91 View Post
    If someone could answer me this (simple) question?
    Put simply, when you tell Qt which module(s) you're using via the QT project variable (i.e. QT += core widgets network sql, etc.), Qt will automatically provide the correct include paths and library paths/modules for linking. This is all handled by qmake.

    If your personal code is available via library (static or dynamic), then you would not need to add the source code via the SOURCES project variable, you would simply tell Qt which library(s) to link against and where to find them using the LIBS qmake variable for example (-L for the library directories to search and -l for the library names to include, i.e. -L /home/user/include -l mylibrary)

    If your personal code only exists in source files, then you must incorporate those source files into your project, so that they are compiled and available to link your executable.

    Look at the compiler output window of Qt Creator or command line make output (or qmake generated make files) and you'll see where the Qt include and library paths are automatically passed to the compiler using -I and linker steps are passed -L and -l to find/resolve the Qt code that you told qmake you will use based on the QT project variable.

    So, for built-in Qt stuff, Qt knows where your include and library files are installed, so qmake does most of the setup for you. For your own personal files, you have to tell qmake where your headers and library paths are, as well as any library name(s) to link with. If your code is not in library form, then you must take the additional step to include that code into your project, which will be compiled and linked with your qmake target.

    Make sense?
    Last edited by jefftee; 7th July 2015 at 06:06.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #6
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Difference between own headers and Qt's headers

    Ok, that sounds logically. I don't have any clue of creating libs, but now I know that if I want to handle my headers/sources like the official Qt headers, I have to put my source code into libs..

    Thank you for your support!

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Difference between own headers and Qt's headers

    I have to put my source code into libs..
    No, that's not true at all. What you need is to ensure that the compiled binary code for your source .cpp files is linked into your executable programs. One way to do this is by including your .cpp files as part of your project using the SOURCES += directive in the .pro file. Another way is to build static libraries that include your code and link those into the project. A third way is to build dynamic libraries that include your code and ensure that these libraries are available to be loaded at run time when your program is executed.

Similar Threads

  1. Where are those headers?
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th January 2009, 17:29
  2. Precompiled headers
    By Benjamin in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2008, 15:48
  3. UI headers does not compile
    By bnilsson in forum Qt Tools
    Replies: 1
    Last Post: 30th October 2008, 09:16
  4. Installing headers
    By MathStuf in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2008, 12:05
  5. importing headers
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 23:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.