Qt linker errors Qmake + Makefile - qt

I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:
TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
/usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11
# Input
SOURCES += test.cc
Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread
What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.
Anyway, this is my linker output:
test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'
The above is the result of building the following source:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?
(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)

As stated in the docs, you need to include the widget library to use QApplication.
Add this to your project file:
QT += widgets
If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.

Related

How to install Box2D-qml with mingw32

My environment:
Qt 5.3.1 + Mingw32
Windows 7
I tired these:
Extract box2d-qml in C:\Qt\Qt5.3.1\5.3\mingw482_32\imports and rename it as Box2D
Open cmd and cd into Box2D's directory .
Input qmake in cmd, no output
Input mingw32-make, no error output.
Input mingw32-make install, no error output. and Box2D.2.0 was generated under C:\Qt\Qt5.3.1\5.3\mingw482_32\qml directory.
Then i create empty quick project and add import Box2D 2.0 into qml.
Moving the mouse cursor on import Box2D 2.0,QtCreator pop up a message:
But when i run it got error:
QQmlApplicationEngine failed to load component
qrc:///main.qml:3 plugin cannot be loaded for module "Box2D": ?v???O?C?? 'C:/Qt/Qt5.3.1/5.3/mingw482_32/qml/Box2D.2.0/Box2D.dll' ????? Qt ?????????????C?u???????g?p?????????B (?f?o?b?N???????[?X?????C?u?????????g?p???邱??????????)
The main Cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
You have 2 options to use the plugin:
Installing into $QTPATH/qml (or $QTPATH/imports) folder as system-wide QML plugin. In this case you just have to do next steps:
download the source from Github and open it with QtCreator (don't put it into system folder, put the source into some user folder)
Go to Projects tab / Run / Add deploy step
add install to Make arguments
go to Edit tab, right click on the project and select Run qmake
now build the project (don't forget to set Release profile)
right click on the project and select Deploy;
The plugin dll (include all you need, Box2D code etc) will be installed into qt folder.
.
Compiling the plugin as part of your project. In this case you just have to do next steps:
download the source from Github and put the plugin source folder into your project folder (myproject/qml-box2d/, for example)
add the line into your .pro file: include(qml-box2d/box2d_lib.pri)
add this code into your main.cpp file:
#include <box2dplugin.h> // <-- this line
int main(int argc, char *argv[])
{
...
Box2DPlugin plugin; // and these
plugin.registerTypes("Box2D"); // 2 lines
...
}
Rerun qmake and recomplile the project. Now you can use Box2D items in your QML files.

Qt 5.5.1/minGW 4.9.2: Linker "cannot find lQt5Core"

I'm trying to build a helloworld project from source following the book "C++-GUI Programming with Qt4". Inside the folder containing the source file I created a project file ("test.pro") using qmake -project and a makefile ("Makefile") using qmake test.pro. When I run mingw32-make I receive the following error:
C:/Program Files/mingw-w64/x86_64-4.9.2-win32-seh-rt_v4-rev4/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lQt5Core
collect2.exe: error: ld returned 1 exit status
Makefile.Release:77: recipe for target 'release\test.exe' failed
mingw32-make[1]: * * * [release\test.exe] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/Staeng/Documents/artofarithmetic/test'
makefile:34: recipe for target 'release' failed
mingw32-make: * * * [release] Error 2
Someone having a similar problem was told to manually include some Qt libraries into the project file. However, I don't want to have to manually edit the project file whenever I introduce a new library, and as far as my (currently very limited) understanding goes qmake is supposed to do all the nasty work.
I'm on a Windows 8.1 x64 machine using Qt 5.5.1 for minGW and gcc 4.9.2 ("x86_64-win32-seh-rev4, Built by MinGW-W64 project"). Where did I go wrong?
Thanks a lot!
Source file:
// test.cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
int main(int argc, char** argv){
QApplication app(argc, argv);
QLabel* label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
Project file:
# test.pro
######################################################################
# Automatically generated by qmake (3.0) Tue Dec 29 00:58:35 2015
######################################################################
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
# Input
SOURCES += test.cpp
Maybe you should add "QT += core gui widgets" in test.pro file.

Why doesn't qmake add "QT += widgets" to *.pro files automatically?

I am trying to build a simple "Hello World" example in QT5.
I am compiling the code using the below steps
qmake-qt5 -project
qmake-qt5
make
I am getting the below error
main.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
When I read QT forums, its mentioned to add "QT += widgets" to the *.pro file. After doing this the code is compiled.
Question: Why do I need to add "QT += widgets" to the *.pro file manually ? Why doesn't qmake do it automatically ?
Note: I am using Ubuntu
Code
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The QT variable in the .pro file is used to specify the modules which are used in the project.
And as the qmake manual mentions:
By default, QT contains both core and gui, ensuring that standard GUI applications can be built without further configuration.
The Qt Widgets module is not linked by default and has thus to be specified in the .pro file with QT += widgets.

Qt 5.3.1: Why do I need to set include path and libs for widgets

I'm on Windows 8.1. I want to build Qt apps from the command line like the Unix trooper I usually am. Well, actually, I want things to be a little more automated! After wondering why qmake would not generate the proper Makefiles and hand-editing them, I finally realized I could just add on to the Qt variable in the Qt .pro file. (Been since about Qt 3-something that I've Qt'ed, so finally the light-bulb went on :)
This is the simplest of applications to help refresh the dead brain cells.
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The .pro file:
######################################################################
# Automatically generated by qmake (3.0) Sat Aug 23 16:38:04 2014
######################################################################
TEMPLATE = app
TARGET = HelloQt
INCLUDEPATH += .
#why do I need these
INCLUDEPATH += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\include\qtwidgets"
LIBS += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\lib\Qt5widgets.lib"
#why does the following line do nothing
qmake: Qt += widgets
CONFIG -= X86
# Input
SOURCES += hello.cpp
Why do I need to specify the widgets INCLUDEPATH and LIBS to get a successful build?
As far as I can tell, Qt += widgets is not a valid addition to the Qt variable.
Why not? If I don't specify the Qt5Widgets include and libs, and I run nmake, I get errors related to not finding the include for QApplication and/or linker errors.
With those two lines in the .pro file everything works fine. Has anybody run into this sort of thing, am I missing something?
The line should look more like QT += core gui widgets. This will set the INCLUDEPATH and LIBS.

QT ffmpeg Setting

I can't use the ffmpeg in QT.
My Steps :
-1. compile the ffmpeg as normal
-2. add lib/include path in QT .pro
LIBS += -L/home/kim/ffmpeg/lib -lavcodec -lavformat
INCLUDEPATH += /home/kim/ffmpeg/include
-3. in code
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
and just call a function named av_register_all();
then I got the following bulid issues< many functions undefined reference >
in function `rl2_read_packet`: undefined reference to `av_free_packet`
in function `av_register_all`: ....
I search a solution of the problem but not work for me.
Any other solution? Thanks.
This is an old post and it is almost solved. By the way, I faced to the problem and I had lots of time struggling with
undefined reference to av_register_all
I am using Qt Creator 5.3.2 and this is my MYPROJECT.pro file
MYPROJECT.pro
QT += core
QT -= gui
TARGET = MYPROJECT
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
QMAKE_CXXFLAGS += -D__STDC_CONSTANT_MACROS
LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil -lswscale
INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include
Setting up project.pro
It is recommended to build ffmpeg from scratch, but if you are on windows and if you are not willing to compile this masterpiece, then congrats! you are like me! Go for pre-built in zereanoe and download both the latest shared and dev win-32 versions. Extract the latter two packages in a place where there is no spaces in the path, i.e. mine is in: C:\ffmpeg-20151219-git-2dba040-win32-dev
I recommend that don't rename the main folder because it is highly informative. Set the include folder of the [PATH TO ffmpg-YYYYMMDD-git-win32-dev]/include to your project's search space by the following line of code:
INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include
You should also use libraries in [PATH TO ffmpeg-YYYYMMDD-git-win32-dev]/lib to your project as below:
LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil
You can call libraries other than lavcodec, lavformat and lavutil. In your cpp file you should enclose headers corresponding to ffmpeg in extern "C" block.
DLLs should be added
The final step is to add all the dll files in the shared version of ffmpeg that you have recently downloaded.
Copy *.dll files within
[PATH TO ffmpeg-YYYYMMDD-git-2dba040-win32-shared]/bin
and paste them wherever your executable exists. Fo instance, my project is located on C:/QtProjects/MYPROJECT, then my binaries are located on:
C:/QtProjects/build-MYPROJECT-Desktop_Qt_5_3_MinGW_32bit-Debug
Now, copy all the dlls to your project's debug or release folder.
My main.cpp
This is my main.cpp :
main.cpp
#include <iostream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
using namespace std;
int main(int argc, char *argv[])
{
cout<<"I have included FFMPEG in my project"<<endl;
av_register_all();
cout<<"If everything goes well you will see ME"<<endl;
return 0;
}
I had the same problems, it worked using the following trick.
In code cpp source:
#define __STDC_CONSTANT_MACROS
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}
And in your .pro file:
LIBS += -LC:/Users/tom/bla/ffmpeg/lib/ -lavcodec -lavformat -lavutil
I think in your case, you need the -lavutil lib parameter.

Resources