QT Classic error message undefined reference to ' vtable for classname

Source: Internet
Author: User

original link: QT Classic error message undefined reference to ' vtable for classname

This error message is too common, the use of QT more than two months of friends can basically solve their own, because too classic, can give reference to the novice.

The error message is generally similar to the following: undefined reference to ' vtable for classname Mywidget '
Occurs after the make command is executed.

The reason for the error is that in order to be able to use signals and slots when defining the class, a q_object is appended to the class definition. Because Q_object is a macro, expanding in different classes is different code, such as the one generated in MYWIDGETH.H

Public:template inline void Qt_check_for_qobject_macro (const T &_q_argument) const {int i = Qyouforgottheq_object_ma Cro (this, &_q_argument); i = i; } static const Qmetaobject Staticmetaobject; Virtual const Qmetaobject *metaobject () const; virtual void *qt_metacast (const char *); Static inline QString tr (const char *s, const char *c = 0) {return staticmetaobject.tr (S, c);} Static inline QString TrU Tf8 (const char *s, const char *c = 0) {return Staticmetaobject.trutf8 (S, c);} Static inline QString tr (const char *s, CO NST char *c, int n) {return staticmetaobject.tr (s, c, n);} static inline QString TrUtf8 (const char *s, const char *c, in T n) {return Staticmetaobject.trutf8 (s, c, n);} virtual int qt_metacall (qmetaobject::call, int, void * *); Private

You can see that the above code declares 3 3 virtual functions that inherit from the parent class. Originally the implementation of these three functions should be done by Moc_mywidget.cpp.
The current error is due to the fact that makefile did not add Moc_mywidget.cpp to the compilation.

The reason behind Makefile is qmake, because the QT compilation system converts. Pro files to makefile files by Qmake. When Qmake scans the. h code, it discovers a code with Q_object, and writes a dependency on the Moc_xxx.cpp code generated by the MOC to makefile. If Q_object is not found in the scan, no additional moc_xxx.cpp such files will be generated.

The reason for the first scene is when the qmake is being executed. h code does not o_object such a code. While executing make, there is already q_object in H, and the solution is to re-execute the qmake and then do it.

——

Shiroki: In addition, I would add that there is a possibility that the person writing the code will write all the code in the. cpp file. Be aware that the MOC tool is read only. h files, if you put the Q_object macro in the CPP, the MOC is not visible ... So you have to write the code or to abide by the declaration. H implementation of the rules of the CPP is better.

Q_object undefined reference to question

Excerpt from: http://waiter94.blog.163.com/blog/static/5277376920103423144138/

If your class is defined and stated in the same CPP file as: Test.cpp, and slot/signal is used in class. At the bottom of this CPP file, add

# include "file name. MoC" will help you to get rid of compilation problems

Qt appears "undefined reference to ' vtable for" cause summary

Due to the mechanism of QT itself, when we use QT to make certain software programs, we encounter a variety of such problems, and many are difficult, or simply difficult to find the reason, even if the problem solved, if someone asked you why, you can only answer-do not know;

The problem I am listing here today is also the problem that is always encountered when I write QT program, the problem is common, but the answer is not the only one, the explanation of this problem is many, often only suitable for a certain situation, because this error is too general, he is-"undefined reference to" Vtable for "Maybe you look familiar, seems to have encountered this problem in the C + + program, you are right, sometimes this error, not only the cause of QT, but also the reasons for your C + + program;

The following collection of online some of the phenomenon, corresponding solutions, reasons, basically I have verified, included in this, to do the backup;

One:

The precompiled compiler opens the macro Q_object, declaring several member functions that are handled by MOC (implement). If you get a compile error like "undefined reference to vtable for Lcdnumber", if you get compiler errors along the lines of "undefined Referen Ce to vtable for Lcdnumber "), you may have forgotten to execute the MOC, or forgot to add the MOC output to the link command.

If the q_object is added to a class, the link prompts: undefined reference to vtable for "xxx::xxx". Delete Him there is no problem.

Resolution: Try (1): Delete any obj documents and UIC documents and recompile. Still failed.

To Trolltech Mail lists find the reason: Because Qmake generated makefile, this class header document does not have q_object, so in the corresponding makefile there is no MOC xxx.h command, The link eventually fails. Rerun Qmake, problem solved.

When looking for a workaround, it comes with a little bit of a discovery:

Qmake does not process the q_object in the. cpp document, so if there is a file in the. cpp document, it will also produce undefined reference to vtable for "xxx::xxx". In this case, you need to generate the corresponding MOC document with MoC Xxxx.cpp, and then include it in the. cpp to solve the problem.

It is possible to find that the problem arises because there is no MOC to generate the corresponding MOC document, and then the connection becomes problematic.

I looked for a lot of source code, such as the problem, is not looking for pro error, and later thought of Qt MoC we have make do

QT make compilation is based on Makefile, and makefile is from pro documentation. This is the idea of finding the pro documentation error.

From:http://www.cublog.cn/u/16292/showart_136087.html

Two:

Undefined reference to vtable for "xxx::xxx"

Today, I met this problem, finally I Google to:

http://www.cublog.cn/opera/showart.php?blogid=8650&id=49526

Cause: Qmake does not process the q_object in the. cpp document, so if there is a file in the. cpp document, it will also produce undefined reference to vtable for "xxx::xxx". In this case, you need to generate the corresponding MOC document with MoC Xxxx.cpp, and then include it in the. cpp to solve the problem.

Other:

1. Problem: The function in Qglviewer does not work properly. FIX: Look at its source code, found that because the installation of libqglviewer from the source, compiled with Qt 3, and my program is compiled with QT4. You must re-compile with Qt4. However, change the Qtdir After the environment variable is QT4, the qmake generates makefile when the error occurs. Further discovery, because although the Qtdir for QT4, header documents and library documents will use QT4, but moc,uic, etc. are used QT3 version, and then change the PATH environment variable , everything OK.

2. Question: If you join Q_object in a class, you are prompted by Link: undefined reference to vtable for "xxx::xxx". Delete Him there is no problem. Resolution: Try (1): Remove any obj documents and UIC documents , recompile. Still failed. To Trolltech Mail lists find the reason: Because Qmake generated makefile, this class header document does not have q_object, so in the corresponding makefile there is no MOC xxx.h command, The link eventually fails. Rerun Qmake, problem solved. When looking for a workaround, it comes with a point: Qmake does not process the q_object in the. cpp document, so if there is a file in the. cpp document, it will also produce undefined reference to vtable for "xxx::xxx". In this case, you need to generate the corresponding MOC document with MoC Xxxx.cpp, and then include it in the. cpp to solve the problem.

3. See QT's Reference discovery: Be able to connect (Pobja, SIGNAL (Somesignala ()), Pobjb,signal (Somesignalb ())), so Pobja sends the Somesingala Causes the POBJB to emit somesignalb, thus forming a signal relay.

QT Classic error message undefined reference to ' vtable for classname

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.