Click the blue word above to pay attention directly! It’s easy to read next time. If it helps you, please click on it or like it. Thank you~
Origin:
Recently, I want to write a demo to verify which thread each slot function runs in when the fifth parameter of QT connect is different. For simplicity, the. H and. CPP files are not created, but are written directly in the main function. As a result, the error of undefined reference to ` VTable for * * appears at runtime
The code is as follows:
class SendMsg : public QObject
{
Q_OBJECT
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SendMsg sendMsg;
return a.exec();
}
After Google, combined with their own verification, make a summary
1. One of the common problems of GCC
When building C++, the linker says my constructors, destructors or virtual tables are undefined, but I defined them
The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined, but does not require any diagnostic for violations of this rule [class.virtual]/8. Based on this assumption, GCC will only emit the implicitly defined constructors, the assignment operator, the destructor and the virtual table of a class in the translation unit that defines its first such non-inline method.
Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols. Unfortunately, in order to improve this error message, it might be necessary to change the linker, and this can’t always be done.
The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]
In short, the C + + standard stipulates that the virtual methods of all non pure virtual functions in a class must be defined, and the code that does not follow this rule is not prompted. So my program directly prompts the above error when linking
2. How does the problem appear in QT
① When the declaration of class sendmsg is put in a separate. H file, there is no such error
At that time, in order to simplify the test, I put it directly in main. CPP, so the above error occurred. If the declaration of the class sendmsg is placed separately in. H, there is no such error. The description in QT is as follows (using the meta object compiler (MOC)):
The meta object compiler MOC is a program dealing with the C + + extension of QT
The MOC tool reads the C + + header file. If one or more of the_ Object macro, which generates a C + + source file containing the meta object code of these classes. In addition, signal and slot mechanism, runtime type information and dynamic attribute system also need meta object code
The C + + source file generated by MOC must be compiled and linked with the implementation of this class
② Conclusion
It can be seen from the above that MOC reads the program from the original file, so the program placed in main.cpp cannot generate a new C + + file
3. Solutions
① If the declaration of sendmsg class is put in a separate. H file, the MOC file can be generated automatically
② Manually generate. MOC file
In the QT installation directory, find the moc.exe tool and manually generate the. MOC file
Manual generation of. MOC file
Generate. MOC file effect
Now the program looks like this:
class SendMsg : public QObject
{
Q_OBJECT
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SendMsg m_sendMsg;
return a.exec();
}
Some functions of MOC file:
Unimplemented virtual method declared in QObject macro:
Seeing this, you will know that because the above three virtual functions are not implemented, there will be the error of undefined reference to ` VTable for * *
4. Summary
Live seriously and try to understand
welcome to the official account:
this article is shared by WeChat official account – Pou Guangming (pou0230).