I'm trying to get a simple hello world example running, and already needed some time to figure out what includes to use
Now I verified the include paths, the QApplication should actually be there, but it throws the above error. For clarity my code:
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *button = new QPushButton("Hello world!");
button->show();
return app.exec();
}
I tried compiling using first qmake -project, then qmake and finally make and then got the following errors:
qt_hello_world.o: In function 'main':
undefined reference to QApplication::QApplication(int&, char**, int)
qt_hello_world.cpp: undefined reference to QPushButton::QPushButton(QString const&, QWidget*)
qt_hello_world.cpp: undefined reference to QWidget::show()
qt_hello_world.cpp: undefined reference to QApplication::exec()
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()
The Makefile created by qmake contains the correct include path to the qt5 directory which contains the QtWidgets/QApplication, the QApplication file just includes the qapplication.h header that contains the actual class QApplication.
The tutorial https://wiki.qt.io/Qt_for_Beginners is fully updated so you must modify it. Change to:
TEMPLATE = app
TARGET = callboot-ui.exe
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS +=
SOURCES += main.cpp
TL; DR;
In the case of #jww it has an error in the following line of the .pro:
greaterThan(QT_MAJOR_VERSION, 5): QT += core gui widgets
The error is caused because greaterThan(QT_MAJOR_VERSION, 5) verifies that the major version of Qt is greater than 5 to add sub-modules, but the latest version of Qt is 5.13.2 is not greater than 5, so it is not linked the modules causing the error shown.
In the tutorial greaterThan(QT_MAJOR_VERSION, 4): QT += widgets is used to support .pro so that it can be compiled for Qt4 and Qt5 since in the latter the widgets moved to a new sub-module called widgets.
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
When trying to use some Qt-5 classes I'm experiencing crashes. I first discovered this trying to use QFileSystemModel. Trying to call setRootPath immediately leads to a crash. The callstack isn't of much help (all of it is assembly code) except that QFileIconProvider::icon() is the last function called before the seg fault occurs.
So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program.
I'm using QtCreator 4 and the type of project is "Qt Quick Application". When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems.
Here's where I'm out of ideas. I don't know enough about the Qt environment to know what difference between the two types of projects could lead to the seg fault.
Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator.
This is my project.pro file:
TEMPLATE = app
QT += qml quick widgets //default .pro file except for widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
This is main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>
#include <QFileSystemModel>
#include <QQmlContext>
#include <QFileIconProvider>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//If trying to use QFileSystemMode...
QFileSystemModel model;
model.setRootPath("/somefolder/"); //..the crash happens here
//Attempting to use QFileIconProvider also crashes
//QFileIconProvider fip;
//fip.icon( QFileInfo("/somefolder/somefile") ); //<- here
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
I'd appreciate any help or pointers as to how to debug that mess.
As confusing as it may sound, QFileSystemModel is part of QtWidgets and therefore requires you to create and instance of QApplication instead of QGuiApplication.
I've already gone through questions similar to mine, but none solved my problem.
So I'm trying to use SDL in a Qt Widget (for educational purpose), and I always get and undefined reference on every SDL fonction that I call.
Here is the (minimalist) code I've been using for testing :
#include "mainview.h"
#include <QApplication>
#include "SDL.h"
#undef main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainView w;
w.show();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindowFrom((void*)w.centralWidget->winId());
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderFillRect(render, NULL);
SDL_RenderPresent(render);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
return a.exec();
}
And here is th .pro file :
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-08T22:37:55
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MapEditor
TEMPLATE = app
SOURCES += main.cpp\
mainview.cpp
HEADERS += mainview.h
FORMS += mainview.ui
INCLUDEPATH += SDL/include/
LIBS += -L SDL/lib/x86/SDL2.lib
According to other answers i've read, it seems the problem comes from the link to the library.
I have never really used this file, or other qmake before, so I am not sure about the path I am using. I work on windows, and according to this article, I should be writing the full path, but then I get access authorization problems (damn Windows).
Furthermore, changing the path to an false one doesn't seems to upset the compiler.
However, when I try to use
LIBS += -L SDL/lib/x86/ -lSDL2.lib
the compiler cannot find the file.
The compiler I'm using is a MinGW 32bits, so I've downloaded the corresponding file on lbsdb.org, and I've tried every library on very sub-folder there was (x86_64, i686,...) but none worked.
And sorry if the way I'm writing is strange, I'm french.
When you have a .lib file, it's easiest to just add the file for linker. You have it subdirectory of your project it seems, but you are probably using a separate build directory (as you should), so the problem is probably that. To fix, use PWD qmake variable:
LIBS += $${PWD}/SDL/lib/x86/SDL2.lib
You might also want to add PWD to include path, because relative path there is relative to directory of the .c file, and you might not always have them all in the same directory.
In a comment you mention something about not being able to use absolute paths... Not sure what you mean by that, but then a way out is to not use shadow build (which is no problem, as long as you remember to do full rebuild when changing build types). If you are using Qt Creator, you can find the project's build settings for each build type under Projects view, and there you have a check box for shadow build.
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.
I'm trying to get the libao library working in Qt. Here's what I have so far.
#include <ao/ao.h>
...
static int audio_driver;
static ao_device *audio_device;
static ao_sample_format audio_format;
...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ao_initialize();
audio_driver = ao_default_driver_id();
MainWindow w;
w.show();
return a.exec();
}
It says that every reference to anything in the ao library is an undefined reference.
error: undefined reference to `ao_initialize'
error: undefined reference to `ao_default_driver_id'
And so on all the way through the code.
For what it's worth, every function in ao/ao.h is in an extern "C".
Any idea what's causing this?
Many thanks.
You doesn't link against ao dynamic library.
If you use qmake add following lines in .pro file
LIBS += -lao
If library in non-standard location, add these lines too
INCLUDEPATH += path/to/headers
LIBPATH += path/to/library
If you're on Linux, or anywhere else where pkg-config is available, then the way this should be done is by adding "link_pkgconfig" to the CONFIG variable, and then add the package name to the PKGCONFIG variable. For example, if you're using libao and libvorbisfile:
CONFIG += link_pkgconfig
PKGCONFIG += ao vorbisfile
This will make sure that not only the correct link flags will be used, but also the correct CFLAGS/CXXFLAGS, which is also important.