Qt Quick + CMake + custom QObject results in undefined reference to `vtable' - c++

I use Qt Quick to make small application to work with files.
Everything was working until one moment i executed in my build/ folder
rm -rf *
cmake ..
make
And then make halted with this error (lising is huge, i suppresed non-important part):
[100%] Linking CXX executable uint32_sort_gui
In function `LibController::~LibController()':
lib_controller.cpp:(.text+0x10): undefined reference to `vtable for LibController'
main.cpp.o: In function `int qmlRegisterType<LibController>(char const*, int, int, char const*)':
...
Here is my .hpp and .cpp files of the class:
lib_controller.hpp
#include <QObject>
#include <QString>
class LibController : public QObject{
Q_OBJECT
Q_PROPERTY(decltype(getProgress) progress READ getProgress NOTIFY ProgressChanged)
public:
explicit LibController(QObject *parent = 0);
~LibController();
double getProgress();
Q_INVOKABLE
void addFile(QString from_name, QString to_name);
Q_INVOKABLE
void sortFile();
signals:
void ProgressChanged();
private:
double current_progress;
FileManager* file_manager;
};
lib_controller.cpp
#include "lib_controller.hpp"
LibController::~LibController(){
delete file_manager;
}
double LibController::getProgress(){...}
void LibController::addFile(QString from_name, QString to_name){...}
void LibController::sortFile(){...}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include "lib_controller.hpp"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Registration of custom type
qmlRegisterType<LibController>("com.sort.controller", 0, 1, "LibController");
...
return app.exec();
}
And my CMakeLists.txt configuration.
I read another questions about this problem, but cleaning and rebuilding did not help (i even accidentally deleted whole project folder).
The problem persists, and i do not understand, how to fix it ...
UPD:
Here is full error message
UPD2:
After excluded LibController from the project and recompiling it, error is gone, but no window is showed to me. I can see from terminal that it is running, but no GUI popped up.
I guess problem is not in LibController, but somewhere else.

You need to add headers to the list of your source files for cmake to be able to run AUTOMOC on them.
This question has been already asked and answered here Changing location of header file causes missing vtable error when compiling with CMake

Since you call out the constructor in your header try adding this to the cpp file:
LibController::LibController(QObject *parent) : QObject(parent) {
}
Also, I have found that Rebuild All doesn't re-run qmake all the time. If you still see the issue run Build, Run qmake to make sure the header is getting processed.

According to comments in this question, you should
Rename your custom object header file to moc_*your header name*.hpp
Add path to this header to add_executable() function
Clear build/ directory and rerun CMake
Be somewhat happy in the end of the day
Hope this will help somebody, who tried to find last resort, like me.

Related

How to solve the "no Qt platform plugin could be initialized" problem?

I'm trying to run a simple template in release mode on Visual Studio with the Qt extension. So far I've always been running projects in debug mode (never had trouble). Recently, I started with a browser application using webengine its widgets, but it's very slow on debug mode, so. I wanted to make sure it's possible to run with higher performance (on release mode), before continuing.
I was surprised, because the application throws 4 error message pop-ups after each other after trying to run it:
The procedure entry point ?endl#QTextStreamFunctions##YAAEAVQTextStream##AEAV2##Z could not be located in the dynamic link library C:\Qt\5.14.1\msvc2017_64\bin\Qt5WebChannel.dll.
The procedure entry point ?argToQString#QQtPrivate...QString...QStringView... could not be located in the dynamic link library C:\Qt\5.14.1\msvc2017_64\bin\Qt5WebChannel.dll.
Two more similar ones for QDebug and QRhiDepthStencilClearValue.
So instead, I tried to compile a simple project (the direct QtWidgetsApplication template) and it gave me this:
This application failed to start because no Qt Platform plugin could be initialized. Reinstalling the application may fix this problem.
I've been looking for a solution for quite some time now, but I didn't find a clear answer.
My directory: C:\Qt\5.14.1\msvc2017_64
My template code:
#include "QtWidgetsApplication2.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication2 w;
w.show();
return a.exec();
}
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication2.h"
class QtWidgetsApplication2 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication2(QWidget *parent = Q_NULLPTR);
private:
Ui::QtWidgetsApplication2Class ui;
};
#include "QtWidgetsApplication2.h"
QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
I have no clue of how to fix this problem. Could you please help me out? Thanks in advance!
See if this helps.
Are you trying to start the program by double clicking the .exe ?
I tried to reproduce it:
build a simple project in release mode
run windeployqt ... and delete the generated folder ./platforms
double click on .exe to run it.
and prompts the error message you got.

