LNK2019 With Qt and Gmock [duplicate] - c++

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.

Related

Undefined reference error when linking dynamic and static libraries consequently

I have a QT project with 4 subprojects with the following names: exec (executable), sharedlib (dynamic library), sharedlib2 (other one dynamic library) and staticlib. All this is linked consequently as follows:
exec->sharedlib2->sharedlib->staticlib
In the static library is defined Staticlib class.
Now, when compiling all of this, I'm getting undefined reference error when trying to use that class (in exec project, main.cpp):
/home/user/temp/libproj/libproj/exec/main.cpp:9: error: undefined reference to `Staticlib::Staticlib()'
What am I doing wrong?
staticlib.h:
#ifndef STATICLIB_H
#define STATICLIB_H
#include <QDebug>
class Staticlib
{
public:
Staticlib();
void debugprint();
};
#endif // STATICLIB_H
sharedlib.pro:
QMAKE_LFLAGS += -Wl,--whole-archive,--allow-multiple-definition
LIBS += -L$$OUT_PWD/../staticlib/
LIBS += -lstaticlib
sharedlib2.pro:
LIBS += -L$$OUT_PWD/../sharedlib/
LIBS += -lsharedlib
exec.pro:
INCLUDEPATH += $$PWD/../staticlib
#doesnt work
LIBS += -L$$OUT_PWD/../sharedlib2
LIBS += -lsharedlib2
#works well
#//LIBS += -L$$OUT_PWD/../sharedlib/
#//LIBS += -lsharedlib
exec, main.cpp:
#include <QCoreApplication>
#include "staticlib.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Staticlib statlib;
statlib.debugprint();
return a.exec();
}
staticlib.pro
QT -= gui
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++17
SOURCES += \
staticlib.cpp
HEADERS += \
staticlib.h
staticlib.cpp
#include "staticlib.h"
#include <QDebug>
Staticlib::Staticlib()
{
}
void Staticlib::debugprint()
{
qDebug() << "Staticlib::debugprint()";
}

C++ Octave in QtCreator (windows)

I am trying to use Octave with Qt creator on Windows but I don't no why i can't succeed to link this library on this OS. I already tried on Ubuntu and everything works so i am a little bit lost...
I have octave3.6.4 (gcc 4.6.2) and i use the compiler gcc4.6.2 with Qt Creator.
my .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2015-01-26T11:35:00
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS += -L$$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/lib/octave/3.6.4/ -loctave -loctinterp -lcruft
INCLUDEPATH += $$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/include
DEPENDPATH += $$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/include
PRE_TARGETDEPS += $$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/lib/octave/3.6.4/libcruft.dll.a
PRE_TARGETDEPS += $$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/lib/octave/3.6.4/liboctave.dll.a
PRE_TARGETDEPS += $$PWD/../../../../Octave/Octave3.6.4_gcc4.6.2/lib/octave/3.6.4/liboctinterp.dll.a
my .h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <octave-3.6.4/octave/oct.h>
#include <octave-3.6.4/octave/octave.h>
#include <octave-3.6.4/octave/parse.h>
#include <octave-3.6.4/octave/toplev.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
And i have many link errors such as C2144 syntax error ...

Unable to locate the header files inside subdirectories while migrating to Qt5

I have been migrating from Qt 4.8.5 to Qt 5.3.0 recently, and these are the related information:
Windows 7 32 bit
Qt 3.1.1
MSVC 2010 compiler (Both my Qt versions are pre-compiled package of MSVC2010 edition)
debugger from Win7 SDK
Now I've been trapped by a problem that I keep getting compilation errors:
fatal error C1083: Cannot open include file
from those header files inside subdirectories, for example, #include "FooFolder/bar.h".
The compiler is unable to locate all this kind of headers and I am totally befuddled since:
The Intellisense works well.
If I change back to old kit Qt 4.8.5, it compiles fine
Both kits use the same MSVC compiler.
Here is my .pro file:
QT += core gui script
QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyApp
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
qcustomplot.cpp \
dialogs/filterdialog.cpp \
databox/databox.cpp \
databox/datom.cpp \
#... and more FooFolder/bar.cpp
HEADERS += mainwindow.h \
qcustomplot.h \
dialogs/filterdialog.h \
databox/databox.h \
databox/datom.h \
#... and more FooFolder/bar.h
RESOURCES += \
resources.qrc
win32: LIBS += -LC:/ADLINK/UDASK/lib/ -lusb-dask \
INCLUDEPATH += C:/ADLINK/UDASK/include \
DEPENDPATH += C:/ADLINK/UDASK/include \
include(qext/qextserialport.pri)
and here is one of the .h file that files to include other subdirectoried headers
#ifndef SETWINDOW_H
#define SETWINDOW_H
#include <QObject>
#include "databox/databox.h" // <---fatal error C1083: Cannot open include file
class SetWindow: public QObject
{
Q_OBJECT
public:
SetWindow();
public Q_SLOTS:
void setPointNum(int n);
void setStepSize(int s);
int getPointNum();
int getStepSize();
void requestSignal();
Q_SIGNALS:
void sendParameters(int p, int s);
private:
QString DynamicString;
DataBox *presentData;
int m_PointNum;
int m_StepSize;
};
#endif // SETWINDOW_H
and the header file failed to be included:
#ifndef DATABOX_H
#define DATABOX_H
#include <QVector>
#include <QFile>
#include <QMap>
#include <QString>
#include "datom.h"
class Measurement
{
public:
Measurement();
void setNumChan(int n);
void setADRange(QString &s);
void setSamplingRate(int n);
void setTimeBase(double d);
int NumChan;
QString ADRange;
int SamplingRate;
double TimeBase;
};
class DataBox
{
public:
DataBox();
void setCurrentFile(QString path);
void loadData();
void cleanAll();
QVector<double>* py_0;
QVector<double>* py_1;
QVector<double>* ptimeStamp;
QVector<double>* pi_M;
QVector<double>* py_M;
QVector<double>* py_W;
QMap<double, double> AI_0;
QMap<double, double> AI_1;
QMap<int, double> AI0;
QMap<int, double> AI1;
double timeBase;
Measurement parameters;
QVector<Datom> *dataPoints;
private:
QString currentFile;
};
#endif // DATABOX_H
These seem to be incorrect:
win32: LIBS += -LC:/ADLINK/UDASK/lib/ -lusb-dask \
INCLUDEPATH += C:/ADLINK/UDASK/include \
DEPENDPATH += C:/ADLINK/UDASK/include \
1) \ means it joins the next line which is not what you wish. It is not an issue for the lines with followed empty line, but it is not good for the INCLUDEPATH in here, and if you replace the empty lines with some containment, you could run into issues with the other lines, too.
2) You will also need to put $$PWD into the includepath to get the project root "registered" since your include should start from there, not the current working directory of the source since that is not the right route as you can assume.
Therefore, you would be writing something like this:
win32: LIBS += -LC:/ADLINK/UDASK/lib/ -lusb-dask
INCLUDEPATH += $$PWD C:/ADLINK/UDASK/include
DEPENDPATH += C:/ADLINK/UDASK/include

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