0

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

1 Answers1

2

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.

Rawen
  • 96
  • 3
  • @luk34 please mark the question as solved, if it is. – Rawen Oct 02 '22 at 12:17
  • OK this solves the above mentioned problem. But when I continue in the tutorial and generate the objectbox-model.hpp and tasklist.obx.hpp with the objectbox-generator and include them into my main.cpp I still get undefined reference errors for the line obx::Store store(create_obx_model()); Any further ideas? –  Oct 10 '22 at 18:41