SDL2 undefined reference sdl_Main in QtCreator - c++

Im trying to use QtCreator to create a project including SDL2. But SDL is failing to compile correctly and I can't see the error.
My QtCreator .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Psiora
TEMPLATE = app
SOURCES += main.cpp\
ui_mainwindow.cpp
HEADERS += ui_mainwindow.h
FORMS += ui_mainwindow.ui
INCLUDEPATH += "D:\devel\SDL2-2.0.3\i686-w64-mingw32\include\SDL2"
LIBS += -L"D:\devel\SDL2-2.0.3\i686-w64-mingw32\lib" \
-lmingw32 \
-mwindows \
-lSDL2main \
-lSDL2
Main.cpp
#include "ui_mainwindow.h"
#include <QApplication>
#define SDL_MAIN_HANDLED //Let QT Supply WinMain
#include "SDL.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
UI_mainWindow win;
win.show();
return app.exec();
}
Compiler errors
g++ -Wl,-subsystem,windows -mthreads -o debug\Psiora.exe debug/main.o debug/ui_mainwindow.o debug/moc_ui_mainwindow.o -lglu32 -lopengl32 -lgdi32 -luser32 -lqtmaind -LD:\devel\SDL2-2.0.3\i686-w64-mingw32\lib -lmingw32 -mwindows -lSDL2main -lSDL2 -LD:\devel\Qt\5.2.1\mingw48_32\lib -lQt5Widgetsd -lQt5Guid -lQt5Cored
D:\devel\SDL2-2.0.3\i686-w64-mingw32\lib/libSDL2main.a(SDL_windows_main.o): In function `console_main':
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
I don't understand why SDL is giving an undefined reference to sdl_Main, when it has been linked in the correct order. I also don't get where this "/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/" has come from when there is no such path on my computer, nor have I added the path myself.
My understanding is the SDL_Main provides WinMain however I wish to use the WinMain provided by Qt instead so I have defined the SDL_MAIN_HANDLED define to stop SDL using SDL_Main.
Addition Information:
OS: Windows 8.1 Pro 32bit
Compiler: MinGW 4.8 - The default one provided with the Qt Installer
Qt: 5.2.1
SDL 2.0.3 - Downloaded directly from libsdl.org, unmodified with no patches.

Add this to your project file to solve this:
win32:QMAKE_LIBS_QT_ENGTRY -= -lqtmain
win32-g++:DEFINES -= QT_NEEDS_QMAIN
or
CONFIG-= windows
QMAKE_LFLAGS += $$QMAKE_LFLAGS_WINDOWS

Related

Adding ObjectBox to Qt application

I have just tried to add ObjectBox to an empty qt application, but somehow I do not get any further.
I tried to follow the instructions: https://cpp.objectbox.io/installation
I took the win x64 lib from here: https://github.com/objectbox/objectbox-c/releases/tag/v0.17.0
I am always getting undefined reference errors and I do not know why. I guess there must be something wrong with linking in der .pro file?
Thats my .pro file (just edited the last 4 lines):
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/objectbox/lib/ -lobjectbox
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/objectbox/lib/ -lobjectbox
INCLUDEPATH += $$PWD/objectbox/include
DEPENDPATH += $$PWD/objectbox/include
And here ist my main.cpp:
#include <QCoreApplication>
#include "objectbox.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Using ObjectBox version %s\n", obx_version_string());
return a.exec();
}
Compiler says the following:
C:/Qt/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/temp/obxTut/ObjectBoxTutorial'
g++ -c -fno-keep-inline-dllexport -g -std=gnu++11 -Wall -W -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_CORE_LIB -I. -Iobjectbox\include -I..\..\..\Qt\5.12.11\mingw73_64\include -I..\..\..\Qt\5.12.11\mingw73_64\include\QtCore -Idebug -I..\..\..\Qt\5.12.11\mingw73_64\mkspecs\win32-g++ -o debug\main.o main.cpp
In file included from main.cpp:2:0:
objectbox\include/objectbox.hpp:711:21: warning: 'std::vector<long long unsigned int> obx::{anonymous}::idVectorOrThrow(OBX_id_array*)' defined but not used [-Wunused-function]
std::vector<obx_id> idVectorOrThrow(OBX_id_array* cIds) {
^~~~~~~~~~~~~~~
objectbox\include/objectbox.hpp:703:14: warning: 'OBX_id_array obx::{anonymous}::cIdArrayRef(const std::vector<long long unsigned int>&)' defined but not used [-Wunused-function]
OBX_id_array cIdArrayRef(const std::vector<obx_id>& ids) {
^~~~~~~~~~~
g++ -Wl,-subsystem,console -mthreads -o debug\ObjectBoxTutorial.exe debug/main.o -LC:\temp\obxTut\ObjectBoxTutorial\objectbox\lib -lobjectbox C:\Qt\5.12.11\mingw73_64\lib\libQt5Cored.a
debug/main.o: In function `idVectorOrThrow':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:712: undefined reference to `obx::internal::throwLastError(int, char const*)'
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:718: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
debug/main.o: In function `applyTo':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:828: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:65: debug/ObjectBoxTutorial.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/temp/obxTut/ObjectBoxTutorial'
23:05:08: The process "C:\Qt\Tools\mingw730_64\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project ObjectBoxTutorial (kit: Desktop Qt 5.12.11 MinGW 64-bit)
When executing step "Make"
Qt 5.12 with MinGW 7.3
The problem is the compiler that you are using, because the library is probably compiled with MSVC and you are trying to build your application with MinGW and use the C++ interface of the library.
this won't work because of name mangling and etc. see How to use libraries compiled with MingW in MSVC?
Thankfully this library also provides a pure C interface "objectbox.h", so including this header instead of the one with "hpp" would also fix your issue.
I was able to build and link a test app by using MSVC 2015 64 Bit.

How to link a dynamic library in QtCreator use qmake and MinGW32?

I am trying to use hiredis and libevent in my project, I downloaded their source code and compiled them with CmakeGUI and MinGW32. Then i got the header files and those library files.
libevent.a
libevent.dll
libevent.dll.a
libevent_core.a
libevent_core.dll
libevent_core.dll.a
libevent_extra.a
libevent_extra.dll
libevent_extra.dll.a
libhiredis.dll
libhiredis.dll.a
Then I wrote some test programs to use these libraries, but could not link successfully
The error looks like this:
error: undefined reference to `redisConnect'
here is my pro file and code.
#my pro file
#hiredis
INCLUDEPATH += $$PWD/include/hiredis
LIBS += -L$$PWD/lib/ -llibhiredis.dll
#event2
INCLUDEPATH += $$PWD/include/libevent
LIBS += -L$$PWD/lib/ -llibevent.dll
LIBS += -L$$PWD/lib/ -llibevent_core.dll
LIBS += -L$$PWD/lib/ -llibevent_extra.dll
#include "hiredis.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow) {
ui->setupUi(this);
redisContext *asd = redisConnect("127.0.0.1", 6379);
}
This is the compile output
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Test.exe release/main.o
release/mainwindow.o release/moc_mainwindow.o
-LC:\Users\VC\Desktop\Test\lib C:\Users\VC\Desktop\Test\lib\libhiredis.dll.a
C:\Users\VC\Desktop\Test\lib\libevent.dll.a
C:\Users\VC\Desktop\Test\lib\libevent_core.dll.a
C:\Users\VC\Desktop\Test\lib\libevent_extra.dll.a
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Widgets.a
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Gui.a
D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libQt5Core.a
-lmingw32 D:\Work\env\cpp\Qt\5.12.9\mingw73_32\lib\libqtmain.a
-LC:\openssl\lib -LC:\Utils\my_sql\mysql-5.6.11-win32\lib
-LC:\Utils\postgresql\pgsql\lib -lshell32
release/mainwindow.o:mainwindow.cpp:(.text+0x2c9): undefined reference to `redisConnect'
I tried two different libraries(libevent and hiredis) and got similar results, what am I doing wrong?
thank you
Try to remove the .dll, just give the name of the lib, I mean include the lib like this :
INCLUDEPATH += $$PWD/include/hiredis
INCLUDEPATH += -L$$PWD/lib
LIBS += -L$$PWD/lib/ -llibhiredis
Then clean the project and run qmake.
I once set a 64-bit version of MinGW in environment variables, but I forgot
When I changed it to the 32-bit MinGW path, the compilation was successful and the dynamic library can be used

compilation error of OpenCV cpp code using Qt

I am using a very basic OpenCV code to compile using Qt but every time the following errors are coming.
OpenCV version
OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
My code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat I = imread("path/filename.ppm", 1);
imshow("conv_image",I);
waitKey();
return 0;
}
.pro file
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you
use
# any feature of Qt which as 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 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
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Given Error
:-1: warning: libopencv_core.so.3.4, needed by /usr/local/lib/libopencv_imgcodecs.so, may conflict with libopencv_core.so.2.4
:-1: error: main.o: undefined reference to symbol '_ZN2cv6String10deallocateEv'
/usr/local/lib/libopencv_core.so.3.4:-1: error: error adding symbols: DSO missing from command line
:-1: error: collect2: error: ld returned 1 exit status
In my .pro file, I have used the following line(from the different link I have found 3 ways, I have tried all but failed)
First
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Second
INCLUDEPATH +=/usr/local/include/opencv
LIBS +=`pkg-config --libs opencv`
Third
I have used information from here
Please give me suggestion to solve the issue.

Rosbag reader in qtcreator

I try to make a rosbag reader in Qtcreator. I create a project and I just want to read informations inside the bag.
Here is my pro file:
QT += core
QT -= gui
CONFIG += c++11
TARGET = ROSBag_Reader
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS += -lboost_system
LIBS+= -lpthread
LIBS+= -L/usr/lib/x86_64-linux-gnu
LIBS+= -ltf2_ros
LIBS+= -lrostime
LIBS+= -lrospack
LIBS+= -lroslz4
LIBS+= -lroslib
LIBS+= -lroscpp_serialization
LIBS+= -lroscpp
LIBS+= -lrosconsole_print
LIBS+= -lrosconsole_bridge
LIBS+= -lrosconsole_backend_interface
LIBS+= -lrosconsole
LIBS+= -lrosbag_storage
LIBS+= -lrosbag
SOURCES += main.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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 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
And my main.cpp:
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
int main(int argc, char *argv[])
{
rosbag::Bag bag;
bag.open("/home/xxx/Bureau/Developpement/ROSBag_Reader/data/dataset_table.bag", rosbag::bagmode::Read);
std::vector<std::string> topics;
topics.push_back(std::string("chatter"));
topics.push_back(std::string("numbers"));
rosbag::View view(bag, rosbag::TopicQuery(topics));
foreach(rosbag::MessageInstance const m, view)
{
std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>();
// if (s != NULL)
// std::cout << s->data << std::endl;
/*
std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>();
if (i != NULL)
std::cout << i->data << std::endl;*/
}
bag.close();
}
But when I try to run the project I get that error:
/home/xxx/Qt/5.7/gcc_64/bin/qmake -spec linux-g++ -o Makefile ../ROSBag_Reader/ROSBag_Reader.pro
g++ -Wl,-O1 -Wl,-rpath,/home/xxx/Qt/5.7/gcc_64/lib -o ROSBag_Reader main.o -lboost_system -L/usr/lib/x86_64-linux-gnu -ltf2_ros -lrostime -lrospack -lroslz4 -lroslib -lroscpp_serialization -lroscpp -lrosconsole_print -lrosconsole_bridge -lrosconsole_backend_interface -lrosconsole -lrosbag_storage -lrosbag -L/home/xxx/Qt/5.7/gcc_64/lib -lQt5Core -lpthread
/usr/bin/ld: main.o: référence au symbole non défini «_ZN3ros6HeaderC1Ev»
//usr/lib/x86_64-linux-gnu/libcpp_common.so.0d: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Makefile:214 : la recette pour la cible « ROSBag_Reader » a échouée
I don't understand where this error comes.
Did you try what the error messages suggests you to do? add -lcpp_common to your LIBS

undefined reference in Qt creator for DCMTK libraries

I'm trying to run a simple application in Qt creator using DCMTK libraries and it doesn't work as it should. I receive the following message:
error: undefined reference to `DicomImage::DicomImage(char const*, unsigned long, unsigned long, unsigned long)'
main.cpp:
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "iostream"
int main(int argc, char *argv[])
{
DicomImage *image = new DicomImage("test.dcm");
if (image != NULL){
std::cout << "hell yeah!";
}
return 0;
}
.pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2016-02-18T19:16:51
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
#------------------
CONFIG += c++11
INCLUDEPATH += "C:\Program Files (x86)\DCMTK_1\include"
LIBS += -L"C:\Program Files (x86)\DCMTK_1\lib"
LIBS +=-ldcmimgle -ldcmdata -loflog -lofstd
win32:LIBS += -lAdvapi32 -lofstd -loflog -ldcmdata -ldcmnet -ldcmimage -ldcmimgle -lws2_32 -lnetapi32 -lwsock32
I think something is wrong with .pro file but I cannot figure out. I have included the necessary libraries and still no use.. I read that for DCMTK the order of libraries is important but I think it is already in order and cannot figure out what could be. Can you provide a solution to this problem?