使用 Qt 对 vtable 的未定义引用
Posted
技术标签:
【中文标题】使用 Qt 对 vtable 的未定义引用【英文标题】:Undefined reference to vtable using Qt 【发布时间】:2021-11-30 22:18:32 【问题描述】:我是 Qt 新手,我正在尝试做一个简单的 VoIP 应用程序。 我使用 CMake 和 conan 来获取所有包并构建应用程序。如果我将所有与 Qt 相关的类头文件和源文件放在同一个目录中,我可以毫无问题地编译,但是当我将头文件移动到另一个目录时,我发现链接器有问题。我不知道为什么,因为在 CMakeLists.txt 中我声明了包含目录(并且对于与 Qt 无关的其他类也可以正常工作),我觉得这与 autoMOC 有关。
项目结构
|--include
| |
| |--client
| | |
| | |--views
| | | |
| | | |--loginwindow.ui
| | | |--ui_loginwindow.h
| | | |--mainwindow.ui
| | | |--ui_mainwindow.h
| | |--Client.h
| | |--loginWindow.h
| | |--mainWindow.h
| | |--Profile.h
| |--common
| |
| |--server
| |
|--src
| |
| |--client
| | |
| | |--Controller
| | |--Model
| | |--View
| | | |
| | | |--loginWindow.cpp
| | | |--mainWindow.cpp
| |--common
| |
| |--server
| |
我省略了一些目录内容,因为那里没有发现问题。
CMakeLists.txt
if ($CMAKE_SOURCE_DIR STREQUAL $CMAKE_BINARY_DIR)
message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the source code and call cmake from there")
endif ()
list(APPEND CMAKE_MODULE_PATH $CMAKE_BINARY_DIR)
list(APPEND CMAKE_PREFIX_PATH $CMAKE_BINARY_DIR)
project(babel)
cmake_minimum_required(VERSION 3.17.4)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions("-fPIC")
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS "$CMAKE_EXE_LINKER_FLAGS")
else()
set(STANDARD_UNIX_CXX_FLAGS "-Wall -g3 -Wextra -Wfatal-errors")
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $STANDARD_UNIX_CXX_FLAGS")
endif()
if (EXISTS $CMAKE_BINARY_DIR/conanbuildinfo.cmake)
include($CMAKE_BINARY_DIR/conanbuildinfo.cmake)
else()
message(FATAL_ERROR "No conanbuildinfo.cmake file found")
endif()
conan_basic_setup(KEEP_RPATHS)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5OpenGL CONFIG REQUIRED)
find_package(Qt5Widgets CONFIG REQUIRED)
find_package(Qt5Core CONFIG REQUIRED)
find_package(Qt5Gui CONFIG REQUIRED)
find_package(Qt5Network CONFIG REQUIRED)
find_package(portaudio REQUIRED)
find_package(Opus REQUIRED)
find_package(asio REQUIRED)
file(
GLOB_RECURSE
SOURCES_CLIENT
$PROJECT_SOURCE_DIR/src/*.cpp
$PROJECT_SOURCE_DIR/src/client/*.cpp
$PROJECT_SOURCE_DIR/include/client/*.hpp
$PROJECT_SOURCE_DIR/include/client/*.ui
$PROJECT_SOURCE_DIR/resources.qrc
)
file(
GLOB_RECURSE
SOURCES_SERVER
$PROJECT_SOURCE_DIR/src/server/*.cpp
)
file(
GLOB_RECURSE
SOURCES_COMMON
$PROJECT_SOURCE_DIR/src/common/*.cpp
$PROJECT_SOURCE_DIR/include/common/*.hpp
)
add_executable(babel_client $SOURCES_CLIENT $SOURCES_COMMON)
install(TARGETS babel_client DESTINATION $PROJECT_SOURCE_DIR/bin)
target_link_libraries(
babel_client
Qt5::Widgets
Qt5::Network
Qt5::OpenGL
Qt5::Core
Qt5::Gui
opus
portaudio
)
target_include_directories(
babel_client PRIVATE
$CONAN_INCLUDE_LIBS
$PROJECT_SOURCE_DIR/include/client
$PROJECT_SOURCE_DIR/include/client/views
$PROJECT_SOURCE_DIR/include/common
)
loginWindow.h
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QMainWindow>
#include "mainWindow.h"
#include "views/ui_loginwindow.h"
#include <QKeyEvent>
QT_BEGIN_NAMESPACE
namespace Ui class LoginWindow;
QT_END_NAMESPACE
class LoginWindow : public QMainWindow
Q_OBJECT
public:
LoginWindow(QWidget *parent = nullptr);
~LoginWindow();
private slots:
void on_loginButton_clicked();
protected:
void keyPressEvent(QKeyEvent *key);
private:
Ui::LoginWindow *ui;
MainWindow *mainWindow;
;
#endif // LOGINWINDOW_H
loginWindow.cpp
#include "loginWindow.h"
#include <QPixmap>
LoginWindow::LoginWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::LoginWindow)
int picWidth;
int picHeight;
ui->setupUi(this);
QPixmap pix(":/resources/babel.png");
picWidth = ui->babelPicture->width();
picHeight = ui->babelPicture->height();
ui->babelPicture->setPixmap(pix.scaled(picWidth, picHeight, Qt::KeepAspectRatio));
ui->statusbar->addPermanentWidget(ui->statusText);
ui->loginButton->setDefault(true);
LoginWindow::~LoginWindow()
delete (ui);
void LoginWindow::keyPressEvent(QKeyEvent *key)
if (key->key() == Qt::Key_Return)
on_loginButton_clicked();
void LoginWindow::on_loginButton_clicked()
QString user = ui->userField->text();
QString pass = ui->passwordField->text();
if (user == "test" && pass == "test")
ui->statusText->setText("Correct login");
this->hide();
mainWindow = new MainWindow(this);
mainWindow->show();
else
ui->statusText->setText("Inorrect login");
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "views/ui_mainwindow.h"
namespace Ui
class MainWindow;
class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
;
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
MainWindow::~MainWindow()
delete ui;
链接器输出
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/loginWindow.cpp.o: in function `LoginWindow::~LoginWindow()':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/loginWindow.cpp:20: undefined reference to `vtable for LoginWindow'
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/loginWindow.cpp.o: in function `non-virtual thunk to LoginWindow::~LoginWindow()':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/loginWindow.cpp:23: undefined reference to `vtable for LoginWindow'
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/loginWindow.cpp.o: in function `LoginWindow::LoginWindow(QWidget*)':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/loginWindow.cpp:6: undefined reference to `vtable for LoginWindow'
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/mainWindow.cpp.o: in function `MainWindow::MainWindow(QWidget*)':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/mainWindow.cpp:5: undefined reference to `vtable for MainWindow'
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/mainWindow.cpp.o: in function `MainWindow::~MainWindow()':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/mainWindow.cpp:10: undefined reference to `vtable for MainWindow'
/usr/bin/ld: CMakeFiles/babel_client.dir/src/client/View/mainWindow.cpp.o: in function `non-virtual thunk to MainWindow::~MainWindow()':
/home/bltksk/epitech/tek3/CPP/babel/src/client/View/mainWindow.cpp:13: undefined reference to `vtable for MainWindow'
正如我在介绍中所说,如果我将 xxWindow.h 和 xxWindow.cpp 放在同一个文件夹中,问题就会消失,但是当我将它们放在包含目录中时,它不会链接。我不是 cmake 专家,但对我来说似乎是正确的。关于解决方案的任何提示?
【问题讨论】:
使用 GLOB 构造源文件列表是一种反模式。典型的失败模式是 CMake 看不到新文件。 【参考方案1】:好的,我成功解决了问题。
为了使 AUTOUIC 正确运行,我们必须将所有包含 ui_*.h 的头文件添加到 sources 目标中,而不仅仅是包含路径。这样 automoc 将正确处理标头并正确链接。
在我的情况下是这样的:
file(
GLOB_RECURSE
SOURCES_CLIENT
$PROJECT_SOURCE_DIR/src/*.cpp
$PROJECT_SOURCE_DIR/src/client/*.cpp
$PROJECT_SOURCE_DIR/include/client/*.h
$PROJECT_SOURCE_DIR/include/client/resources/resources.qrc
)
另外,记得设置CMAKE_AUTOUIC_SEARCH_PATHS(cmake版本>3.9)为.ui文件所在的路径,否则会出现AutoUic错误。
我希望这对将来的人有所帮助。
【讨论】:
一个人不应该在没有CONFIGURE_DEPENDS
的情况下使用GLOB_RECURSE
,但最好不要使用它。以上是关于使用 Qt 对 vtable 的未定义引用的主要内容,如果未能解决你的问题,请参考以下文章
Qt C++ Q_OBJECT 错误未定义对 vtable 的引用
QT中的Q_OBJECT(未定义对vtable xxx的引用)[重复]