I'm trying to add unit tests to a project of mine for the first time.
I can run mock tests alright (without using my project's classes) and run the application alright. But if I instantiate objects from the project I get an unresolved external symbol of the QMetaObject. If I recall correctly, this means the moc of the object isn't being included on the project.
How do I fix this? I have the same issue using googletests. The guide also doesn't help on this. I've tried installing the qt unit testing plugin, same result.
I've uploaded a mock project that follows the same structure that I'm using in the aforementioned project, fetch it here: https://github.com/quimnuss/QtUnitTestingTest
I'm using a static build of qt on windows, but I guess that's irrellevant. Using QtCreator as IDE and NMAke build.
I've also tried add the HelloWorld.lib, but taking a look at the Makefile.release it isn't used.
Somebody has an idea of what I'm doing wrong?
Here's the unit testing .pro:
QT += widgets network testlib
TARGET = tst_someunittesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += $$PWD/../HelloWorld
include($$PWD/../HelloWorld/helloworldCommon.pri)
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
message("Searching libs here $$LIBS")
SOURCES += tst_someunittesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
The first error's complete message:
tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject#HelloWorld##UEBAPEBUQMetaObject##XZ)
When you use the following flags:
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
You must have the compiled dynamic or static library. Therefore you must create a project generating a library. In the next part I show you how to create a dynamic library.
HelloWorldLib.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:37:49
#
#-------------------------------------------------
QT -= gui
TARGET = HelloWorldLib
TEMPLATE = lib
DEFINES += HELLOWORLDLIB_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
INCLUDEPATH += $$PWD/include
SOURCES += src/helloworldlib.cpp
HEADERS += include/helloworldlib.h\
include/helloworldlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
DESTDIR = $$PWD/lib
include/helloworldlib.h
#ifndef HELLOWORLDLIB_H
#define HELLOWORLDLIB_H
#include "helloworldlib_global.h"
#include <QDebug>
class HELLOWORLDLIBSHARED_EXPORT HelloWorldLib: public QObject
{
Q_OBJECT
public:
HelloWorldLib(){
}
static bool returnTrue()
{
return true;
}
public slots:
void someSlot()
{
qDebug() << "test";
}
};
#endif // HELLOWORLDLIB_H
include/helloworldlib_global.h
#ifndef HELLOWORLDLIB_GLOBAL_H
#define HELLOWORLDLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(HELLOWORLDLIB_LIBRARY)
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // HELLOWORLDLIB_GLOBAL_H
src/helloworldlib.cpp
#include "helloworldlib.h"
Here I show the test project.
HelloWorldTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:42:42
#
#-------------------------------------------------
QT += testlib
QT -= gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tst_helloworldtesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
SOURCES += tst_helloworldtesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/release/ -lHelloWorldLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/debug/ -lHelloWorldLib
else:unix: LIBS += -L$$PWD/../HelloWorldLib/lib/ -lHelloWorldLib
INCLUDEPATH += $$PWD/../HelloWorldLib/include
DEPENDPATH += $$PWD/../HelloWorldLib/include
tst_helloworldtesttest.cpp
#include <QString>
#include <QtTest>
#include <helloworldlib.h>
#include <QDebug>
class HelloWorldTestTest : public QObject
{
Q_OBJECT
public:
HelloWorldTestTest();
private Q_SLOTS:
void testCase1_data();
void testCase1();
};
HelloWorldTestTest::HelloWorldTestTest()
{
}
void HelloWorldTestTest::testCase1_data()
{
QTest::addColumn<QString>("data");
QTest::newRow("0") << QString();
}
void HelloWorldTestTest::testCase1()
{
QFETCH(QString, data);
QVERIFY2(true, "Failure");
HelloWorldLib hw;
QVERIFY(hw.returnTrue());
}
QTEST_APPLESS_MAIN(HelloWorldTestTest)
#include "tst_helloworldtesttest.moc"
Output:
********* Start testing of HelloWorldTestTest *********
Config: Using QtTest library 5.7.1, Qt 5.7.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 6.2.1 20160830)
PASS : HelloWorldTestTest::initTestCase()
PASS : HelloWorldTestTest::testCase1(0)
PASS : HelloWorldTestTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of HelloWorldTestTest *********
In the following link is the complete project: https://github.com/eyllanesc/stackoverflow/tree/master/QtUnitTestingTest
Related
For my project i need a GUI and library called "STM32CubeProgrammer CLI".
They include some example C++ projects with VS and QT (both only command line, no GUI).
I'm able to compile and work with them.
In the next step I created a QT Widgets Application and add the STM library but the program crash instantly.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
$$PWD/../../../include/CubeProgrammer_API.h \
$$PWD/../../../include/DeviceDataStructure.h \
FORMS += \
mainwindow.ui
#comment out that line and the programm works
win32: LIBS += -L$$PWD/../../../lib/ -lCubeProgrammer_API
INCLUDEPATH += $$PWD/../../../include
DEPENDPATH += $$PWD/../../../include
This is the main.cpp I don't edit any "window" or other files, I don't even use a function of the dll.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The linker find the dlls, but the program only crashes if I'm using a GUI, a simple cmd program dont crash. Just for fun I added Qlibrary, but the program crashes befor.
I'm using qmake, QT 5.14.2 MinGW-32bit and QT Creator 4.11.1.
I want to build a modern Windows application using WinRT (Windows 10). I use Qt 5.13.1 UWP kits for Visual Studio 2017. When building a project, it displays a lot of compilation errors:
Code:
testproject.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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
SOURCES += \
main.cpp \
testproject.cpp
HEADERS += \
testproject.h
FORMS += \
testproject.ui
LIBS += -lwindowsapp
# Default rules for deployment.
#qnx: target.path = /tmp/$${TARGET}/bin
#else: unix:!android: target.path = /opt/$${TARGET}/bin
#!isEmpty(target.path): INSTALLS += target
testproject.h
#ifndef TESTPROJECT_H
#define TESTPROJECT_H
#include <QDialog>
#include <QDebug>
#include "winrt/Windows.System.Diagnostics.h"
using namespace winrt;
using namespace Windows::System::Diagnostics;
QT_BEGIN_NAMESPACE
namespace Ui { class TestProject; }
QT_END_NAMESPACE
class TestProject : public QDialog
{
Q_OBJECT
public:
TestProject(QWidget *parent = nullptr);
~TestProject();
private:
Ui::TestProject *ui;
};
#endif // TESTPROJECT_H
testproject.cpp
#include "testproject.h"
#include "ui_testproject.h"
TestProject::TestProject(QWidget *parent)
: QDialog(parent)
, ui(new Ui::TestProject)
{
ui->setupUi(this);
init_apartment();
auto info = SystemDiagnosticInfo::GetForCurrentSystem();
auto memory = info.MemoryUsage().GetReport().TotalPhysicalSizeInBytes();
qDebug() << memory;
}
TestProject::~TestProject()
{
delete ui;
}
Without WinRT code it compiles successfully. Any ideas how to configure WinRT using Qt? What libraries are required for Qt to run WinRT code? Thanks in advance.
Try either adding #undef X64 before #include "winrt/Windows.System.Diagnostics.h" or go to your project settings, select "Configuration Properties" -> "C/C++" -> "Preprocessor", in the dropdown next to "Preprocessor Definitions" select "Edit", remove the line X64. I've no idea what this define is for, but it conflicts with the ProcessorArchitecture::X64 enum in Windows.System.0.h.
PS. This answer is just a reproduced guesswork as I can't make much sense of the errors on the screenshot. Usually copy-pasting the text from the Output panel is more informative than the Error List.
I try to use the QtNetwork library and added all dependencies for it in the .pro file. But when I compile my code Qt Creator fails in building the project and claims
C1083: Include "QTcpSocket": No such file or directory - telnet.h:4
I thought adding network to the .pro file would be enough?
networkmonitor.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-07-24T13:18:19
#
#-------------------------------------------------
QT += core gui network charts
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = networkmonitor
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
SOURCES += \
main.cpp \
mainwindow.cpp \
telnet.cpp
HEADERS += \
mainwindow.h \
telnet.h
FORMS += \
mainwindow.ui
telnet.h
#ifndef TELNET_H
#define TELNET_H
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>
class Telnet
{
public:
Telnet();
void sendValues(QString _ip, int _port, QString _message);
private:
QTcpSocket *socket;
};
#endif // TELNET_H
Normally this means you just forgot to re-run qmake
I created this simply application in Qt4 and would like to test it on RH Linux distro. The distro has both QtCore "Qt4" and Qt3 installed. I cannot add or delete any of these Qt versions, but would like to work with what's available.
I have an error compiling my windows based Qt program "Qt4" in Linux .
First question :
- How do I compile my compile in Linux without QT creator "only Qt libraries" are installed, what I did is get the .pro file from windows and typed qmake . , the errors are :
WARNING : Found potential symbol conflict of mainwindow.cpp (mainwwindow.cpp) in SOURCES
WARNING : Found potential symbol conflict of mainwindow.h (mainwwindow.cpp) in HEADERS
WARNING : Found potential symbol conflict of dialog.cpp (dialog.cpp) in SOURCES
WARNING : Found potential symbol conflict of dialog.h (dialog.h) in HEADERS
How can I modify qmake to specify the version of Qt
Thank you.
Below is my .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestTool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
dialog.cpp
HEADERS += mainwindow.h \
dialog.h
FORMS += mainwindow.ui \
dialog.ui
The compilation error is :
Try using the Qt4 version of qmake explicitly:
/usr/lib64/qt4/bin/qmake -o Makefile TestTool.pro
Windows 7
Qt 5.4.0
OpenCV 2.4.10
Mingw 4.9.1
I'm trying to create a simple Hello World app, to test if Qt is working with OpenCV. Besides the stuff created by default, the code is
#include <opencv/cv.h>
#include <opencv/highgui.h>
(...)
cv::Mat image= cv::imread("pic.jpg");
cv::namedWindow("Test picture");
cv::imshow("Test picture", image);
cv::waitKey(1000);
(...)
However, I'm having trouble linking libraries. At first I've set the paths to *.dll.a files manually
INCLUDEPATH += C:/opencv/my_build/install/include
INCLUDEPATH += C:/opencv/my_build/install/include/opencv
INCLUDEPATH += C:/opencv/my_build/install/include/opencv2
LIBS += -LC:/opencv/my_build/install/x86/mingw/lib \
-llibopencv_core2410 \
-llibopencv_highgui2410
etc etc
But then I'd get a cannot find -llibopencv_<lib>2410 error. I've even used Qt Creator's "Add library" function (Projects -> rightclick -> add library), the result is the same. That error only goes away if I change -llibopencv_<lib>2410 to -opencv_<lib>2410. Which from my understanding is weird, as the "l" argument is missing.
Even though that error goes away, a bunch of others replace it, as I get undefined reference to cv::EVERYTHING error. I've looked around, found solutions to either problem, but solving one leads to the other and vice versa.
I'm fairly sure the OpenCV build is not at fault - I've already completed a pretty big console-ish app using it with Code::Blocks.
My current *.pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:/opencv/my_build/install/include
win32: LIBS += -L$$PWD/../../../../../../OpenCV/my_build/install/x86/mingw/lib/ -llibopencv_core2410
INCLUDEPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
DEPENDPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
win32: LIBS += -L$$PWD/../../../../../../OpenCV/my_build/install/x86/mingw/lib/ -llibopencv_highgui2410
INCLUDEPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
DEPENDPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
How to correctly link the OpenCV libraries to Qt Creator? Am I missing something?
edit: I've rebuilt the whole damn thing and it still doesn't work. God damn it.
When specifying library, you need to exclude the 'lib' part:
LIBS += -LC:/opencv/my_build/install/x86/mingw/lib \
-lopencv_core2410 \
-lopencv_highgui2410
Frankly, I have little idea of what went wrong. I've edited the environment PATH variable, twice, to include the OpenCV build. Maybe I've made a typo the first time. It now looks like this:
C:\MinGW\bin;C:\OpenCV\my_build\install\x86\mingw\bin;C:\Qt\5.4.0\5.4\mingw491_32\bin;
My *.pro file also looks different.
QT += core
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += C:/OpenCV/qtbuild//install/include
LIBS += "C:/OpenCV/qtbuild/install/x86\mingw/lib/*.a"
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I've change +=gui to -=gui and added CONFIG -= app_bundle. The INCLUDE and LIBS paths are also different now - I won't be changing them out of an irrational fear something will go horribly wrong again.
And lastly, I've simply removed the previous project and made a new one. Maybe something else got corrupted?
Anyway, it works now, after doing these things.