为什么我得到“未定义的vtable ...的引用”链接这个Qt 5.0应用程序时出现错误? [英] Why am I getting "undefined reference to vtable..." errors when linking this Qt 5.0 application?

查看:1631
本文介绍了为什么我得到“未定义的vtable ...的引用”链接这个Qt 5.0应用程序时出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对简单的Qt 5.0项目,使用CMake 2.8.9:

I've got a relatively simple Qt 5.0 project that uses CMake 2.8.9:

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
project(hello-world)

find_package(Qt5Widgets REQUIRED)
qt5_wrap_ui(hello-world_UI MainWindow.ui)

add_executable(hello-world MainWindow.cpp main.cpp ${hello-world_UI})
qt5_use_modules(hello-world Widgets)

MainWindow.h:

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:

        MainWindow();
        virtual ~MainWindow();

    private:

        Ui::MainWindow * const ui;
};

#endif // CMAINWINDOW_H

MainWindow.cpp:

MainWindow.cpp:

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow()
    : ui(new Ui::MainWindow)
{
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp:

#include <QApplication>
#include "MainWindow.h"

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    MainWindow win;
    win.show();

    return app.exec();
}

该项目还包括 .ui

The project also includes a .ui file created with Qt Creator 2.6.1 (MainWindow.ui).

当我尝试使用 g ++

When I attempt to build the file with g++ on Linux, I receive the following errors:


CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::MainWindow()':
MainWindow.cpp:(.text+0x3b): undefined reference to `vtable for MainWindow'
MainWindow.cpp:(.text+0x4d): undefined reference to `vtable for MainWindow'
CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::~MainWindow()':
MainWindow.cpp:(.text+0xaf): undefined reference to `vtable for MainWindow'
MainWindow.cpp:(.text+0xc1): undefined reference to `vtable for MainWindow'
collect2: error: ld returned 1 exit status

可能是什么原因导致此类错误?我最近从 qmake 切换到CMake,我从来不记得遇到这个麻烦,得到一个简单的例子来编译。我做错了什么?

What could possibly be causing this sort of error? I recently switched to CMake from qmake and I never remember running into this much trouble getting a trivial example to compile. What am I doing wrong?

编辑:这里是用来链接一切的命令:

here is the command being used to link everything:


/usr/bin/c++ CMakeFiles/hello-world.dir/MainWindow.cpp.o
CMakeFiles/hello-world.dir/main.cpp.o -o hello-world -rdynamic
/usr/local/Qt-5.0.0/lib/libQt5Widgets.so.5.0.0
/usr/local/Qt-5.0.0/lib/libQt5Gui.so.5.0.0
/usr/local/Qt-5.0.0/lib/libQt5Core.so.5.0.0 
-Wl,-rpath,/usr/local/Qt-5.0.0/lib


推荐答案

结果我忘了:

set(CMAKE_AUTOMOC ON)

在CMakeLists.txt文件的顶部。

At the top of the CMakeLists.txt file.

这篇关于为什么我得到“未定义的vtable ...的引用”链接这个Qt 5.0应用程序时出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