I have one of this unpopular LNK2019-errors in my project. Perhaps anyone can help me?
That's my header file:
#ifndef ELIST_H
#define ELIST_H
#include <QTableWidget>
class EList : public QTableWidget{
Q_OBJECT
public:
explicit EList(QWidget *parent = 0){}
~EList(){}
};
#endif // ELIST_H
and that's another cpp-file, where I want to create an object of EList in method do():
#include "elist.h"
#include "a.h"
void A::do(){
EList* el = new EList;
}
But then I get this LNK2019 error message:
a.obj:-1: Error:LNK2019: ""public: __cdecl EList::EList(class QWidget *)" (??0EList##QEAA#PEAVQWidget###Z)" in function ""public: __cdecl A::A(class QWidget *)" (??0A##QEAA#PEAVQWidget###Z)".
What did I wrong or what I have to do? I have insert all files/classes I could imagine but it did not show any effect.
EDIT: Here you can see my pro-File:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = A
TEMPLATE = app
SOURCES += main.cpp\
a.cpp \
elist.cpp
HEADERS += a.h \
elist.h
FORMS += a.ui
RESOURCES += \
ListViewIcon.qrc
And if I change Compiler (Qt 5.1 MSVC 2012) to Microsoft (Microsoft Visual C++ Compiler 11.0), all files will compiled...
With C++ code, you generally can't mix-and-match compilers. Whatever C++ compiler was used to build Qt is what you should be using to build your application. You've basically answered your own question :)
Related
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
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 months ago.
I've been banging my head against the wall on this one for the past few days. I am trying to incorporate googletest's gmock library into my Qt Autotest subdir project, but have been receiving the following linker error and I am unsure on how to resolve this. The main application compiles and runs perfectly fine.
tst_reptileprofile.obj : error LNK2019: unresolved external symbol "public: __thiscall DashboardWidget::DashboardWidget(void)" (??0DashboardWidget##QAE#XZ) referenced in function "private: void __thiscall TestReptileProfile::init(void)" (?init#TestReptileProfile##AAEXXZ)
Here is the test code:
#include <QtTest/QtTest>
#include <QCoreApplication>
#include <QObject>
#include "gmock/gmock.h"
#include "../Application/dashboardwidget.h"
class TestReptileProfile : public QObject
{
Q_OBJECT
public:
TestReptileProfile() {}
~TestReptileProfile() {}
private slots:
void initTestCase()
{
}
void cleanupTestCase()
{
}
void init()
{
dashboard_ = new DashboardWidget();
}
void cleanup()
{
delete dashboard_;
}
private:
DashboardWidget* dashboard_;
};
#include "tst_reptileprofile.moc"
QTEST_MAIN(TestReptileProfile)
DashboardWidget.h/.cpp
#pragma once
#include <QtWidgets/QWidget>
#include <QtWidgets/QAbstractButton>
namespace Ui {
class DashboardWidget;
}
class DashboardWidget : public QWidget
{
Q_OBJECT
public:
explicit DashboardWidget();
~DashboardWidget();
QAbstractButton* openProfileButton();
private:
Ui::DashboardWidget *ui;
QAbstractButton* openProfileButton_;
};
#include "dashboardwidget.h"
#include "ui_dashboardwidget.h"
DashboardWidget::DashboardWidget() :
ui(new Ui::DashboardWidget)
{
ui->setupUi(this);
openProfileButton_ = ui->openReptileProfilePageButton;
}
DashboardWidget::~DashboardWidget()
{
delete ui;
}
QAbstractButton* DashboardWidget::openProfileButton()
{
return openProfileButton_;
}
Subdirs Project .pro
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
Application \
AutoTests
AutoTests.depends = Application
Application.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Application
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainview.cpp \
. . .
mainpresenter.cpp \
dashboardwidget.cpp \
FORMS += \
mainview.ui \
i_mainpresenter.h \
dashboardwidget.ui \
HEADERS += \
mainview.h \
dashboardwidget.h \
AutoTests.pro
QT += testlib
QT += gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += qt warn_on depend_includepath testcase
TEMPLATE = app
SOURCES += \
tst_reptileprofile.cpp
unix|win32: LIBS += -L$$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release/ -lgmock
INCLUDEPATH += $$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release
DEPENDPATH += $$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release
INCLUDEPATH += D:\googletest\googlemock\include\
INCLUDEPATH += D:\googletest\googletest\include\
I also tried converting the project into a Visual Studio project which causes a compilation error instead.
Error C2059 syntax error: '.' AutoTests ProjectDir\tst_reptileprofile.cpp 63
Thanks
There are two points. The first thing: you are creating two executables (application and AutoTest). To test your production code you have to build it into a library.
TEMPLATE = lib
That means, one should create two executables, for testing and for the application. The executables need to be linked against the new library.
e.g:
LIBS += -L../lib/debug -llib
At the end you have three subdirectories and .pro-files.
The second thing: The symbols in the library has to be exported if the library is created and imported when you are linking against the library. This can be done with the preprocessor definition: TEST_COMMON_DLLSPEC.
You find the complete how to in the following link:
https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application
This tutorial isn't with googletest but you can use it similar.
I've got a header file with the following include:
#include <QtTest/QtTest>
I am trying to use the following line to generate a non-blocking wait in my main window:
QTest::qWait(1000 - ui->speedDial->value());
I receive the following error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl QTest::qSleep(int)" (__imp_?qSleep#QTest##YAXH#Z) referenced in function "void __cdecl QTest::qWait(int)" (?qWait#QTest##YAXH#Z)
Can anyone help me understand what I am doing wrong, or provide an alternative method? These lines are independent of other code.
It works fine for me. Check this example which is a modified version of the Qt Test official documentation example:
tutorial1.pro:
SOURCES = testqstring.cpp
CONFIG += qtestlib
# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial1
sources.files = $$SOURCES *.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial1
INSTALLS += target sources
symbian {
TARGET.UID3 = 0xA000C60B
include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
}
maemo5: include($$QT_SOURCE_TREE/examples/maemo5pkgrules.pri)
symbian: warning(This example might not fully work on Symbian platform)
maemo5: warning(This example might not fully work on Maemo platform)
simulator: warning(This example might not fully work on Simulator platform)
testqstring.cpp:
#include <QtTest/QtTest>
#include <QDebug>
class TestQString: public QObject
{
Q_OBJECT
private slots:
void toUpper();
};
void TestQString::toUpper()
{
QString str = "Hello";
QTest::qWait(2000);
QCOMPARE(str.toUpper(), QString("HELLO"));
}
QTEST_MAIN(TestQString)
#include "testqstring.moc"
I'm having troubles with using of external library, lets start from beginning.
I have some library trans2quik, wich consists of 3 files: trans2quik .dll/.lib/.h
So, I use Qt5.2, MSVC 2012 x64, win7. I create simple qt widget application, and link library, using "Add library" wizard, for generating LIBS, INCLUDEPATH, ect in my pro file. Then, when I call any function, I get unresolved extenal symbol error:
widget.obj:-1: error: LNK2019: unresolved external symbol __imp_TRANS2QUIK_CONNECT in function "public: __cdecl Widget::Widget(class QWidget *)" (??0Widget##QEAA#PEAVQWidget###Z)
The code follows:
PRO file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = bot_test
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
win32: LIBS += -L$$PWD/ -lTRANS2QUIK
INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
widget.h file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <Windows.h> //For LPSTR and DWORD
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
//Some vars for lib's function
LPSTR connectionParams;
LPSTR errorMsg;
DWORD errorMsgSize;
long *errorCode;
};
#endif // WIDGET_H
widget.cpp file:
#include "widget.h"
#include "trans2quik_api.h"
#pragma comment(lib, "TRANS2QUIK.lib")
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
TRANS2QUIK_CONNECT(connectionParams, errorCode, errorMsg, errorMsgSize);
}
So, the .lib and lib's .h files are in projects directory and LIB+= and INCLUDEPATH+= were generated by QtCreator, so I beliave it's not a problem. Hope for any halp, thank you in advance.
The problem is that you are trying to link againt a 32 bit trans2quik, whereas your application seems to be defined as 64 bit. Do not mix them. Either build 32 bit application, or use a 64 bit library.
I have two module in Qt
1. SapPackets : lib
2. SapApplication : app
pro file for both module
SapPackets.pro has Qt -= gui
SapApplication.pro has Qt += core gui xml
Target OS is Windows 7
there is a class SapEntity in SapPacket.lib
#ifndef SAPENTITYCLASS_HPP
#define SAPENTITYCLASS_HPP
#include <QString>
namespace Sap
{
namespace Entity
{
class SapEntityClass
{
protected:
unsigned short mush_Id; /* Entity Id */
QString msz_title; /* Entity Title */
public:
SapEntityClass(const unsigned short Id,const QString title);
unsigned short GetId() const;
QString GetTitle() const;
};
}
}
#endif
implementation file of SapEntity is
#include "SapEntityClass.hpp"
using namespace Sap::Entity;
SapEntityClass::SapEntityClass(const unsigned short Id,const QString title)
:mush_Id(Id),msz_title(title)
{}
inline
unsigned short SapEntityClass::GetId() const
{
return mush_Id;
}
inline
QString SapEntityClass::GetTitle() const
{
return msz_title;
}
SapApplication.pro has following line for adding SapPackets.lib
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../SapPackets_Build/release/ - lSapPackets
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../SapPackets_Build/debug/ -lSapPackets
else:unix: LIBS += -L$$PWD/../SapPackets_Build/ -lSapPackets
INCLUDEPATH += $$PWD/../SapPackets
DEPENDPATH += $$PWD/../SapPackets
Main function in SapApplication
#include <iostream>
#include "SapEntityClass.hpp"
using namespace Sap::Entity;
int main(int argc, char *argv[])
{
SapEntityClass obj(56,"Sample");
std::cerr<<obj.GetId();
return 0;
}
Problem:
I am getting following error on Compilationa
main.obj:-1: error: LNK2019: unresolved external symbol "public: unsigned short
__thiscall Sap::Entity::SapEntityClass::GetId(void)const " (?
GetId#SapEntityClass#Entity#Sap##QBEGXZ) referenced in function _main
Please help me to resolve this....
Edit:Why do you inline the method in the implementation file? Inlined functions must have visible definition together with their declaration. GCC reports same linker error for inlined method in implementation file, so I think that is your problem - remove inline in method definition or move it to the header.
Old answer:
Well you posted only header with Sap::Entity::SapEntityClass::GetId() method declaration. Where is definition? It seems it is not implemented or at least not linked to your application.
This can happen when the linker finds the function definition, but not the implementation, which is most likely compiled into the library.
Try adding
LIBS += -lSapPacket
to your SapApplication.pro file unless it's there already.
This tells your linker that there is a library called SapPacket.lib (on Windows; file extension would be different on other OSs), which contains implementations of some functions.