Adding ObjectBox to Qt application - c++

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.

Related

Qt generated project code finds shared library file but still getting undefined references during build

I was given source code for a third party library for controlling a printer. I built a shared object from that. I was able to successfully use that shared object to run a simple program in C. I tried to integrate it with an existing Qt GUI that I built, but I was getting undefined reference errors. So, I made a new, clean Qt project, but I'm still getting the following errors:
/usr/bin/ld: main.o: in function `test_printer(int, char const**)':
/home/user/printer-test/debug/../printer-test/main.cpp:142: undefined reference to `sii_api_open_device(void**, char*)'
/usr/bin/ld: /home/user/printer-test/debug/../printer-test/main.cpp:143: undefined reference to `sii_api_close_device(void*)'
The .pro and main.cpp are:
printer-test.pro
QT += quick
CONFIG += c++17
HEADERS += sii_api.h
SOURCES += main.cpp
RESOURCES += qml.qrc
TRANSLATIONS += printer-test_en_US.ts
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# Qt generated the following lines when given my library path
unix:!macx: LIBS += -L$$PWD/printer_lib/ -lsii
INCLUDEPATH += $$PWD/printer_lib
DEPENDPATH += $$PWD/printer_lib
# I added this one afterward
unix:!macx: LIBS += -lrt
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "sii_api.h" //< Library header
void test_printer(int argc, const char *argv[])
{
SIIAPIHANDLE hSiiApiHandle = nullptr;
sii_api_open_device( &hSiiApiHandle, const_cast<char *>(argv[2]) );
sii_api_close_device( hSiiApiHandle );
}
int main(int argc, char *argv[])
{
//<Qt setting up the gui and engine>
const char* args[] = {"", "0", "/dev/ttyUSB0"};
test_printer(3, args);
return app.exec();
}
The qmake generates a Makefile that looks something like this:
<snip>
LIBS = $(SUBLIBS) -L/home/user/printer-test/printer-test/printer_lib/ -lsii -lrt <snip>
printer-test: $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
<snip>
I was able to get the library linked with a sample C program given to me by the manufacturer. Here is the Makefile I used for the C program:
PROGRAM = sample
all::
gcc $(PROGRAM).c -o $(PROGRAM) -L ./printer_lib -lsii -lrt
clean::
rm -f $(PROGRAM) *.o
As seen in the sample Makefile, I needed to include the rt library as well. The Makefile did not work unless the rt library was included after the sii library. Still, my Qt program does not link.
Trying to compile the sample code as a C++ program instead of a C program gave me the clue I needed.
I needed to include extern "C" around the API header.
My Qt program compiles now.

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

SDL2 undefined reference sdl_Main in QtCreator

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

Undefined reference error while compiling Qt hello world program

I am trying to compile a Qt hello world program. I am using Qt 5.2.0 on Windows 64 bit.
This is my code
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hello world";
return a.exec();
}
When i run the application i get the following run time errors:
mingw32-make[1]: Entering directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I..\QtTest -
I"D:\Qt\5.2.0\msvc2012_64\include" -I"D:\Qt\5.2.0\msvc2012_64\include\QtCore" -I"debug" -
I"." -I"D:\Qt\5.2.0\msvc2012_64\mkspecs\win32-g++" -o debug\main.o ..\QtTest\main.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\QtTest.exe debug/main.o -
LD:\Qt\5.2.0\msvc2012_64\lib -lQt5Cored
debug/main.o: In function `main':
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC- Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
Makefile.Debug:77: recipe for target 'debug\QtTest.exe' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
mingw32-make[1]: Leaving directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
Makefile:34: recipe for target 'debug' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug\QtTest.exe] Error 1
mingw32-make: *** [debug] Error 2
16:37:34: The process "D:\pen drive data\MinGW\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project QtTest (kit: TestLocal PC)
When executing step 'Make'
.pro file
QT += core
QT -= gui
TARGET = QtTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
I want to use Qt with MinGW .I am currently using Qt creatore for 64 bit Visual Studio 2012 compiler. Since 64 bit MinGW is not available in the downloads for 5.2.0
Can someone help me solve these reference errors?
Why would you download a VS2012 version of Qt if you don't have, well, VS2012?
Download a version that matches the exact compiler you have. If there is none, download the sources and compile it yourself.
If you want to avoid the complications of self-compilation, then VS2012 Express is perfectly adequate to develop with Qt. It's the same compiler as the paid-for versions, and if you develop using Qt Creator, then you really don't care for whatever limitations the express edition IDE might have. It works great.
Another possibility is to use the mingw build of Qt. During setup, you can enable the installation of the MinGW 4.8.0 toolchain. This gives you a complete development environment, with the IDE, the Qt headers and binaries, and a toolchain.

Inclusion of header files from include/Qt is deprecated

I've recently began working through a C++ Gui Programming with Qt 4 book.
However, I can't get past the first tutorials.
#include <QApplication>
#include <QTextEdit>
int main(int argv, char **args)
{
QApplication app(argv, args);
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
Each time I try to compile this, I get this:
C:\Qt\4.8.0\andrew>qmake -project
C:\Qt\4.8.0\andrew>qmake
C:\Qt\4.8.0\andrew>make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Qt/4.8.0/andrew'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -
DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\includ
e\QtCore" -I"..\include\QtGui" -I"..\include" -I"." -I"..\include\ActiveQt" -I"d
ebug" -I"..\mkspecs\win32-g++" -o debug\tutone.o tutone.cpp
In file included from tutone.cpp:1:0:
C:\Qt\4.8.0\include\Qt\Qapplication.h:3:10: warning: #warning "Inclusion of head
er files from include/Qt is deprecated." [-Wcpp]
In file included from tutone.cpp:2:0:
C:\Qt\4.8.0\include\Qt\Qpushbutton.h:3:10: warning: #warning "Inclusion of heade
r files from include/Qt is deprecated." [-Wcpp]
tutone.cpp: In function 'int qMain(int, char**)':
tutone.cpp:11:7: error: 'class QApplication' has no member named 'setMainWidget'
mingw32-make[1]: *** [debug/tutone.o] Error 1
mingw32-make[1]: Leaving directory `C:/Qt/4.8.0/andrew'
mingw32-make: *** [debug] Error 2
As I'm new to Qt and fairly new to compiling, I assume the error is on my end. Am I declaring the headers wrong or something?
My .pro file looks like this:
######################################################################
# Automatically generated by qmake (2.01a) Thu Jan 19 12:41:21 2012
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += tutone.h \
C:/Qt/4.8.0/include/Qt/Qapplication.h \
../include/QtGui/qapplication.h \
../src/gui/kernel/qapplication.h \
C:/Qt/4.8.0/include/Qt/Qpushbutton.h \
../include/QtGui/qpushbutton.h \
../src/gui/widgets/qpushbutton.h
SOURCES += tutone.cpp \
tutthree.cpp \
../src/gui/kernel/qapplication.cpp \
../src/gui/widgets/qpushbutton.cpp
Thanks
It's a false error due to a bug in the Qt build setup.
It's not a problem and has been logged on the Qt bug tracker
After running qmake -project you should add QT variable in the .pro file. Then you can remove these odd HEADERS and SOURCES (and link dynamically Qt libraries to your program):
QT += core gui # <-- this line
TEMPLATE = app
TARGET = ProjectNameHere
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += tutone.h
SOURCES += tutone.cpp \
tutthree.cpp \