Changing location of header file causes missing vtable error when compiling with CMake

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 (boilerplate)
#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 (boilerplate)
#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 (boilerplate)
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
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
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.
--
EDIT
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
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.
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
...
)
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.
Meta object compiler (moc) generates moc_*.cpp files from headers. So the build system needs to know which headers to feed it. Similar to C++ compiler that needs to know which files to process. So when working with CMake and Qt you need to consider the header files as source files. CMake is smart enough to not ask C++ compiler to compile them so there's no harm in adding them for other libraries too.
Unfortunately neither Qt-CMake nor CMake-Qt documentation state this explicitly but you should always add headers to the list of sources in add_executable/add_library for CMAKE_AUTOMOC to work.
Another important thing is the minimal required version of CMake. CMake 2.6 does not support Qt5. Neither does CMake 2.8. The official Qt documentation claims the minimal CMake version to be 3.1.0. However CMake 3.0 already has Qt5 support. Still those version are way too old, slow, and buggy so consider updating the requirement. I'd recommend 3.11 or latest stable.

Qt Creator projects breaking: "symbols not found for architecture x86_64"

I am programming with Qt Creator on a Mac (High Sierra 10.13.4). My projects seem to spontaneously break after a few days of work.
The linker error given is: "symbol(s) not found for architecture x86_64".
There is nothing wrong with the code - I open, run, close, and reopen projects and they are suddenly broken. I have also tested this with Qt's provided examples, to the same effect. I can copy-paste the code to a new project and compile it with no problem, but it will eventually do the same thing again.
I have attempted deleting the whole debug output folder of the project to give it a fresh start, but it did not make a difference. Same error.
Has anyone had this issue with QtCreator before? Is there a solution?
I have looked up a lot of very similar questions on here, but they all seem to be errors with the code. Just in case it's the same with me, this is all my code:
//main.cpp
#include "display.h"
#include "frame.h"
#include <QApplication>
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
Display w;
w.show();
return a.exec();
}
Display.h & Frame.h are auto-generated.
//display.cpp
#include "display.h"
#include "ui_display.h"
#include "frame.h"
Display::Display(QWidget *parent) : QMainWindow(parent), ui(new Ui::Display)
{
ui->setupUi(this);
QWidget * f = new Frame(this);
setCentralWidget(f);
}
Display::~Display()
{
delete ui;
}
Frame.cpp last:
#include "frame.h"
#include "ui_frame.h"
Frame::Frame(QWidget *parent) : QFrame(parent), ui(new Ui::Frame)
{
ui->setupUi(this);
}
Frame::~Frame()
{
delete ui;
}
It is not that the linker error is as given. There's way more to it, and you're not including the meat of the message that actually carries meaningful information allowing to debug this issue. Your problem is possibly with the project becoming internally binary-incompatible, and qmake build system not catching onto it. The issue has nothing to do with Qt Creator. The build is done by qmake and make. You'll see those problems if you build from the command line - which I highly advise you to do.
Assuming the sources are in /Users/mycaptain/src/myproject, and that you're using Qt from macports, proceed as follows in terminal:
$ mkdir ~/src/build-myproject
$ cd ~/src/build-myproject
$ /opt/local/libexec/qt5/bin/qmake ../myproject
$ make

Why is the Q_OBJECT macro causing issues (Qt)?

