How to get started with ObjectBox database in C++
Tags: database, beginners, tutorial, cpp
Published: 2022-03-03 14:28:37

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](https://objectbox.io/) is a high-performant embedded database with integrated [Data Sync](https://objectbox.io/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](https://docs.microsoft.com/en-us/windows/wsl/install) (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](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?activetab=pivot:overviewtab) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vep133sciom2wty8z24c.jpg)

4. Get the latest packages and upgrade:
```
sudo apt update
sudo apt upgrade
```

5. 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
```

## 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 .
```

2. Install the following extensions:
[Remote - WSL](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl)
[C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
[CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools)

3. 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)
```

4. 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());
}
```

5. Follow [this guide for VS Code](https://code.visualstudio.com/docs/cpp/cmake-linux#_select-a-kit) 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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avcmqh9ycatvd5qw2jof.JPG)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dumae3xd40rvnn9dl8q5.png)
![launch selected target in VS code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq525poqyswwvd0bq7ly.png)

2. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhju7egy82bv5uguccvx.png)

Done! Now you have everything you need to start working with ObjectBox. Check out the [ObjectBox C/C++ API docs](https://cpp.objectbox.io/) 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](https://twitter.com/ObjectBox_io) and [GitHub](https://github.com/objectbox/objectbox-c). You'd also help us a lot by sharing your opinion via this [Feedback Form](https://docs.google.com/forms/d/e/1FAIpQLSd0rNJOf9Gmkk_Ares42pWM60XrPfJ67fBhhjJ4UFjxtMTkvg/viewform). Thanks!