If you want to integrate the ObjectBox-Generator via CMake (as an alternative to offline installation and pre-generation of C++ sources), use the following snippet:
# find objectbox-generator (auto-download if not found on system per default)find_package(ObjectBoxGenerator 4.0.0 REQUIRED)# generate C++ files from tasklist.fbs and compile/link with targetadd_obx_schema(TARGET myapp SCHEMA_FILES tasklist.fbs INSOURCE # Opt-in: Generate in source directory CXX_STANDARD 11 # Defaults to C++14 otherwise)
If you want to use an ObjectBox Sync variant of the library, change the list line to:
CMakeLists.txt
target_link_libraries(myapp objectbox-sync)
Using the download.sh script (on Windows, use something like Git Bash to run it)
Get the repo's download.sh and run it in a terminal:
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-c/main/download.sh)
To get the ObjectBox Sync variant of the library, pass --sync to the previous command.
Details on the download.sh script:
Creates a "download" directory and a version dependent sub directory named like "libobjectbox-0.1-some-hex-hash".
Inside the version dependent sub directory, you will find the directories "include" and "lib"/
The "lib" directory contains the binary library.
Gives you an option to install the library to /usr/lib (linux) or /usr/local/lib (macOS).
Get the library for your platform from the latest GitHub release:
https://github.com/objectbox/objectbox-c/releases/latest
You can choose between three different versions per release:core, sync and jni;
As a good starting point for C/C++ Development download "ObjectBox Core" named objectbox-<platform>-<arch>.{tar.gz,zip}.
Supported Platforms:
Linux (x86_64, aarch64, armv7hf, armv6hf), macOS (x64,arm64), Windows (x64,x86)
On Windows you might have to install the latest Microsoft Visual C++ Redistributable package (X64) to use the ObjectBox DLL.
Support for other platforms and architectures on request (i.e. QNX, armv6)
Once you setup the headers and library like this, you can already start using the ObjectBox API! Here's a minimal example to verify your setup:
main.cpp
#defineOBX_CPP_FILE // Put this define in one file only before including#include"objectbox.hpp"intmain() {printf("Using ObjectBox version %s\n",obx_version_string());return0;}
#define OBX_CPP_FILE is not strictly required in this minimal example. However, when starting with the real C++ API, it is required to have exactly one .cpp file that defines OBX_CPP_FILE right before the inclusion of the "objectbox.hpp" header.
main.c
#include"objectbox.h"intmain() {printf("ObjectBox version %s\n", obx_version_string());return0;}
If you used CMake to setup your project you can already build and execute this program. Otherwise ensure your includes and the runtime shared library (libobjectbox.so, .dylib, .dll depending on the platform) are setup correctly for your compiler and linker environment.
ObjectBox Generator
ObjectBox Generator is a tool that will help you with during development of your application (and as opposed to the ObjectBox shared library, it's not supposed to be distributed with your app).
Using the ObjectBox Generator with CMake is straightforward (after the installation via FetchContent above):
# Downloads automatically if not found on system per default)find_package(ObjectBoxGenerator 4.0.0 REQUIRED)# generate C++ files from tasklist.fbs and compile/link with targetadd_obx_schema(TARGET myapp SCHEMA_FILES tasklist.fbs INSOURCE # Opt-in: Generate in source directory CXX_STANDARD 11 # Defaults to C++14 otherwise)
As an alternative, install the objectbox-generator executable by downloading the version for your OS from releases. If you want, add it to $PATH for convenience. Alternatively, instead of downloading, you can build the generator yourself by cloning this repo and running make. To build yourself, you need a recent Go version, CMake and a C++11 toolchain.
Try running objectbox-generator -help to verify the installation and see the options.
ObjectBox uses FlatBuffers to represent objects at lower levels. It is a highly efficient binary representation that works across platforms. For advanced usage, you can opt to work with FlatBuffers directly.
If you are using the recommended CMake's FetchContent ObjectBox setup, there's no FlatBuffers setup required. You can skip this section.
To set up ObjectBox for C++ projects, you need to provide the FlatBuffers headers additionally. The objectbox.hpp header file requires it; e.g. you will find the line #include "flatbuffers/flatbuffers.h" there. Thus, you also need it when using ObjectBox Generator (see above).
The default ObjectBox mode of operation involves the Generator, which generates C++ data (entity) classes. It's a higher level abstraction which takes care of FlatBuffers internals so you don't have to.
Nevertheless, advanced users may also use FlatBuffers directly, e.g. for zero-copy data access, which can be even faster.
The ObjectBox shared library already includes FlatBuffers symbols so no additional linking should be necessary. For headers, there are the following options (chose one):
If you're using the recommended CMake setup with the FetchContent command like described above, you are already set up to work with FlatBuffers APIs. (The FlatBuffers headers are already part of the objectbox library interface include directories.)
Get the latest FlatBuffers headers and e.g. copy them into your source/include path.
Add the "external" directory from our C/C++ GitHub repository as an include path to your project. This is likely not the latest version of FlatBuffers. On the upside it is tested to work with ObjectBox.
The OBX_DISABLE_FLATBUFFERS define
For special setups, the objectbox.hpp header also allows a configuration, which does not depend on including FlatBuffers. This is a limited setup, as it does not allow putting entities created by ObjectBox Generator. It can be helpful though, e.g. if you want to verify your ObjectBox basic setup without generated entities yet as a first step. To enable this, simply add the OBX_DISABLE_FLATBUFFERS define to your C++ compiler configuration.