PDA

View Full Version : Strange problem of unresolved symbols



gib
17th August 2017, 07:39
I am stuck on a weird problem. In several Qt programs I use extended versions of QLabel, QCheckBox and QGroupBox. For example here is qmygroupbox.h

#ifndef QMYGROUPBOX_H
#define QMYGROUPBOX_H

#include <qgroupbox.h>
#include <QMouseEvent>

class QMyGroupBox: public QGroupBox
{
Q_OBJECT
public:
QMyGroupBox(QWidget *parent = 0);
signals:
void groupBoxClicked(QString text);
private:
void mousePressEvent (QMouseEvent *event);
};

#endif

In QtCreator I promote a QGroupBox, for example, to QMyGroupBox. This has been working well for years. Now in one program I am getting link errors:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QMyCheckBox::QMyCheckBox(class QWidget *)" (??0QMyCheckBox@@QAE@PAVQWidget@@@Z) referenced in function "public: void __thiscall Ui_MainWindow::setupUi(class QMainWindow *)" (?setupUi@Ui_MainWindow@@QAEXPAVQMainWindow@@@Z)
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QMyGroupBox::QMyGroupBox(class QWidget *)" (??0QMyGroupBox@@QAE@PAVQWidget@@@Z) referenced in function "public: void __thiscall Ui_MainWindow::setupUi(class QMainWindow *)" (?setupUi@Ui_MainWindow@@QAEXPAVQMainWindow@@@Z)
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QMyLabel::QMyLabel(class QWidget *)" (??0QMyLabel@@QAE@PAVQWidget@@@Z) referenced in function "public: void __thiscall Ui_MainWindow::setupUi(class QMainWindow *)" (?setupUi@Ui_MainWindow@@QAEXPAVQMainWindow@@@Z)

I have not been able to what is different about this program. I am using Qt 4.8.6 with MSVC 11.0 (VS 2010). Other programs with the same toolchain, same usage of the promoted widgets build without problems.

Any suggestions?

high_flyer
17th August 2017, 11:20
Promotion in QtCreator is simply a way to tell QtCreator you are using a subclass of a Qt witdget, so that it knows to include the correct header for your widget, that is all.
Where is the implementation of the header you posted?
The errors are linking error, which means probably the lib or object files of your custom widgets is not found (have a look a previous errors).
Make sure the link paths are correctly set to include the lib of your custom widgets (if you are indeed use a lib).

gib
18th August 2017, 02:42
Promotion in QtCreator is simply a way to tell QtCreator you are using a subclass of a Qt witdget, so that it knows to include the correct header for your widget, that is all.
Where is the implementation of the header you posted?
The errors are linking error, which means probably the lib or object files of your custom widgets is not found (have a look a previous errors).
Make sure the link paths are correctly set to include the lib of your custom widgets (if you are indeed use a lib).

I will undoubtedly reveal my ignorance, but that's OK.

What is really puzzling is that this is a program that builds without any problems on another machine, the only difference that I can see being that the other machine is using Qt 4.8.1 and MSVC 10, rather than Qt 4.8.6 and MSVC 11. The source code, .ui and pro files are all identical. What's more, on both machines I have several other Qt programs using these same extended classes.

There are no compile errors.

I'm puzzled by your question about implementation of the header. All I have ever done to use these custom widgets is make the header files available (I showed qmygroupbox.h, the others look just the same.) There are no .lib or .obj files to be supplied by me, although I see that the MOC has created files like moc_qmygroupbox.cpp and moc_qmygroupbox.obj. Would it help to show the .cpp file?

Just to be sure, I have checked the link command for another project on the same machine that uses the same custom widgets and builds successfully. The link commands are identical. I'm baffled.

high_flyer
18th August 2017, 11:34
... the only difference that I can see being that the other machine is...
Well, it seems you do not understand that object files and/or a lib is needed for the implementation, so what you see is probably what you are aware of to look for.
Just because you can't see the difference it doesn't mean its not there.

On the other machines something else is different which allows on thoese setups for the linker to find the symbols it needs that you do not have on the current machine.


All I have ever done to use these custom widgets is make the header files available (I showed qmygroupbox.h, the others look just the same.) There are no .lib or .obj files to be supplied by me
Unless the implementation of your custom widgets is not in the header it self (and judging from the way the header is written it doesn't seem like that is the case) then there HAS to be a *.cpp file where classes from your headers are implemented, unless you are using a library someone else wrote, then you need the library and the headers will be enough.


although I see that the MOC has created files like moc_qmygroupbox.cpp and moc_qmygroupbox.obj. Would it help to show the .cpp file?
Not at this point, since the link error show that the constructor of your class can't be found, so that is not even a moc issue (yet).

Can you post your pro file?
Maybe we can get some hints from there.
You also can try to look at the errors at build time, search for errors of not found resources (files, libs etc)

gib
18th August 2017, 11:41
You are dead right. I found my mistake, which was (as usual) a stupid one. While I had "pulled" all the source files to other machine, which already had an earlier version of the code, I forgot to add to the .pro file the .cpp file with the custom widget implementations - the result of a code reorganisation. Thanks for your patience, sorry for wasting your time.

Cheers, Gib

high_flyer
18th August 2017, 14:42
No problem, glad to have been able to help.