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,

    this error usually occurs if CEditor has no Q_OBJECT macro.

    -Michael.



  • @m.sue thank you

    But there are Q_OBJECT macro in my code.


  • Qt Champions 2016

    @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/designer

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


  • Lifetime Qt Champion

    Hi,

    Did you follow the Splitting up the plugin recommendation in the custom widget for Designer documentation ?



  • @SGaist thanks for reminding me

    I have two custom widgets, A(a.dll,a.lib) and B(b.dll,b.lib) .

    linking a custom widget(A) into another custom widget(B) has problems.

    Unable to generate a custom widget library file(b.dll and b.lib)


  • Lifetime Qt Champion

    Are you linking all dependencies correctly ?



  • This post is deleted!


  • @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 = lib

    HEADERS = 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 += target

    include(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 generic

    The Second custom designer widget code as follows:
    [SecondCustomWidget.pro]
    CONFIG += plugin release
    TARGET = $$qtLibraryTarget(secondcustomwidgetplugin)
    TEMPLATE = lib

    HEADERS = 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 += target

    include(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 generic

    the 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


  • Lifetime Qt Champion

    So you are not doing the splitting suggested in the documentation I linked to ?



  • @SGaist Thank you very much.

    I solved the problem.The staticlib option must be used.

    "CONFIG += staticlib"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.