22

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?

smallB
  • 16,662
  • 33
  • 107
  • 151

9 Answers9

18

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.

David Stolarsky
  • 439
  • 5
  • 13
jobor
  • 189
  • 3
  • #jobor I'm using qt addin (that's what you refer as vs addin I hope) – smallB Jul 10 '11 at 18:22
  • Check if moc_multiplication_dialog.cpp included in your vsproj. if it is missed, @jobor is right) – Raiv Jul 11 '11 at 12:10
  • 1
    Yes I mean the Qt add-in. The problem is that your class Multiplication_dialog has a Q_OBJECT macro but moc_multiplication_dialog.cpp (I can only guess the file name) is not created or compiled. – jobor Jul 12 '11 at 05:55
17

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

michael_s
  • 2,515
  • 18
  • 24
6

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!

Grunelf
  • 61
  • 1
  • 1
4

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.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • This is applicable not only to .cpp BUT TO .H as well. We defined a pure abstract as our defined interface, but it had a non-virtual signal. This means the .h has to be in the source to be compiled in your CMakeLists.txt file so it will be moc'd. – PfunnyGuy Feb 20 '18 at 02:17
3

I'm currently working with VS 2013 with QT 5.4 add-in. Building projects with the add-in makes it easier as the moc'ing is automatically handled. To prevent linker error problems with the Qt metaObject issues:

  1. Comment out all instances of Q_OBJECT in all header files that contain it in the class declarations.

  2. Build the solution

  3. Uncomment all instances of the Q_OBJECT in the header files.

  4. Rebuild the solution. This is where the all the all the header and cpp files are re-compiled with the moc'ing automatically done. The linker errors are in turn, taken care of.

  • that temporarily worked for me. Thanks... I will see if it holds up as the application continues to build itself – Matthew Jan 05 '17 at 16:15
1

Same problem occurred when I used Qt Addin for VS, I saw moc files are not included into project after building. I included them into project these errors are gone.

Akın Yılmaz
  • 300
  • 5
  • 18
  • Why would that be necessary? – László Papp Dec 25 '14 at 12:10
  • Because of some wrong configuration of VS preventing usage of moc files. Sometimes even if it generates moc files correctly but because they are not included, it behave like it does not exist – Akın Yılmaz Dec 25 '14 at 12:14
  • Include it in the source file at the end? – László Papp Dec 25 '14 at 12:15
  • There is a need for something if it is not handled by VS. That is also a good practice when using cmake. If you do it IDE agnostic, it will work with any buildsystem. – László Papp Dec 25 '14 at 12:16
  • Some of the Examples shipped with QtCreator include .moc files in the project. So the conclusion has to be that, while including header files seems more correct, including moc files is totally valid. – Mark Ch Dec 09 '16 at 07:34
0

Try to relaunch the .pro of your project.

Patapoom
  • 794
  • 1
  • 7
  • 16
0

Some fairly long-winded explanations above.
Using Qt Creator, on the menu bar:
Build>Run qmake
Build>Build All

Richard Jessop
  • 897
  • 1
  • 12
  • 19
0

I've just had a case "How can I create a method with a variable number of arguments in a class with Q_OBJECT? [duplicate]" where I had linker errors when I had a Q_OBJECT in a class definition of a header; but the bodies of some offending functions were in a .cpp file. I moved the bodies to the class definition in the header file, and the problem vanished. No need to do anything special with the moc_ files. Another function's body in the same class did just fine in the .cpp file. This was Qt 5.15.2.

CodeLurker
  • 1,248
  • 13
  • 22