更改头文件的位置会导致在使用CMake编译时丢失vtable错误 [英] Changing location of header file causes missing vtable error when compiling with CMake

查看:108
本文介绍了更改头文件的位置会导致在使用CMake编译时丢失vtable错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个大型C ++项目,我需要从qmake过渡到CMake,但是在研究玩具示例时,遇到了一些我不了解的行为。该示例代码仅包含一个头文件,当该头文件移入子目录时,MainWindow类会丢失vtable错误。

I need to transition from qmake to CMake for a large C++ project, but while working through a toy example I encountered some behavior that I don't understand. The example code features a single header file, and when that header file is moved into a subdirectory, I get a missing vtable error for the MainWindow class.

CMakeLists .txt

cmake_minimum_required(VERSION 2.6)
project(HelloCMake)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Widgets CONFIG REQUIRED)

include_directories("include")
set(INCLUDES include/mainwindow.h)
set(SOURCES
    main.cpp
    mainwindow.cpp
    mainwindow.ui
)

add_executable(hello-cmake ${SOURCES})   # error
# add_executable(hello-cmake ${SOURCES} ${INCLUDES})   # no error
target_link_libraries(hello-cmake Qt5::Widgets)

include / mainwindow.h (样板)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp (样板)

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

main.cpp (样板)

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

这是我运行 make时看到的内容(首先运行 cmake之后。):

Here's what I see when I run make (after first running cmake .):

[ 20%] Automatic MOC and UIC for target hello-cmake
[ 20%] Built target hello-cmake_autogen
[ 40%] Linking CXX executable hello-cmake
Undefined symbols for architecture x86_64:
  "vtable for MainWindow", referenced from:
      MainWindow::MainWindow(QWidget*) in mainwindow.cpp.o
      MainWindow::~MainWindow() in mainwindow.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hello-cmake] Error 1
make[1]: *** [CMakeFiles/hello-cmake.dir/all] Error 2
make: *** [all] Error 2

如果我通过交换第二个 add_executable CMakeLists.txt 中的第一个命令,错误消失了。我也可以通过将标头与.cpp文件一起移动到基本目录中来使错误消失。但是,我想知道这里的实际情况。我了解将标头文件包含在目标中的一般价值,但是为什么不这样做会生成缺少的vtable错误?除非我有严重的误解,否则 mainwindow.h 的所有内容都应该可用,而 mainwindow.cpp 的编译是凭借 #include 的标题,无论标题是否是 add_executable 语句的一部分。

If I add the header to the target by swapping the second add_executable command for the first one in CMakeLists.txt, the error goes away. I can also make the error go away by moving the header into the base directory with the .cpp files. However, I'd like to know what's actually going on here. I understand the general value of including the header files in the target, but why is a missing vtable error generated when I don't do this? Unless I grossly misunderstand, all the contents of mainwindow.h should be available while mainwindow.cpp is being compiled by virtue of the #include, whether or not the header is part of the add_executable statement.

-
编辑

以下是 ui_mainwindow的内容。 h ,如果它们之间存在某种相关性。

Here are the contents of ui_mainwindow.h, in case they're somehow relevant.

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 5.10.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralWidget;
    QPushButton *pushButton;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QStringLiteral("MainWindow"));
        MainWindow->resize(393, 307);
        centralWidget = new QWidget(MainWindow);
        centralWidget->setObjectName(QStringLiteral("centralWidget"));
        pushButton = new QPushButton(centralWidget);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(10, 10, 113, 32));
        MainWindow->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(MainWindow);
        menuBar->setObjectName(QStringLiteral("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 393, 22));
        MainWindow->setMenuBar(menuBar);
        mainToolBar = new QToolBar(MainWindow);
        mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(MainWindow);
        statusBar->setObjectName(QStringLiteral("statusBar"));
        MainWindow->setStatusBar(statusBar);

        retranslateUi(MainWindow);
        QObject::connect(pushButton, SIGNAL(released()), pushButton, SLOT(hide()));

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", nullptr));
        pushButton->setText(QApplication::translate("MainWindow", "Do not press", nullptr));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H


推荐答案

我能够将我的真实项目迁移到CMake上:只是一个小小的麻烦:链接器对我的QObject中的任何信号功能都不满意派生的类。 (对于不熟悉Qt的人,元对象编译器moc魔术地将标头文件中标记为信号的某些空函数转换为C ++编译器可以使用的实函数。)我的结论是,这两个问题都是由CMake看不到引起的QObject派生类的头文件,因此尽管设置为 CMAKE_AUTOMOC ,也不会将它们发送到moc。

I was able to migrate my real project to CMake with exactly one hiccup: the linker wasn't happy with any of the signal functions in my QObject-derived classes. (For those not familiar with Qt, a meta-object compiler, moc, magically converts certain empty functions marked as signals in a header file into real functions that the C++ compiler can use.) My conclusion is that both problems resulted from CMake not seeing the header files for a QObject-derived class, and so they were not sent to the moc in spite of the CMAKE_AUTOMOC setting.

结果是,如果moc(或uic或rcc)需要编译文件,则CMake必须在建立任何相关目标之前知道它的存在。我用于项目的粗略解决方案是在目录中包含所有头文件的 grep Q_OBJECT ,然后将此列表复制/粘贴到长的集中 CMakeLists.txt中的命令

The upshot is, if moc (or uic or rcc) needs to compile a file, then CMake has to know it exists before building any dependent targets. The crude solution I used for my project was to grep Q_OBJECT in the directory with all my header files, and copy/paste this list into a long set command in CMakeLists.txt

set(MOC_SOURCES
    include/applewidget.h
    include/borangewidget.h
    ...
)

在我的 add_executable 行中添加 $ {MOC_SOURCES} 。可能有一个更复杂的解决方案,其中涉及分别构建这些对象,但是我对CMake的使用尚未达到那种复杂的水平。

and then add ${MOC_SOURCES} to my add_executable line. There may be a more sophisticated solution that involves building these objects separately, but my use of CMake has not yet reached that level of sophistication.

这篇关于更改头文件的位置会导致在使用CMake编译时丢失vtable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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