Important: Please read the Qt Code of Conduct -

I customize a slot in a class inherited class QObject. The error LNK2001 occurs.

  • Hello, everyone,

    I use Qt creator 3.2.1 based on Qt 5.3.2 on windows7 64bit.
    The compiler kit is Desktop Qt 5.3 MSVC2013 64bit (autodetected) using the compiler: Microsoft Visual C++ Compiler 12.0 (x86_amd64).

    Here are the codes:

    #include <QMainWindow>
    #include <QApplication>
    #include <QObject>
    #include <QTextCodec>
    #include <QDialog>

    QTextCodec *codec = QTextCodec::codecForName("BIG5");

    class SignalAndSlot : public QObject
    public slots:
    void testSlot(void)



    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QMainWindow w;
    SignalAndSlot *connector1 = new SignalAndSlot;
    return a.exec&#40;&#41;;


    After building it, it returns LNK2001 error messages:

    bq. C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\qttest4.pdb -DUNICODE -DWIN32 -DWIN64 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013_64\include" -I"C:\Qt\5.3\msvc2013_64\include\QtWidgets" -I"C:\Qt\5.3\msvc2013_64\include\QtGui" -I"C:\Qt\5.3\msvc2013_64\include\QtANGLE" -I"C:\Qt\5.3\msvc2013_64\include\QtCore" -I"debug" -I"." -I"C:\Qt\5.3\msvc2013_64\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\user\AppData\Local\Temp\main.obj.5956.78.jom
    ..\qttest4\main.cpp(28) : warning C4189: 'connector1' : 已初始化區域變數,但並未參考
    echo 1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID / 24 / RT_MANIFEST / "debug\qttest4.exe.embed.manifest">debug\qttest4.exe_manifest.rc
    if not exist debug\qttest4.exe if exist debug\qttest4.exe.embed.manifest del debug\qttest4.exe.embed.manifest
    if exist debug\qttest4.exe.embed.manifest copy /Y debug\qttest4.exe.embed.manifest debug\qttest4.exe_manifest.bak
    link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='' publicKeyToken='6595b64144ccf1df' language='
    ' processorArchitecture='*'" /MANIFEST /MANIFESTFILE:debug\qttest4.exe.embed.manifest /OUT:debug\qttest4.exe @C:\Users\user\AppData\Local\Temp\qttest4.exe.5956.1575.jom
    main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl SignalAndSlot::metaObject(void)const " (?metaObject@SignalAndSlot@@UEBAPEBUQMetaObject@@XZ)
    main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl SignalAndSlot::qt_metacast(char const *)" (?qt_metacast@SignalAndSlot@@UEAAPEAXPEBD@Z)
    main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl SignalAndSlot::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@SignalAndSlot@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    I've checked this on the Internet. It seems running qmake and rebuilding it can solve this problem.
    I tried but the problem is still there, so I am here looking for help.
    If my expression makes you feeling difficult to understand, please let me know.
    I'll try to express my question more clear.

    Thanks for your help.

  • Moderators

    Hi, welcome to devnet.

    You've put your class in a .cpp file. MOC (the Qt meta object compiler) only processes header files (for things like Q_OBJECT macros, signals , slots etc.). Put your class in proper .h/.cpp files where it belongs and it will work.

    After you do, re-run qmake to make sure it picks up the changes (usually not necessary but can't hurt).

  • Thank you very much, Mr. Kawa. Problem solved!

Log in to reply