为什么在连接这个Qt 5.0应用程序时,会出现“未定义的vtable引用…”错误?

时间:2023-01-13 21:47:00

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

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

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:

main.cpp:

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

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

    MainWindow win;
    win.show();

    return app.exec();
}

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

该项目还包含一个.ui文件,由Qt Creator 2.6.1 (MainWindow.ui)创建。

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

当我尝试在Linux上使用g++构建文件时,我收到了以下错误:

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

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?

是什么导致了这种错误呢?我最近从qmake切换到CMake,我不记得在编译一个简单的示例时遇到过这么多麻烦。我做错了什么?


Edit: 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

2 个解决方案

#1


19  

Turns out I forgot:

原来我忘记了:

set(CMAKE_AUTOMOC ON)

At the top of the CMakeLists.txt file.

在CMakeLists的顶端。txt文件。

#2


2  

I struggled with this for a long time using all the hints published here:

我花了很长一段时间来使用这里公布的所有提示:

http://doc.qt.io/qt-5/cmake-manual.html

http://doc.qt.io/qt-5/cmake-manual.html

And here

这里

https://www.kdab.com/using-cmake-with-qt-5/

https://www.kdab.com/using-cmake-with-qt-5/

What I had to do was specify things in the right order. For example, the following is the top of my CMakeLists.txt. Note that the two CMAKE set directives come before add_executable. Once I did this, I was able to link without undefined symbols and vtable references. I just thought I'd post this for the benefit of others.

我所要做的就是以正确的顺序指定事情。例如,下面是CMakeLists.txt的顶部。注意,两个CMAKE set指令在add_可执行之前。一旦我这样做了,我就可以链接没有定义的符号和vtable引用。我只是想为了其他人的利益而发布这篇文章。

cmake_minimum_required (VERSION 2.8)

set (CMAKE_AUTOMOC ON)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(FHSpectrumSensor wideband_seq_spectrum_sensor.cpp sensor.cpp   gui.cpp ${gui_SRC})

Later in the CMakeLists.txt I have the following:

CMakeLists之后。txt我有以下内容:

 find_package(Qt5Widgets REQUIRED)
 find_package(Qt5Charts REQUIRED)
 find_package(Qt5Core REQUIRED)

 qt5_use_modules(FHSpectrumSensor Widgets Charts)
 qt5_wrap_cpp(gui_SRC gui.h gui.cpp)

That did the trick.

起了作用。

#1


19  

Turns out I forgot:

原来我忘记了:

set(CMAKE_AUTOMOC ON)

At the top of the CMakeLists.txt file.

在CMakeLists的顶端。txt文件。

#2


2  

I struggled with this for a long time using all the hints published here:

我花了很长一段时间来使用这里公布的所有提示:

http://doc.qt.io/qt-5/cmake-manual.html

http://doc.qt.io/qt-5/cmake-manual.html

And here

这里

https://www.kdab.com/using-cmake-with-qt-5/

https://www.kdab.com/using-cmake-with-qt-5/

What I had to do was specify things in the right order. For example, the following is the top of my CMakeLists.txt. Note that the two CMAKE set directives come before add_executable. Once I did this, I was able to link without undefined symbols and vtable references. I just thought I'd post this for the benefit of others.

我所要做的就是以正确的顺序指定事情。例如,下面是CMakeLists.txt的顶部。注意,两个CMAKE set指令在add_可执行之前。一旦我这样做了,我就可以链接没有定义的符号和vtable引用。我只是想为了其他人的利益而发布这篇文章。

cmake_minimum_required (VERSION 2.8)

set (CMAKE_AUTOMOC ON)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(FHSpectrumSensor wideband_seq_spectrum_sensor.cpp sensor.cpp   gui.cpp ${gui_SRC})

Later in the CMakeLists.txt I have the following:

CMakeLists之后。txt我有以下内容:

 find_package(Qt5Widgets REQUIRED)
 find_package(Qt5Charts REQUIRED)
 find_package(Qt5Core REQUIRED)

 qt5_use_modules(FHSpectrumSensor Widgets Charts)
 qt5_wrap_cpp(gui_SRC gui.h gui.cpp)

That did the trick.

起了作用。