我有一個使用的CMake 2.8.9一個相對簡單的Qt 5.0的項目:連接這個Qt 5.0應用程序時,爲什麼會出現「undefined reference to vtable ...」錯誤?
的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:
#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:
#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();
}
該項目還包括使用Qt Creator 2.6.1(MainWindow.ui)創建的.ui
文件。
當我嘗試建立文件與g++
在Linux上,我收到以下錯誤:
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,我從來沒有想過要編寫一個簡單的例子遇到這麼多麻煩。我究竟做錯了什麼?
編輯:這裏是正在使用的一切鏈接命令:
/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
我確實設置了(CMAKE_AUTOMOC ON),但我仍然看到與您所看到的相同的錯誤。另外,我的鏈接命令與您的相同。你能幫忙嗎? –
另外,moc文件「nova_app_automoc。這個文件是自動生成的,不要編輯*/ enum some_compilers {need_more_than_nothing}; –
我有幾乎完全相同的問題,但我已經設置了(CMAKE_AUTOMOC ON)已經在我的CMakeLists.txt中,但我仍然遇到問題! –