How to get started with ObjectBox in C++ (beginner tutorial)

Anna Ivahnenko
5 min readOct 28, 2021

--

installing wsl ubuntu and cmake to use your first external library in C++

This step-by-step tutorial is for anyone without much C++ experience who wants to get started with ObjectBox on Windows. It will help you install all the essential development tools and run a simple example. ObjectBox is a fast NoSQL database that can be used for efficient data persistence in your app.

We will start with setting up a Linux subsystem (WSL2) and installing such tools as:

  • CMake, which will generate build files from the ObjectBox source code to work on Linux
  • Git, which will download the source code from the ObjectBox repository

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).
Windows terminal download page in Microsoft store

3. To open Ubuntu in the Windows Terminal, choose it from the dropdown menu.

Dropdown menu in Windows Terminal showing how to open Ubuntu

4. Get the latest packages and upgrade by pasting this to Ubuntu:

sudo apt updatesudo apt upgrade

5. Now we can install some build tools

sudo apt install build-essential git cmake ccache gdb# install LLVM / clangLLVM_VERSION=12sudo apt install clang-$LLVM_VERSION clang-tools-$LLVM_VERSION clang-format-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION# Make clang-LLVM_VERSION the default clang, and clang the default C/C++ compilersudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-$LLVM_VERSION 1000sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++ 1000sudo update-alternatives --config c++sudo update-alternatives --config clang++sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-$LLVM_VERSION 1000sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang 1000sudo update-alternatives --config ccsudo update-alternatives --config clangcc --versionc++ --version# clang toolssudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-$LLVM_VERSION 1000sudo update-alternatives --install /usr/bin/scan-build scan-build /usr/bin/scan-build-$LLVM_VERSION 1000# lld is faster than the standard ld linkersudo update-alternatives --install /usr/bin/ld ld /usr/bin/ld.lld-$LLVM_VERSION 50sudo update-alternatives --install /usr/bin/ld ld /usr/bin/ld.bfd 10sudo update-alternatives --config ldld --version

Install and compile ObjectBox using CMake

Now that you have WSL2 and all the packages, we can open Visual Studio Code to install ObjectBox with the help of CMake.

  1. In Ubuntu, create a new directory for our example and open it in Visual Studio Code:
mkdir objectbox-excd objectbox-excode .

2. Install these extensions:

C/C++, CMake Tools and Remote — WSL extensions installed in Visual Studio Code

3. Now we need to make a file that will tell CMake to get the ObjectBox source code from its Git repository and link the library to your project. Create a text file called CMakeLists.txt with the following code:

include(FetchContent)FetchContent_Declare(objectboxGIT_REPOSITORY https://github.com/objectbox/objectbox-c.gitGIT_TAG v0.14.0)FetchContent_MakeAvailable(objectbox)add_executable(myapp main.cpp)target_link_libraries(myapp objectbox)

4. Create a simple main.cpp file that will help us verify the setup:

#include "objectbox.hpp"int main() {printf("Using ObjectBox version %s\n", obx_version_string());}

5. Follow this guide to select Clang as the compiler, configure and build ObjectBox. As a result, .vscode and build folders will be generated. Your directory should now look like this:

Running the tasks-list app example

Finally, we can check that everything works by running a couple of simple examples.

  1. Click “Select target to launch” on the status bar and select myapp from the dropdown menu. Then launch it. You should see it output the correct ObjectBox version as in the screenshot.
select myapp as the launch target in vs code
output of myapp, stating what version of ObjectBox is used

2. Before proceeding with the example, you need to download the most recent ObjectBox generator for Linux from releases. Then come back to the Windows Terminal and type

explorer.exe .

to open the current directory in Windows Explorer. Copy the objectbox-generator file in there.

3. Back in VS Code, you should now run the generator for the example code:

./objectbox-generator -cpp build/_deps/objectbox-src/examples/cpp-gen

If you get a “permission denied” error, try this to make the generator file executable for your user:

chmod +x objectbox-generator

4. Now choose objectbox-c-examples-tasks-cpp-gen as the target (same way as we did with myapp in step 1) and run it. You should see the menu of a simple to-do list app as shown in the screenshot. It stores your tasks, together with their creation time and status. Try playing around with it and exploring the code of this example app to get a feel of how ObjectBox can be used.

objectbox tasks-list example app menu

If you see a sync error (e.g. Can not modify object of sync-enabled type “Task” because sync has not been activated for this store), please delete the first line from the tasklist.fbs file and run the objectbox generator once again. Or, if you want to try sync, apply for our Early Access Data Sync. There is a separate example (called objectbox-c-examples-tasks-cpp-gen-sync) that you can run after installing the Sync Server.

Now that you have ObjectBox up and running, you can start building your own simple examples or incorporating the database into your project. Hope this helps someone! I would love to hear what you think.

Find more information in the official documentation for C/C++ APIs

--

--