PDA

View Full Version : Difference between own headers and Qt's headers



Binary91
6th July 2015, 19:50
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:

#include "myHeader.h"

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

stampede
6th July 2015, 20:48
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


SOURCES += myImplementationFile.cpp

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.

Binary91
6th July 2015, 20:53
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:

HEADERS += myImplementationFile.h
SOURCES += myImplementationFile.cpp

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?

anda_skoa
6th July 2015, 20:58
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,
_

jefftee
7th July 2015, 04:14
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?

Binary91
7th July 2015, 11:32
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!

d_stranz
8th July 2015, 04:03
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.