DEV Community

Cover image for How to get started with ObjectBox database in C++
ObjectBox.io for ObjectBox

Posted on • Updated on

How to get started with ObjectBox database in C++

This tutorial is for anyone new to C++ who wants to get started with ObjectBox on Windows. It will help you install all the development tools needed to get going.

ObjectBox Database is a high-performant embedded database with integrated Data Sync to make it easy for app developers to store and sync data on and across embedded devices / edge devices in Mobile and IoT projects. By storing data systematically, you will always be able to access, manipulate and search for different entries in your app with ease.

We will start with setting up a Linux subsystem (WSL2) and installing some build tools, as well as CMake and Git. Then, we will compile ObjectBox and run a simple example app in Visual Studio Code.

Windows Subsystem for Linux (WSL2)

In this section, you will set up a simple Linux subsystem that you can use to build Objectbox in C++.

  1. Install WSL (Note: this requires a reboot; it also configures a limited HyperV that may cause issues with e.g. VirtualBox).
    Warning: to paste e.g. a password to the Ubuntu setup console window, right-click the title bar and select Edit → Paste. CTRL + V may not work.

  2. (optional, but recommended) install Windows Terminal from Microsoft Store and use Ubuntu from there (does not have the copy/paste issue, also supports terminal apps better).

  3. Choose Ubuntu from the dropdown menu in the Windows Terminal to open it in another tab.

Screenshot of the dropdown menu in Windows Terminal

  1. Get the latest packages and upgrade:
sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode
  1. Install build tools:
sudo apt install build-essential git cmake ccache gdb
LLVM_VERSION=12
sudo apt install clang-$LLVM_VERSION clang-tools-$LLVM_VERSION clang-format-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-$LLVM_VERSION 1000
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++ 1000
sudo update-alternatives --config c++
sudo update-alternatives --config clang++
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-$LLVM_VERSION 1000
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang 1000
sudo update-alternatives --config cc
sudo update-alternatives --config clang
cc --version
c++ --version
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-$LLVM_VERSION 1000
sudo update-alternatives --install /usr/bin/scan-build scan-build /usr/bin/scan-build-$LLVM_VERSION 1000
sudo update-alternatives --install /usr/bin/ld ld /usr/bin/ld.lld-$LLVM_VERSION 50
sudo update-alternatives --install /usr/bin/ld ld /usr/bin/ld.bfd 10
sudo update-alternatives --config ld
ld --version
Enter fullscreen mode Exit fullscreen mode

Install and compile ObjectBox C++ database using CMake

Now let's complete our setup by installing some useful extensions in Visual Studio Code. After this we can go straight into installing ObjectBox.

  1. Within Ubuntu, make a new directory for our example and open it in Visual Studio Code:
mkdir objectbox-ex
cd objectbox-ex
code .
Enter fullscreen mode Exit fullscreen mode
  1. Install the following extensions:
    Remote - WSL
    C/C++
    CMake Tools

  2. Create CMakeLists.txt – a standard file for installing CMake projects. It is needed to get the ObjectBox source code from its Git repository and link the library to your project.

include(FetchContent) 
FetchContent_Declare( 
objectbox 
GIT_REPOSITORY https://github.com/objectbox/objectbox-c.git 
GIT_TAG v0.14.0 
) 
FetchContent_MakeAvailable(objectbox) 
add_executable(myapp main.cpp) 
target_link_libraries(myapp objectbox)
Enter fullscreen mode Exit fullscreen mode
  1. Create a main.cpp file with the following contents to verify the setup:
#include "objectbox.hpp"
int main() {
printf("Using ObjectBox version %s\n", obx_version_string());
}
Enter fullscreen mode Exit fullscreen mode
  1. Follow this guide for VS Code to select Clang as the compiler, configure and build ObjectBox. As a result, .vscode and build folders will be generated. This is how your directory should look like at this point:

Project directory showing the folders generated on build.

Run the tasks-list example

Let's now run a standard ObjectBox example to see the database in action.

  1. Click “Select target to launch” on the status bar and select myapp as the target. Then click “launch”: the app will output the correct ObjectBox version as shown in the screenshot.

select target to launch in VS code
launch selected target in VS code

  1. Now you can run objectbox-c-examples-tasks-cpp-gen: a simple To-Do list app. Try exploring its functionality by looking at the source code to get a better idea of how ObjectBox database is used to store, get, and mark tasks as done. Here is what the output of the app looks like:

ObjectBox task-list example app output

Done! Now you have everything you need to start working with ObjectBox. Check out the ObjectBox C/C++ API docs to find more about what you can do with this library. We'd love to see what you build, so don't be a stranger - share your projects and ideas with us via our Twitter and GitHub. You'd also help us a lot by sharing your opinion via this Feedback Form. Thanks!

Top comments (0)