Cannot build project from QT wiki: Unresolved external symbol - c++

Error when attempting to build code I copy-pasted directly from the QT for beginners wiki page.
error
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Window::Window(class QWidget *)" (??0Window##QEAA#PEAVQWidget###Z) referenced in function main
debug\testempty.exe:-1: error: LNK1120: 1 unresolved externals
testempty.pro
TEMPLATE = app
TARGET = testempty
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
main.cpp \
window.cpp
HEADERS += \
window.h
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class Window : public QWidget
{
public:
explicit Window(QWidget *parent = nullptr);
private:
QPushButton *m_button;
};
#endif // WINDOW_H
window.cpp
#include "window.h"
#include <QPushButton>
Window::Window(QWidget *parent) : QWidget(parent)
{
setFixedSize(100, 50);
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
}
main.cpp
#include <QApplication>
#include "window.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
As for things I have already tried, I was instructed to build->clean all and try building again, but that made no difference.

Solution From a similar thread.
Right click on project > Clean
Right click on project > Run qmake
Right click on project > Build
Run
Why it works
The reason this worked is because Run qmake updates your Makefile. For some reason qt is not automatically updating paths when you make changes to your project such as adding or removing files. Running qmake forces qt to update the paths for your project which enables it to find the mainwindow.obj file. You probably could just run qmake and your problem would be solved.

Related

QArrayData error, linking Qt libraries with qmake

I am trying follow the instructions published here:
https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/
but in a PC running Windows 10, to build a Qt application createdwith the Atom editor. I have this 3 files in my project right now:
qt_main.cpp
#include <QtWidgets>
#include "mainwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();
return a.exec();
}
mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private:
QPushButton* button_;
QTextBrowser* textBrowser_;
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include <QtWidgets>
#include "mainwidget.h"
MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
button_ = new QPushButton(tr("Push Me!"));
textBrowser_ = new QTextBrowser();
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(button_,0,0);
mainLayout->addWidget(textBrowser_,1,0);
setLayout(mainLayout);
setWindowTitle(tr("Connecting buttons to processes.."));
}
MainWidget::~MainWidget()
{
delete button_;
delete textBrowser_;
}
I execute this commands, in sequence:
qmake -project
add `QT += widgets` to the qt_main.pro file generated
qmake qt_main.pro
make
After that, the EXE file is generated without error, but when I try run it, I got the error:
"The procedure entry point _ZN10QArrayData10deallocateEPS_jj could not be located in the dynamic link library etc."
I try follow the suggested on the comments for the question:
QArrayData error, linking Qt libraries with CMake
and check my PATH. right now, the only directory containing qt DLLs is the one:
C:\Qt\Qt5.14.0\5.14.0\mingw73_64\bin
(this directory was createwd by the Qt offline installer).
Anyone can give a hint of how to solve this issue?

Why does compile Qt BluetoothDiscoveryAgent results in a Linker Error

