Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt metaObject linker problem

After integrating Qt with Vs and trying to compile .pro file I'm getting following errors:

Error   9   error LNK2001: unresolved external symbol "public: virtual int __thiscall Multiplication_dialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Multiplication_dialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z)     

Error   7   error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Multiplication_dialog::metaObject(void)const " (?metaObject@Multiplication_dialog@@UBEPBUQMetaObject@@XZ)  


    Error   8   error LNK2001: unresolved external symbol "public: virtual void * __thiscall Multiplication_dialog::qt_metacast(char const *)" (?qt_metacast@Multiplication_dialog@@UAEPAXPBD@Z)    

What to do with this?

like image 640
smallB Avatar asked Jul 10 '11 17:07

smallB


4 Answers

You usally get these errors when the moc_foo.cpp for foo.h (which contains your class marked with Q_OBJECT) is not compiled / linked in your project.

To make a Qt project work in VS you either

  1. Create a .vcproj file with 'qmake -tp vc' or
  2. Use the Qt Visual Studio Add-in which handles all the moc magic automatically for you (doesn't work with VC Express versions though).

When using the add-in you can trigger the creation of the moc_foo.cpp by

  • Make sure the header file of the object in question appears in the VS project
  • List item
  • remove all occurrances of Q_OBJECT from the header file of Multiplication_dialog.
  • save the file
  • add Q_OBJECT again
  • save the file

Now you should have two versions of moc_multiplication_dialog.cpp in your "Generated Files" folder in the Solution Explorer. One for "Debug" and one for "Release". Make sure that one of these files is not excluded from build.

like image 157
jobor Avatar answered Oct 21 '22 04:10

jobor


I faced the same linker error today, but it was due to a small slip:

I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.

So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file

like image 43
michael_s Avatar answered Oct 21 '22 04:10

michael_s


Citate from book "C++ GUI Programming with Qt 4" (page 19): For moc to work correctly, we must put the class definition in a header file, separate from the implementation file. So, you need write 2 files for your class: Multiplication_dialog.h and Multiplication_dialog.cpp! And you must recreate makefile!

like image 6
Grunelf Avatar answered Oct 21 '22 06:10

Grunelf


Well Today I faced probably the same problem. I know the thread is pretty old. But It may still help someone.

What happened in my case was moc was generating the moc_ .cpp files but VC doesn't know that It has to compile them too. So I manually added those moc generated files so that it compiles. and It worked.

like image 4
Neel Basu Avatar answered Oct 21 '22 04:10

Neel Basu