error: LNK2019: unresolved external symbol in Qt - c++

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.

Related

Unresolved External Symbol while trying to use bit7z library C++ / Qt

I've been trying to install and properly link bit7z into my C++ code as I have to do a task for my internship which concludes in automatically zipping a certain directory and sending the zip-file out as an email. Right now the email is not interesting to me as I can't even get the base program. I just keep getting the Linker Error 2019 and I don't know what to do anymore. I'll provide as much info as I can.
I use Visual Studio 2019.
My .pro file:
TEMPLATE = app
TARGET = aixLogger
DESTDIR = ./Debug
CONFIG += debug console
DEPENDPATH += .
MOC_DIR += .
OBJECTS_DIR += debug
UI_DIR += GeneratedFiles
RCC_DIR += GeneratedFiles
LIBS += -D:/local/aretz/Programmierung/git-workplace/aixLogger/Dependencies/bit7z/lib -lbit7z
INCLUDEPATH += D:/local/aretz/Programmierung/git-workplace/aixLogger/Dependencies/bit7z/include
include(aixLogger.pri)
My .h
#pragma once
#include <qwidget.h>
#include <qobject.h>
#include <bit7z.hpp>
class AIXLogger : public QWidget
{
Q_OBJECT
public slots:
public:
void CompressDir();
void Execute();
};
My .cpp
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <filesystem>
#include <bit7z.hpp>
#include "main.h"
#include "bitcompressor.hpp"
namespace fs = std::filesystem;
using namespace bit7z;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string path = "C:/Users/aretz/Downloads/test";
for (const auto& entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
//return a.exec();
}
void AIXLogger::CompressDir() {
try {
Bit7zLibrary lib{ L"C:/Program Files/7-Zip/7z.dll" };
//BitCompressor compressor{ lib, BitFormat::Zip };
std::vector< std::wstring > files = { L"aretz/downloads/test/test1.txt", L"aretz/downloads/test/test1.txt" };
//Zip Archiv erstellen
//compressor.compress(files, L"output_archive.zip");
//Directory zippen
//compressor.compressDirectory(L"dir/path/", L"dir_archive.zip");
}
catch (const BitException& ex) {
//irgendwas mit &ex machen
}
}
void AIXLogger::Execute() {
CompressDir();
}
I'm also adding pictures of the properties I changed.
Additional DependenciesAdditional Library DirectoriesAdditional Include Directories
EDIT: Here is the actual Error I'm getting with just the line "Bit7zLibrary lib {L"C:/Program Files/7-Zip/7z.dll" };:
Error LNK2019 unresolved external symbol "public: __thiscall bit7z::Bit7zLibrary::Bit7zLibrary(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" (??0Bit7zLibrary#bit7z##QAE#ABV?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##std###Z) referenced in function "public: void __thiscall AIXLogger::CompressDir(void)" (?CompressDir#AIXLogger##QAEXXZ) aixLogger D:\local\aretz\Programmierung\git-workplace\aixLogger\main.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: virtual __thiscall bit7z::Bit7zLibrary::~Bit7zLibrary(void)" (??1Bit7zLibrary#bit7z##UAE#XZ) referenced in function "public: void __thiscall AIXLogger::CompressDir(void)" (?CompressDir#AIXLogger##QAEXXZ) aixLogger D:\local\aretz\Programmierung\git-workplace\aixLogger\main.obj 1
The problem you're having is that the linker cannot find the bit7z static library.
By default, the bit7z library is built to bit7z\bin\$(PlatformShortName)\, where $(PlatformShortName) is either x86 or x64 according to your target architecture.
However, you specified a different library directory (bit7z\lib\), which is wrong (unless you changed the directory where you output the built library).
You can fix it by changing the path to $(SolutionDir)Dependencies\bit7z\bin\$(PlatformShortName)\.
Also, please note that, on x86, the default library name is just bit7z.lib, while on x64 is bit7z64.lib.

Qt 5.2 cannot get qWait function to work

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"

Qt, Library function calling gives unresolved external symbol error

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.

C++/Qt: Just a LNK2019 and I don't know why

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 :)

Qt: Linker error with QVERIFY (Qt Unit Tests)

Basically I am following the basic example here. My .pro file contains QT += core network qtestlib. [Solved] testlib instead of typo qtestlib
When I include QVERIFY, It get the following linker error:
testwaypointlist.obj:-1: error: LNK2019: unresolved external symbol "bool __cdecl
QTest::qVerify(bool,char const *,char const *,char const *,int)"
(?qVerify#QTest##YA_N_NPBD11H#Z) referenced in function "private: void __thiscall
TestWaypointList::fillWaypoints(void)" (?fillWaypoints#TestWaypointList##AAEXXZ)
What files do I miss to link? Without QVERIFY the linker error disappears.
Header file:
#include <QObject>
#include <QtTest/QtTest>
#include "waypointlist.h"
//
// Testing the waypoint list
//
class TestWaypointList : public QObject
{
Q_OBJECT
private:
WaypointList _waypointList;
public:
explicit TestWaypointList(QObject *parent = 0);
private slots:
void fillWaypoints();
};
cpp:
//
// Fill the waypoint list
//
void TestWaypointList::fillWaypoints()
{
_waypointList = WaypointList();
.....
for (int i=0; i < TESTWPLISTCOUNT; i++) {
.....
TestWaypointList::_waypointList.updateOrAppend(id, timeframe);
}
QVERIFY(TestWaypointList::_waypointList.count() == 1); // causing the linker error
}
In your .pro file, you need to change QT += qtestlib to QT += testlib (note the absence of a "q").
Of note, you used to be able to do this: CONFIG += qtestlib, but according to the comment on this page, this is no longer the recommended way of linking to the test library.