I try to compile a little Qt project, but i get a linker error.
I want to make a small project which read Bluetooth Low Energy (BLE) Gatt characteristics. I have an embedded Board where the BLE works.
When I start the compiler i get theses warnings / Errors:
qtmain.lib(qtmain_winrt.obj):-1: Warnung: LNK4099: PDB "qtmain.pdb" wurde nicht mit "qtmain.lib(qtmain_winrt.obj)" oder an "C:\Users\Kennis\Documents\build-Bachlerorarbeit-Qt_5_11_2_for_UWP_64bit_MSVC_2017-Profile\release\qtmain.pdb" gefunden; Objekt wird verknüpft, als ob keine Debuginformationen vorhanden wären.
widget.obj:-1: error LNK2019: unresolved external symbol ""__declspec(dllimport) public: __cdecl QBluetoothDeviceDiscoveryAgent::QBluetoothDeviceDiscoveryAgent(class QObject *)" (__imp_??0QBluetoothDeviceDiscoveryAgent##QEAA#PEAVQObject###Z)" referenced in function""public: __cdecl Widget::Widget(class QWidget *)" (??0Widget##QEAA#PEAVQWidget###Z)".
widget.obj:-1: error LNK2019: unresolved external symbol ""__declspec(dllimport) public: virtual __cdecl QBluetoothDeviceDiscoveryAgent::~QBluetoothDeviceDiscoveryAgent(void)" (__imp_??1QBluetoothDeviceDiscoveryAgent##UEAA#XZ)" referenced in function ""public: virtual void * __cdecl QBluetoothDeviceDiscoveryAgent::`scalar deleting destructor'(unsigned int)" (??_GQBluetoothDeviceDiscoveryAgent##UEAAPEAXI#Z)".
widget.obj:-1: error LNK2001: unresolved external symbol""public: virtual struct QMetaObject const * __cdecl QBluetoothDeviceDiscoveryAgent::metaObject(void)const " (?metaObject#QBluetoothDeviceDiscoveryAgent##UEBAPEBUQMetaObject##XZ)".
widget.obj:-1: error LNK2001: unresolved external symbol ""public: virtual struct QMetaObject const * __cdecl QBluetoothDeviceDiscoveryAgent::metaObject(void)const " (?metaObject#QBluetoothDeviceDiscoveryAgent##UEBAPEBUQMetaObject##XZ)".
widget.obj:-1: error LNK2001: unresolved external symbol ""public: virtual void * __cdecl QBluetoothDeviceDiscoveryAgent::qt_metacast(char const *)" (?qt_metacast#QBluetoothDeviceDiscoveryAgent##UEAAPEAXPEBD#Z)".
:
i work on Windows 10 with Qt5.11.2 for UWP 64bit (MSVC 2017).
without the line of the init of the Discovery Agent i´m able to compile the project.
I have started Qmake and rebuild multiple times.
main.cpp :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp :
#include "widget.h"
#include "ui_widget.h"
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothDeviceInfo>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
}
Widget::~Widget()
{
delete ui;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothDeviceInfo>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
bachelorarbeit.pro:
#-------------------------------------------------
#-------------------------------------------------
#
# Project created by QtCreator 2019-02-02T21:22:40
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Bachlerorarbeit
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact
warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain
version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the
APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Setting compiler right for meson

I am trying to build a basic Qt Application with the Meson Build System on my Mac (using macOS Sierra), following the tutorial on http://mesonbuild.com/samples.html.
My meson.build file looks like this:
project('qt5 demo', 'cpp',
default_options : ['cpp_std=c++11'])
qt5_dep = dependency('qt5', modules : ['Core', 'Gui', 'Widgets'])
# Import the extension module that knows how
# to invoke Qt tools.
qt5 = import('qt5')
prep = qt5.preprocess(moc_headers : 'mainWindow.h',
ui_files : 'mainWindow.ui')
executable('qt5app',
sources : ['main.cpp', 'mainWindow.cpp', prep],
dependencies : qt5_dep,
cpp_args : '-std=c++11')
I have a small test program consisting of only four files: main.cpp, mainwindow.h, mainwindow.cpp, and mainwindow.ui.
The source code is as follows.
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
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;
}
The program compiles and executes as expected when I use qmake as a build system using the following qmake-file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtDesigner
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
When I execute
meson build
it works fine, except for the warning:
WARNING: rcc dependencies will not work reliably until this upstream issue is fixed:
https://bugreports.qt.io/browse/QTBUG-45460
It also compiles without errors, when I switch to the build directory and call
ninja
but when I execute the program I get the following error:
dyld: Library not loaded: #rpath/QtCore.framework/Versions/5/QtCore
Referenced from:
/Users/<myname>/code/C++/QtDesignerCode/build/./qt5app
Reason: image not found
Abort trap: 6
It seems like the linker cannot find the libraries? Which is weird, as in Qt Creator (using qmake) the source code compiles fine.
Thanks in advance for any help.
Do the following in the build dir
mesonconf -Dcpp_std=c++11
or, set the default language version in your meson.build file

Qt standard project and examples stopped building

Qt project suddenly stopped building. So as new just created empty projects based on QDialog or examples. Cleaning, rebuilding not helping.
Log of key errors:
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: unknown type name 'QDialog'
Q_DISABLE_COPY(QDialog)
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: C++ requires a type specifier for all declarations
Q_DISABLE_COPY(QDialog)
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: unknown type name 'QDialog'
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/dialog.h:10:
error: unknown class name 'QDialog'; did you mean 'Dialog'?
class Dialog : public QDialog
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/dialog.h:10:
error: base class has incomplete type
class Dialog : public QDialog
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/main.cpp:8:
error: no member named 'show' in 'Dialog'
w.show();
7 errors generated.
make: *** [main.o] Error 1
18:46:36: Process «/usr/bin/make» exit with code 2.
Seems like something happened with qdialog.h or something. By the way project on QMainWindow works fine. I didn't do anything. Tried to reopen QtCreator, reboot computer.
I use Mac OS X and Qt 5.2.1
Any example provided by QtCreator or empty project based on QDialog. For example:
dialog.cpp:
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
}
Dialog::~Dialog()
{
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
};
#endif // DIALOG_H
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
NewDiaproj.pro
#-------------------------------------------------
#
# Project created by QtCreator 2014-04-20T19:31:45
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NewDiaproj
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
Based on the fact that your files work fine for me on Archlinux with Qt 5.2, I think your QDialog file in the Qt installation got corrupted by some accidental or "vis major" action.
Reinstall it cleanly and then it should just work.

Unresolved external symbols - Qt creator

I must be missing a basic concept with headers and includes because when I attempt to call even the simplest of a function from a separate source file I get an error:
main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl
buildDeck(int,int)" (?buildDeck##YAXHH#Z) referenced in function _main
deck.h
#ifndef DECK_H
#define DECK_H
#include <QString>
void buildDeck(int deckSize, int jokers);
struct card
{
QString suit;
QString color;
int rank;
};
#endif // DECK_H
deck.cpp
#include"mainwindow.h"
#include "deck.h"
void buildDeck(int deckSize, int jokers)
{
int blackRed = deckSize-=jokers;
}
main.cpp
#include "mainwindow.h"
#include "deck.h"
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
buildDeck(10,20);
return a.exec();
}
And this gives me an error. However, If I move the function definition from deck.cpp to the bottom of main.cpp, then the application will build.
All of the files are included in the same project, and stored in the same directory.
Other files:
.pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = carddeck
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
deck.cpp
HEADERS += mainwindow.h \
deck.h
FORMS += mainwindow.ui
not sure if you need it, but here's mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void runButtonClicked();
private:
Ui::MainWindow *ui;
QPushButton *runButton;
QTextEdit * runText;
QCheckBox * betHearts;
QCheckBox * betDiamonds ;
QCheckBox * betClubs;
QCheckBox * betSpades ;
QCheckBox * betFlush ;
QCheckBox * betAllRed;
QCheckBox * betAllBlack ;
};
#endif // MAINWINDOW_H
It looks like the Makefile was not regenerated when you edited the .pro file.
Run qmake and try again.
You could also check if the deck.cpp is compiled or not; is there a deck.o in the build directory ?
yes, some time Makefile file is not updated while you change .pro file. So you have to run qmake.
Follow this steps:
Right click on project Clean / Build menu -> cleanAll
Right click on project Run qmake / Build menu -> runQmake
Right click on project Build / Build menu -> build
Pro Advice:
I don't know but sometime Qt is not updating Makefile. so i recomanded to all whenever you add/removing any resource in project or if any changes occur in your .pro file, just Run qmake and build your project(Running qmake do manually to update the path of project, which help to find the mainwindow.obj file).