undefined reference SDL_Init with Qt - c++

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.

Related

The program has unexpectedly finished in QT opencv

I know that this has been posted many times,but I could not find the solution from previous posts. I followed tutorial on How to setup Qt and openCV on Windows from wiki Qt.
My .pro file and mainwindows.cpp are shown below. I wanted to open image following the example. What is wrong here? Checked the opencv version and it is the same as libs included. The PATH is also correct.
The cpp file
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::imread("C://1.jpg", 1);
cv::namedWindow("My Image");
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
and
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opencvtest
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_core451.dll
LIBS += C:\opencv-build\bin\libopencv_highgui451.dll
LIBS += C:\opencv-build\bin\libopencv_imgcodecs451.dll
LIBS += C:\opencv-build\bin\libopencv_imgproc451.dll
LIBS += C:\opencv-build\bin\libopencv_features2d451.dll
LIBS += C:\opencv-build\bin\libopencv_calib3d451.dll
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When program crashes like that under Qt Creator, and you have extra libraries, the very likely reason is that the extra libraries are missing from the runtime PATH.
In this case, you need to add C:\opencv-build\bin to the PATH. There are at least 3 ways to go about it.
Edit the system environment, so that the relevant directory is always in the system PATH. You need to restart Qt Creator for this change to take effect. This is not the recommended way, unless you actually want these things in there also for other purposes.
You can edit the Build environment of the project under Qt Creator Project view. There's separate configuration for each build type, so you may need to do this to them all separately, which both good and bad. It is good, because then you can have different directory for different builds (for example debug vs relase, MSVC vs MinGW builds). It's bad because it's extra hassle and makes it easier to have something wrong.
You can add it to the run environment in the Qt Creator Project view. Then it will be the same for all build types.
In this case, 3 is probably the way to go.
Qt Creator annoyingly does not display any information about which DLL is missing, it just says the program crashed. This can be solved by instead string the "Qt command prompt" for the correct toolchain from Windows Start menu (search Qt and you should find it). Then go to the built .exe directory and run the .exe. You should now get an error dialog where Windows tells you which DLL it failed to find. Then you can look where that DLL is and add it to the path and try again, until the program starts. After you know the directories using this method, you can then add them to Qt Creator as explained above.

Undefined reference to CMU Sphinx functions in Qt Creator

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.

How to get the CImg library working for Qt

