It's the first time I make mock object in QT. I'm trying using Gmock, but I don't know how to using it.
Now, I create project TestGmock (QT Application) in QT, and I copy include folder in gmock-1.7.0 ( download it from https://code.google.com/) to TestGmock project directory ( and the same with gtest ). In class main :
#include <QCoreApplication>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
int main(int argc, char *argv[])
{
testing::InitGoogleMock (&argc, argv);
return RUN_ALL_TESTS();
}
But error :
C:\Qt\Qt5.2.1\Tools\QtCreator\bin\TestGmock\main.cpp:6: error: undefined reference to `testing::InitGoogleMock(int*, char**)'
C:\Qt\Qt5.2.1\Tools\QtCreator\bin\TestGmock\gtest\gtest.h:2288: error:undefined reference to `testing::UnitTest::GetInstance()'
C:\Qt\Qt5.2.1\Tools\QtCreator\bin\TestGmock\gtest\gtest.h:2288: error:undefined reference to `testing::UnitTest::Run()' collect2.exe:-1:
error: error: ld returned 1 exit status
Please help me using gmock and gtest in QT.
This error is caused the fact, that Qt Creator couldn't find Gmock library. There is no need to put it in your project (or Qt installation) directory, but you should add INCLUDEPATH and LIBS options in .pro file. Look at Adding external library into Qt Creator project.
Related
I'm following Qt's Qt for Beginners tutorial and for some reason I am getting a compilation error when trying to build the simplest example of a gui. It looks like it's failing on button creation because it thinks that qcolor is unimplemented(?). I just downloaded the latest Qt 5.14.0 and installed it today, so it's possible that something happened during the install?
This is what my project layout looks like:
build-new_qt_project-Desktop_Qt_5_14_0_GCC_64bit-Debug:
Makefile
new_qt_project:
main.cpp
new_qt_project.pro
main.cpp:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
new_qt_project.pro:
TEMPLATE = app
TARGET = new_qt_project
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp
And this is the output from trying to make it:
In file included from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qpixmap.h:45:0,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qicon.h:46,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qabstractbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qpushbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/QPushButton:1,
from ../new_qt_project/main.cpp:2:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h: In constructor ‘constexpr QColor::QColor(int, int, int, int)’:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h:79:18: sorry, unimplemented: use of the value of the object being constructed in a constant expression
0) {}
^
make: *** [main.o] Error 1
I'm using the default (detected) kit to build it and it has the C++ compiler pointing to the one in /usr/bin, so I think that is correct. Is there something additional I've missed?
I think this is the problem:
Qt bug
update your gcc. up to 4.9 !!!
i had this problem and i used gcc 4.8 and after update it to gcc-7 it is now correct
I'm trying to use CMU Sphinx in Qt. I've installed pocketsphinx and sphinxbase, and made a project in Qt, and added the following code to the main.cpp file:
#include <pocketsphinx.h>
#define MODELDIR "/usr/local/share/pocketsphinx/model"
int main(int argc, char* argv[])
{
ps_decoder_t *ps = nullptr;
cmd_ln_t *config = nullptr;
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "/en-us/en-us",
"-lm", MODELDIR "/en-us/en-us.lm.bin",
"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
NULL);
}
I've also added the following lines to the .pro file:
INCLUDEPATH += /usr/local/include/sphinxbase
INCLUDEPATH += /usr/local/include/pocketsphinx
When I try building, I get the following errors:
undefined reference to ps_args
undefined reference to cmd_ln_init
I'm on Debian 8. What have I missed?
Okay, so this might seem silly, but I finally solved the issue, and I hope this helps anyone with similar errors in Qt in the future.
The way to solve it is to right-click anywhere in the editor, and click Add Library, and then select System Library. I then entered the library name, which in my case was pocketsphinx. I repeated it for sphinxbase. Then go to Build - Run qmake, and then build.
I have a problem when I want to link a library to my Qt project.
When I try to include an external library (libnodave.lib) in Qt Creator and try to build it, the following error occurs.
main.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_daveSetDebug referenced in function main
I'm pretty sure that I included all needed files in my project and the .pro file. I used the "Add Library" wizard to add the library.
After no success with Qt Creator, I created a minimal example with Visual Studio. When I include all the needed files to the VS project, I can build and run it without errors. So I think that there must be a problem with Qt Creator linking the library. I also tried the Qt-Visual-Studio-Add-in, but there, the same error occurs.
Here are my minimal examples with the library I want to include.
In the Visual Studio example, I added the library path, the include path, and the name of the library to the project properties. It works.
I hope you can help me with my problem.
EDIT:
I want to use the library to get some data from a S7-300 SPS device.
The following code is the minimal example from Qt Creator.
#include <QCoreApplication>
#include <QDebug>
#include <nodave.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
daveInterface *di;
daveSetDebug(daveDebugConnect); // Function of libnodave Library
qDebug() << "Hello World";
return a.exec();
}
This is the whole code from the Visual Studio minimal example.
#include "stdafx.h"
#include <nodave.h>
int _tmain(int argc, _TCHAR* argv[])
{
daveInterface *di;
daveSetDebug(daveDebugConnect);
printf("Hello World\n");
return 0;
}
The code is very small, so I don't think that there is an error inside.
That's why I think it must be a problem with the Qt linker or something like that.
EDIT:
My .pro file.
QT += core
QT -= gui
TARGET = qtminimal
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32: LIBS += -L$$PWD/../libnodave-0.8.5/win/ -llibnodave
INCLUDEPATH += $$PWD/../libnodave-0.8.5
DEPENDPATH += $$PWD/../libnodave-0.8.5
The problem was that the Qt project is 64bit and the library I want to include is only 32bit.
So I downloaded the 32bit version of Qt and now it works.
I found the mistake, when I tried to build only the minimal example with libnodave, without any 64bit Qt libraries.
By creating a new Qt project in VS2013, with this workaround and adding the libnodave library afterwards I could change whether it should be a 64bit or 32bit build. By choosing the 32bit build, the Qt library creates errors but not the libnodave lib. When I choose 64bit build, only libnodave creates the errors.
I hope it is useful for someone else.
I know it's a stupid question, but still.
I want to use the QJson library in my project. I've downloaded the tarball from the official site (probably need to mention that I'm under 64-bit Ubuntu 12.04). The INSTALL file has the following instructions
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=_preferred_path_ ..
make
make install
/sbin/ldconfig, if necessary
which I followed precisely. I've got the /include, /lib and share folders of QJson added to my /usr/local.
After that, I open my IDE (which is QtCreator), and make a test project with the following simple code:
#include <QVariant>
#include <qjson/serializer.h>
int main(int argc, char *argv[])
{
QJson::Serializer s;
QVariantMap map;
map["hello"] = QVariantList() <<"t1"<<"t2";
QByteArray json = s.serialize(map);
}
The #include is handled fine, all the types are recognized, the auto-complete for the QJson classes works fine. However, when trying to compile, I'm getting this (full path removed for readability):
<...>/QJsonTest/main.cpp:15: undefined reference to `QJson::Serializer::Serializer()'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::serialize(QVariant const&)'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::~Serializer()'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::~Serializer()'
collect2: ld returned 1 exit status
What's the reason, and how do I make it work?
You are hitting linker (not compiler) errors. You need to add the path to your QJson library files with a -L option
I'm new to Code::Blocks (nice IDE!) and to Qt with C++ (been using PyQt, which works wonderfully)
I have a test project - here's the code (generated by code::blocks wizard):
#include <QApplication>
#include <QFont>
#include <QPushButton>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
I BELIEVE I have the search path for Qt4 configured correctly in code::blocks, but when I try to run this code I get the below listed errors - so obviously I'm missing something - either my paths are in correct or my installation is corrupt. For my Qt installation I used Qt_SDK_Win_offline_v1_1_2_en.exe, found on the Qt website - installed the entire Qt SDK in D:, so the root path for the Qt installation is D:\QtSDK..
Any help on how to fix this configuration would be appreciated - I am excited about using Qt with C++ after fighting with Win32s/MFC/.NET etc etc for so many years....
Errors:
||=== QtTest, Debug ===|
obj\Debug\main.o||In function `main':|
E:\ResourceBrowse\QtTest\main.cpp|7|undefined reference to >`imp__ZN12QApplicationC1ERiPPci'|
E:\ResourceBrowse\QtTest\main.cpp|9|undefined reference to `imp__ZN11QPushButtonC1ERK7QStringP7QWidget'|
E:\ResourceBrowse\QtTest\main.cpp|12|undefined reference to `imp__ZN5QFontC1ERK7QStringiib'|
E:\ResourceBrowse\QtTest\main.cpp|12|undefined reference to `imp__ZN7QWidget7setFontERK5QFont'|
E:\ResourceBrowse\QtTest\main.cpp|12|undefined reference to `imp__ZN5QFontD1Ev'|
E:\ResourceBrowse\QtTest\main.cpp|12|undefined reference to `imp__ZN5QFontD1Ev'|
E:\ResourceBrowse\QtTest\main.cpp|14|undefined reference to `imp__Z13qFlagLocationPKc'|
E:\ResourceBrowse\QtTest\main.cpp|14|undefined reference to `imp__Z13qFlagLocationPKc'|
E:\ResourceBrowse\QtTest\main.cpp|14|undefined reference to `imp__ZN7QObject7connectEPKS_PKcS1_S3_N2Qt14ConnectionTypeE'|
E:\ResourceBrowse\QtTest\main.cpp|18|undefined reference to `imp__ZN12QApplication4execEv'|
E:\ResourceBrowse\QtTest\main.cpp|18|undefined reference to `imp__ZN11QPushButtonD1Ev'|
E:\ResourceBrowse\QtTest\main.cpp|18|undefined reference to `imp__ZN11QPushButtonD1Ev'|
E:\ResourceBrowse\QtTest\main.cpp|18|undefined reference to `imp__ZN12QApplicationD1Ev'|
E:\ResourceBrowse\QtTest\main.cpp|18|undefined reference to _imp___ZN12QApplicationD1Ev'|
obj\Debug\main.o||In functionQString':|
D:\QtSDK\Desktop\Qt\4.7.3\msvc2008\include\QtCore\qstring.h|426|undefined reference to _imp___ZN7QString16fromAscii_helperEPKci'|
obj\Debug\main.o||In function~QString':|
D:\QtSDK\Desktop\Qt\4.7.3\msvc2008\include\QtCore\qstring.h|883|undefined reference to `imp__ZN7QString4freeEPNS_4DataE'|
obj\Debug\main.o:D:\QtSDK\Desktop\Qt\4.7.3\msvc2008\include\QtGui\qwidget.h|1001|undefined reference to
`imp__ZN7QWidget6resizeERK5QSize'|
||=== Build finished: 17 errors, 0 warnings ===|