Search code examples
c++qtlinkermetamoc

Qt Link Issue with MyClass::metaobject VS2005


Just quickly before I start, I have searched SO and Google for a length of time trying to solve this and have been unsuccessful.

I am trying to compile my project, which used to use a certain library for providing GUI functionality based on Windows Forms. Now my company has started to move to Qt, and I decided I would start to convert my small application to support Qt also.

At first it would not compile at all, due to missing headers. Now that is sorted, I am stuck with my final .exe not being able to link due to the following errors.

Creating library Bin\VS_V8\Win32\Debug\Disp.lib and object Bin\VS_V8\Win32\Debug\Disp.exp
QtMainMenu.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QtMainMenu::metaObject(void)const " (?metaObject@QtMainMenu@@UBEPBUQMetaObject@@XZ)
QtMainMenu.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QtMainMenu::qt_metacast(char const *)" (?qt_metacast@QtMainMenu@@UAEPAXPBD@Z)
QtMainMenu.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QtMainMenu::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QtMainMenu@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
Bin\VS_V8\Win32\Debug\Disp.exe : fatal error LNK1120: 3 unresolved externals

Our company has a small "Qt Test App" that was written to play around with, which is where I have based my import from. I can compile and link that fine. From what I have gathered, the following properties must be met with Qt files:

  • Make sure QTDir is included
  • Add to the UI Files the UIC Compiler
  • Add to the Resoruce Files the Resource Compiler
  • Add to the created Header Files the MOC compiler.

I noticed I was missing the MOC Build Tool commands, so I have added them to my UI File's header. However this didnt change the linking problem. I read that I should delete all built files and do a clean to solve it. This didnt work either.

I have checked, and Q_OBJECT is defined in the class.

I am using VS2005 with the Qt Addin. I did not start a new project for Qt however, I am just using the old Visual Studio Solution / VCProj.

Any ideas where to go next?


Solution

  • @Cameron Stubber you need moc object .. You need to modify Custom Build Step.

    You can find Custom Build Step in header file (which has Q_OBJECT) Properties by right click. Then type this commands ;

    Command Line = $(QTDIR)\bin\moc.exe -I"$(QTDIR)\include\QtCore" -I"$(QTDIR)\include\QtGui" -I"$(QTDIR)\include" -I"$(QTDIR)\mkspecs\$(QMAKESPEC)" finddialog.h -o debug\moc_finddialog.cpp
    
    Description = MOC finddialog.h
    
    Outputs = debug\moc_finddialog.cpp
    
    Additional Dependencies = $(QTDIR)\bin\moc.exe;finddialog.h
    

    But carefull by writing this types you need to change somethings here like $(QTDIR) it is my enviroment variables you need to write full form of where your QT located like D:\qt_5\

    and also you need to be carefull finddialog you should write your .h and .cpp files name

    And then you need to create a folder by right clicking solution explorer Add\New Filter .. Make folder name as Generated Files

    And last step right click Generated Files add\existing item and you will see Debug folder in your solution Project and add moc_"projectName".cpp

    Then re-build your solution. Problem will be solved.

    Also you should add C/C++ /General/Additional Library Directories

    $(QTDIR)\include
    $(QTDIR)\include\QtGui
    $(QTDIR)\include\QtCore
    

    And Link/General/Additional Library Directories

    $(QTDIR)\lib
    

    Link/Input/Additional Dependencies

    qtmaind.lib
    QtCored4.lib
    QtGuid4.lib
    

    But dont forget to change $(QTDIR) to your enviroment variables or location of your qt folder. Like D:\qt_4.7.4