'undefined reference to vtable' Error when Using Q_OBJECT
The error "'undefined reference to vtable'" occurs when the linker cannot find the virtual function table (vtable) for a class that inherits from QObject and declares the Q_OBJECT macro. This error can arise for several reasons.
Possible Causes:
-
Missing MOC Unit: The MOC (Meta-Object Compiler) generates a separate unit file that contains the implementation of the QObject-related functionality. Ensure that the unit generated by MOC is included in the linking process.
-
Class Declaration in Implementation File: Place the class declaration in a separate header file instead of the implementation file. The build system may not be scanning implementation files for MOC processing.
-
Missing qmake Invocation: Qmake generates the build rules necessary for MOC. If the class didn't initially belong to the Qt meta-object system, run qmake again to update its timestamp and trigger its execution.
Solution:
To resolve this issue, follow these steps:
- Move the class declaration to a separate header file.
- Run "Run qmake" from the project context menu in Qt Creator or manually update the project file's timestamp to force qmake invocation.
- Ensure that the generated MOC unit is included in the linking process.
These measures should eliminate the 'undefined reference to vtable' error and allow your code to compile successfully.
The above is the detailed content of Why am I getting an \'\'undefined reference to vtable\'\' error when using Q_OBJECT?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn