Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:31:08+00:00 2026-06-17T03:31:08+00:00

Possible Duplicate: Qt: Signals and slots Error: undefined reference to `vtable for Here we

  • 0

Possible Duplicate:
Qt: Signals and slots Error: undefined reference to `vtable for

Here we have test.cpp:

#include <QApplication>
#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Place alone in a new directory and:

$ qmake -project
$ qmake
$ make

It doesn’t work:

test.o: In function `MainWindow::~MainWindow()':
test.cpp:(.text._ZN10MainWindowD2Ev[_ZN10MainWindowD5Ev]+0x3): undefined reference to `vtable for MainWindow'
test.cpp:(.text._ZN10MainWindowD2Ev[_ZN10MainWindowD5Ev]+0xb): undefined reference to `vtable for MainWindow'
test.o: In function `main':
test.cpp:(.text.startup+0x48): undefined reference to `vtable for MainWindow'
test.cpp:(.text.startup+0x51): undefined reference to `vtable for MainWindow'
test.o: In function `MainWindow::~MainWindow()':
test.cpp:(.text._ZN10MainWindowD0Ev[_ZN10MainWindowD0Ev]+0x7): undefined reference to `vtable for MainWindow'
test.o:test.cpp:(.text._ZN10MainWindowD0Ev[_ZN10MainWindowD0Ev]+0xf): more undefined references to `vtable for MainWindow' follow
collect2: error: ld returned 1 exit status
make: *** [tmp] Error 1

In general such errors are either because moc is not invoked or because of unimplemented virtual methods.

moc should be invoked automatically by qmake, and afaik QMainWindow doesn’t have any pure virtual methods – so what is the problem here?

When I remove Q_OBJECT it works. Why is this? What is going on?

I see that Q_OBJECT tells moc to generate the signals/slots meta-data for that object, why can’t it do that in this case?

Update:

Solution is to add #include "test.moc" just below class:

#include <QApplication>
#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
};

#include "test.moc"  // <----------- HERE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:31:09+00:00Added an answer on June 17, 2026 at 3:31 am

    It is because you declared the class inside the .cpp file. Put it into a separate .h header.

    By default, the MOC is not invoked for source files, just for headers (edit: according to @Kamil, MOC is ran against .cpp source files, except that the generated .moc files should be #include-d manually in the .cpp files — see my edit below).

    Then, add the respective header file to the HEADERS variable inside the .pro file.

    Of course, #include the same header into test.cpp.

    Also, see this answer here:

    https://stackoverflow.com/a/5854884/1284631

    edit:

    According to this thread: http://www.qtcentre.org/threads/28580-Why-does-qmake-moc-only-process-header-files

    to force MOC running for .cpp files, you should add:

    #include "test.moc"

    right below the definition of your class. And re-run qmake. However, I did not test this.

    edit2: See this reference for a discussion on the line on what OP is looking for:

    http://harmattan-dev.nokia.com/docs/library/html/qt4/moc.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Undefined reference to vtable. Trying to compile a Qt project here is
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: C declaration from standard signal Library Here is a reference to the
Possible Duplicate: while( !feof( file ) ) is always wrong I have a strange
Possible Duplicate: Does every Core Data Relationship have to have an Inverse? I have
Possible Duplicate: How to get useful error messages in PHP? Im currently learning php
Possible Duplicate: Speed up the loop operation in R I have a few questions
Possible Duplicate: Variable doesn’t get returned from AJAX function I have this function: function
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.