I'm currently learning how to use Qt. I want to try out some simple image processing applications using Qt, and since I'm already familiar with CImg I want to use that. I guess it should be possible to do so, if not mark my question for deletion or something.
My question is: how to get CImg working for Qt? CImg is a header file. Lets say its located on my desktop. I import it using Qt creator 4.1.0, by using the "add existing file..." in the rightclick menu on the header folder. Then my menu looks like this:
.
It compiles when I add #include "CImg.h", but I can't use it, even when I'm trying to type using namespace cimg_library it will tell me that cimg_library doesn't exist. I also tried just creating a header file and copying the content of the CImg.h into it but then it simply fails to compile and the Qt Creator freezes.
Edit: I managed to make the situation a bit better by adding the header location to the include code (like this: #include "C:/Users/Marci/Desktop/CImg.h" )I can now "see" CImg related stuff in the dev environment, and it won't bother me with not finding the constructor for CImg or anything like that. However when I try to compile while using anything CImg related it will give me around 20 linker errors. (Error code: LNK2019) My .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
C:/Users/Marci/Desktop/CImg.h
LIBS += -C:/Users/Marci/Desktop/ -CImg.h
FORMS += mainwindow.ui
Edit2: after implementing the changes that PeterT suggested in his comment my .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
INCLUDEPATH += C:/Users/Marci/Desktop
FORMS += mainwindow.ui
And my mainwindow.cpp (in which i'm trying to create a CImg object) looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <CImg.h>
using namespace cimg_library;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CImg<unsigned char> myimage(100,200);
}
MainWindow::~MainWindow()
{
delete ui;
}
The compiler errors i get are: error: C2871: 'cimg_library': a namespace with this name does not exist
error: C2065: 'CImg': undeclared identifier
error: C2062: type 'unsigned char' unexpected
I hope this is specific enough.
After I few months I figured it out. The problem lies in the fact, that CImg uses a windows .dll file for the visualizing functions of the class cimg_display. Since Qt is platform independent it doesnt like this. However you can make it work with an interesting trick. First you need to include the header file normally, in the project file. After that, whenever you actually #include it, you need to write the following macro:
#define cimg_display 0
In my understanding this makes it work, because the C and C++ compilers simply copy the content of the included file into the source. And thanks to the macro the compiler will ignore the class thats causing trouble for us.

Error accessing GStreamer Library in Qt C++ - Program excited with code 0xc0000135 when Debugging

I want to use gstreamer library for video programming and I read that Qt5.5.0 provided support for gstreamer-1.0, which is the library I use. I downloaded firstly version 1.0.7, but I got the following error, so I downloaded the latest version 1.5.2, which I am trying to use now.
I managed to link the gstreamer library header files, so I didn't build it, but just include its path and access the header files and libs. Anyway, now I have an error of The program has unexpectedly finished. When I try to debug it, I get the following error in a pop-up:
During startup program excited with code 0xc0000135.
On General Messages:
:1:24: Reading only version 1.1 parts.
:10:5: Expected only Component and ModuleApi object definitions.
I read many questions related to this error, but none helped, I think is an error with the gstreamer library, but may be a compiler/debugger error too, as I read in some threads.
I checked my PATH variable, as I also can not run the program outside Qt (by trying to run the .exe in debug folder of the build) - the error : library Qt5Cored.dll missing - and I found this paths:
C:\Qt\Qt5.5.0_Android\5.5\mingw492_32\lib;
C:\Users\user\Documents\build-GStreamer-test5-Desktop_Qt_5_5_0_MinGW_32bit-Debug;
C:\Qt\Qt5.5.0_Android\5.5\mingw492_32\bin;
C:\Qt\Qt5.5.0_Android\Tools\mingw492_32\bin;
C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0;
C:\Program Files\Skype\Phone\ (I dont know why is this included, but it was there when I checked )
I am relatively new to Qt, and never encountered this error before. If you need any code:
.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GStreamer-test5
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:/gstreamer/1.0/x86/include/gstreamer-1.0
C:/gstreamer/1.0/x86/lib/gstreamer-1.0/include
C:/gstreamer/1.0/x86/include/glib-2.0
C:/gstreamer/1.0/x86/lib/glib-2.0/include
LIBS += -L C:/gstreamer/1.0/x86/lib/ -lgstreamer-1.0
and .main:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <stdio.h>
#include "gst/gst.h"
void *__gxx_personality_v0;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
const gchar *nano_str;
guint major, minor, micro, nano;
gst_init (&argc, &argv);
gst_version (&major, &minor, &micro, &nano);
if (nano == 1)
nano_str = "(CVS)";
else if (nano == 2)
nano_str = "(Prerelease)";
else
nano_str = "";
// printf ("This program is linked against GStreamer %d.%d.%d %s\n", major, minor, micro, nano_str);
qDebug() << "This program is linked against GStreamer %d.%d.%d %s\n", major, minor, micro, nano_str ;
return a.exec();
Firstly, I had the error :
undefined reference to `__gxx_personality_v0' , but fixed it by adding *void __gxx_personality_v0; , although I don't know what excatly it is.
Its been three days I struggle with this problem. Any help appreciated, or maybe if you can help me by some code on how to access the header files and libs, or how the configuration of the debugger options / PATH must be.
I am using Qt5.5.0 for Android, for Windows I use MinGW 4.9.2 32 bit compiler and GNU gdb 7.8 as debugger.
Thanks in advance!
I had similar problems in my project that was using both Qt and Gstreamer.
The сause of "undefined reference to `__gxx_personality_v0'" error is that Qt and GStreamer for windows from official sites were built using different compilers and their libstdc++ libraries are not compatible - they are using different exception handling methods.
In fact, GStreamer is pure C library. The only component of it that was written in C++ is taglib library. If you will build this lib from sources using your MinGW compiler, and then replace it in the GStreamer folder, the problem of incompatibility will be solved.
In more detail you can read here: https://github.com/knowthelist/knowthelist.
I managed to solve the above problem. I had to include in the .exe folder (the build-debug folder) all the dlls included, so I tried to run the .exe from there, not run it from Qt, and some errors with dlls missing gave me the names of the dlls. I found some in my library bin folder and some downloaded, and now everything seems fine :)

QtCreator LNK2019 error with external library

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.