How to resolve error LNK2019?
-
Hello everybody,
my system is follows:
OS:WIN10 Qt:5.8.0 Compiler:MSVC2015 qtcreator:4.3.0
I created a qtcreator custom control,CEditer(cediterplugin.dll and cediterplugin.lib). It works well.
.h file as follows:
CEditer.h:
#include <QtUiPlugin/QDesignerExportWidget> namespace Ui { class CEditerForm; } // namespace Ui class QDESIGNER_WIDGET_EXPORT CEditer: public QWidget { Q_OBJECT public: CEditer(); XXX . . . private: Ui::CEditerForm *ui; };
Now,I'm going to create another custom control,CEditPanel(ceditpanelplugin.dll and ceditpanelplugin.lib). It contains the CEditer control.
CEditPanel.h:
#include <QtUiPlugin/QDesignerExportWidget> #include "CEditer.h" #pragma comment(lib,"cediterplugin.lib") namespace Ui { class CEditerPanelForm; } class QDESIGNER_WIDGET_EXPORT CEditerPanel : public QWidget { Q_OBJECT public: CEditerPanel(); XXX . . . private: Ui::CEditerPanelForm *ui; };
"ui_CEditerPanelForm.h"
#include "CEditer.h" QT_BEGIN_NAMESPACE class Ui_CEditerPanelForm { public: QHBoxLayout *horizontalLayout_3; CEditer *editer; void setupUi(QWidget *CEditerPanelForm) { ... } namespace Ui { class CEditerPanelForm: public Ui_CEditerPanelForm {}; } // namespace Ui
.pro file segment as follows:
QT_DESIGNER_PLUGIN_PATH = $$[QT_INSTALL_PLUGINS]/designer LIBS += \ -L$${QT_DESIGNER_PLUGIN_PATH} \ -lcediterplugin
When I compiled the program,I got the following link error:
ceditpanelplugin.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const CEditer::staticMetaObject" (?staticMetaObject@CEditer@@2UQMetaObject@@B),referenced in function "public: static class QString __cdecl CEditer::tr(char const *,char const *,int)" (?tr@CEditer@@SA?AVQString@@PBD0H@Z) ceditpanel.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const CEditer::staticMetaObject" (?staticMetaObject@CEditer@@2UQMetaObject@@B) moc_ceditpanel.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const CEditer::staticMetaObject" (?staticMetaObject@CEditer@@2UQMetaObject@@B)
I guess maybe the problem is dllexport or dllimport settings,But I don't know how to solve the error.
someone can help me?
-
@A.Michael
Hi
have you check that $$[QT_INSTALL_PLUGINS] actually maps to the place where the .lib and dll are ?message({$$[QT_INSTALL_PLUGINS])
for me it gives
Project MESSAGE: {C:/Qt/5.8/msvc2015/plugins}But the path where my Designer plugin needs to be is
C:/Qt/Tools/QtCreator/bin/plugins/designerYour setup might vary of course but do check what the variable contains.
-
@mrjj Thank you for your answer.
There's nothing wrong with my .pro file. I'm pretty sure.
Everything is OK when I compiled a EXE file with cediterplugin.dll and cediterplugin.lib.
The link error 2019 occurs only when I created a custom control that uses another custom control.
The only difference between a exe program and a custom control plugin is the QDESIGNER_WIDGET_EXPORT .
QDESIGNER_WIDGET_EXPORT is a null value, when compiling a exe program
so, I guess it might be the QDESIGNER_WIDGET_EXPORT setting problem.
-
Hi,
Did you follow the Splitting up the plugin recommendation in the custom widget for Designer documentation ?
-
-
Are you linking all dependencies correctly ?
-
@SGaist I don't know what's wrong with it.
The mistake is easy to reproduce.
Two custom designer widgets,examples:
The First custom designer widget code as follows:
[ FirstCustomWidget.pro]
CONFIG += plugin release
TARGET = $$qtLibraryTarget(firstcustomwidgetplugin)
TEMPLATE = libHEADERS = firstcustomwidgetplugin.h
SOURCES = firstcustomwidgetplugin.cpp
RESOURCES = icons.qrc
LIBS += -L.greaterThan(QT_MAJOR_VERSION, 4) {
QT += designer
} else {
CONFIG += designer
}target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += targetinclude(firstcustomwidget.pri)
lib_out_path = $${PWD}/../../LnkTester/build/lib
include_out_path = $${PWD}/../../LnkTester/build/include$$[QMAKE_SPEC] {
CONFIG(release, debug|release) {
QMAKE_POST_LINK += copy /y release\.dll "$${target.path}" &
QMAKE_POST_LINK += copy /y release\.dll "$${lib_out_path}" &
QMAKE_POST_LINK += copy /y release\*.lib "$${lib_out_path}"
}
}[FirstCustomWidget.h]
#ifndef FIRSTCUSTOMWIDGET_H
#define FIRSTCUSTOMWIDGET_H#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QtUiPlugin/QDesignerExportWidget>class QDESIGNER_WIDGET_EXPORT FirstCustomWidget : public QWidget
{
Q_OBJECT
public:
FirstCustomWidget(QWidget * parent = 0);
private:
QLabel * _label;
};
#endif[ FirstCustomWidget.cpp]
#include "firstcustomwidget.h"FirstCustomWidget::FirstCustomWidget(QWidget * parent) :
QWidget(parent), _label(new QLabel(this))
{
}[firstcustomwidgetplugin.h] [firstcustomwidgetplugin.cpp]
These two files are genericThe Second custom designer widget code as follows:
[SecondCustomWidget.pro]
CONFIG += plugin release
TARGET = $$qtLibraryTarget(secondcustomwidgetplugin)
TEMPLATE = libHEADERS = secondcustomwidgetplugin.h
SOURCES = secondcustomwidgetplugin.cpp
RESOURCES = icons.qrc
LIBS += -L.greaterThan(QT_MAJOR_VERSION, 4) {
QT += designer
} else {
CONFIG += designer
}target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += targetinclude(secondcustomwidget.pri)
lib_out_path = $${PWD}/../../LnkTester/build/lib
INCLUDEPATH += $${PWD}/../../LnkTester/FirstCustomWidget
$$[QMAKE_SPEC] {
CONFIG(release, debug|release) {
LIBS += -L$${lib_out_path} -lfirstcustomwidgetplugin
}
}[secondcustomwidget.h]
#ifndef SECONDCUSTOMWIDGET_H
#define SECONDCUSTOMWIDGET_H#include <QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
#include "firstcustomwidget.h"class QDESIGNER_WIDGET_EXPORT SecondCustomWidget : public QWidget
{
Q_OBJECT
public:
SecondCustomWidget(QWidget * parent = 0);
~SecondCustomWidget();
private:
FirstCustomWidget *_cwidget;
};#endif
[secondcustomwidget.cpp]
#include "secondcustomwidget.h"SecondCustomWidget::SecondCustomWidget(QWidget *parent) :
QWidget(parent),_cwidget(new FirstCustomWidget(this))
{
}SecondCustomWidget::~SecondCustomWidget()
{
delete _cwidget;
}[secondcustomwidgetplugin.h] [secondcustomwidgetplugin.cpp]
These two files are genericthe lnk error is as follows:
Generating Code...
link /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /DLL /SUBSYSTEM:WINDOWS /OUT:release\secondcustomwidgetplugin.dll @E:\TEMP\nm4C1A.tmp
Creating library release\secondcustomwidgetplugin.lib and object release\secondcustomwidgetplugin.exp
secondcustomwidgetplugin.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const FirstCustomWidget::staticMetaObject" (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B) referenced in function "public: static class QString __cdecl FirstCustomWidget::tr(char const *,char const *,int)" (?tr@FirstCustomWidget@@SA?AVQString@@PBD0H@Z)
secondcustomwidget.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const FirstCustomWidget::staticMetaObject" (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B)
moc_secondcustomwidget.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const FirstCustomWidget::staticMetaObject" (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B)
release\secondcustomwidgetplugin.dll : fatal error LNK1120: 1 unresolved externals -
So you are not doing the splitting suggested in the documentation I linked to ?