I am running QtCreator in OSX Lion, and anytime I create a class that requires the Q_OBJECT macro, I get an error when I try to build my application. The code for that class is below, as is the error I am recieving. Any clue what may be going on?
Note: I have already tried cleaning, running qmake and re-building to no avail.
#ifndef TASKLIST_H
#define TASKLIST_H
#include <QObject>
class TaskList : public QObject
{
Q_OBJECT
public:
explicit TaskList(QObject *parent = 0 );
public slots:
void addTask();
void displayTasks();
};
#endif // TASKLIST_H
And the error:
:-1: error: symbol(s) not found for architecture x86_64
:-1: error: collect2: ld returned 1 exit status
There still seems to be a bug in Qt Creator.
I have a large project with a number of classes all having Q_OBJECT and another number of classes not having Q_OBJECT. It works fine. However, if I add Q_OBJECT to one of the classes which didn't have it, I get this "collect2: ld returned 1 exit status" error when trying to build it.
Checking the build directory, I see that the moc file for this class is missing. Qt just does not create the moc files for it! However, if I remove the header and cpp files from the project and add them again, it works, the moc files are generated and the project is built successfully.
This problem seems to happen only if I have a class which did not have Q_OBJECT and it was built succesfully in the past. A fresh class with Q_OBJECT that was never compiled before adding "Q_OBJECT" always works fine.
So, if this problem happens and you are sure you included everything correctly (and commenting out Q_OBJECT lets the project to be built correctly), do the following:
remove the .h and .cpp files (where you just added the Q_OBJECT) from the project.
add them to the project again
clean project
build it again.
EDIT
In some cases running qmake (Build/Run qmake) followed by a Clean All is enough.
tasklist.h file
#ifndef TASKLIST_H
#define TASKLIST_H
#include <QObject>
class TaskList : public QObject
{
Q_OBJECT
public:
explicit TaskList(QObject *parent = 0 );
public slots:
void addTask(){};
void displayTasks(){};
};
#endif // TASKLIST_H
tasklist.cpp
#include "tasklist.h"
TaskList::TaskList(QObject *parent) :
QObject(parent)
{
}
main.cpp
#include <QtCore/QCoreApplication>
#include "tasklist.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TaskList t;
return a.exec();
}
works fine, but it should be in separate files

C++ Qt Multiple Definitions

I'm trying to create a simple GUI application (so far) in Qt with C++ using the MinGW compiler. However, the compiler is informing me that I have a multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' on line 4 of wiimotescouter.cpp. I am using a check to make sure the header isn't included multiple times, but apparently it's not working, and I'm not sure why.
Here's the header file:
#ifndef WIIMOTESCOUTER_H
#define WIIMOTESCOUTER_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class WiimoteScouter : public QWidget
{
Q_OBJECT
public:
WiimoteScouter(QWidget *parent = 0);
private:
QLineEdit *eventLine;
};
#endif // WIIMOTESCOUTER_H
And here's the cpp file:
#include <QtGui>
#include "wiimotescouter.h"
WiimoteScouter::WiimoteScouter(QWidget *parent) :
QWidget(parent)
{
QLabel *eventLabel = new QLabel(tr("Event:"));
eventLine = new QLineEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(eventLabel, 0, 0);
mainLayout->addWidget(eventLine, 0, 1);
setLayout(mainLayout);
setWindowTitle(tr("Wiimote Alliance Scouter"));
}
Lastly, the main.cpp:
#include <QtGui>
#include "wiimotescouter.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
WiimoteScouter wiimoteScouter;
wiimoteScouter.show();
return app.exec();
}
I've seen this happen before when a source file got duplicated in the project (.pro or .pri) file. Check all of the "SOURCES =" and "SOURCES +=" lines in your project file and make sure the cpp file is not in there more than once.
I don't use MinGW but this sounds like a linker error rather than a compiler error. If this is the case then you should check that the .CPP file is not added to the project twice. I also noticed that the extension is "php", this is very unusual as it should be "cpp".
Answer just for reference:
I was including
#include myclass.cpp
instead of
#include myclass.h
This can also happen if you have two .ui files with the same name in different folders. Their corresponding headers are built in the same directory, resulting in one being overwritten. At least that was my problem.
I got this error message when I had my slot declarations listed listed under the signals heading in the header file, rather than the slots one. Another thing for anyone experiencing this error message to check for.
Cut and paste solved the problem and a need to check next time I create Slots manually.
For me it was due to Qt's compilation model in Windows using MinGW.
My code compiled perfectly fine for Linux, but for Windows the linker errors were happening for following files:
Message.cpp
Util.cpp
At first, in the .pro file, I couldn't find any similar file names. Then observing keenly I figured out that, the external google protobuf library, which I was compiling along, had some library files inside its folder named as:
message.cc
util.cc
The cases and the extensions were different, but somehow it created mess in the Qt compilation. I just added an underscore to those library files and the things worked fine.
In my case this was caused by having the function declared globally in a header file.