2012-12-24 33 views
8

我有一個使用的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 

回答

18

原來我忘了:

set(CMAKE_AUTOMOC ON) 

在的CMakeLists.txt文件的頂部。

+2

我確實設置了(CMAKE_AUTOMOC ON),但我仍然看到與您所看到的相同的錯誤。另外,我的鏈接命令與您的相同。你能幫忙嗎? –

+1

另外,moc文件「nova_app_automoc。這個文件是自動生成的,不要編輯*/ enum some_compilers {need_more_than_nothing}; –

+6

我有幾乎完全相同的問題,但我已經設置了(CMAKE_AUTOMOC ON)已經在我的CMakeLists.txt中,但我仍然遇到問題! –

2

我這個掙扎了很長一段時間用在這裏發佈的所有提示:

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

這裏

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

我有什麼做的是在正確指定的東西訂購。例如,以下是我的CMakeLists.txt的頂部。請注意,兩個CMAKE set指令在add_executable之前。一旦我這樣做了,我就可以鏈接到未定義的符號和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}) 

中的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) 

該訣竅。

相關問題