Database tutorial for a C++ beginner

C++ data persistence beginner tutorial – get started with ObjectBox

Are you a C++ beginner looking for a C++ database that is easy to use? In this tutorial, we’ll explain how you can install ObjectBox on Windows, even if you have never worked with an external library before. Your best option might be to set up a Linux subsystem (WSL2) first. After that, we will install the build tools and some useful development software such as CMake and Git. When this setup is ready, we can jump right into installing ObjectBox and running a simple example with it. We encourage you to explore its source code to get a grasp of ObjectBox in action.

Why use the ObjectBox C++ database as a C++ beginner?

Almost any program that stores some kind of data will benefit from incorporating a database into it. By storing data systematically, you will always be able to easily access, manipulate and search for different entries. Because of its flexibility, a NoSQL database will make a great fit for your first project and will serve you well further down the road. ObjectBox is not an ORM, so you do not need to learn another programming language, and can get started in minutes. ObjectBox uses native C++ APIs, which can be intuitively understood if you have any C++ experience. Furthermore, if you want high performance or your project is created with scalability in mind, a NoSQL database like ObjectBox might be the only viable option.

Windows Subsystem for Linux (WSL2)

By following these simple steps, you will be able to run Linux on your Windows. You will then install a compiler (Clang), the software for working with external libraries (CMake, Git), and a debugger (GDB).
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 installation page in Microsoft Store
3. Choose Ubuntu from the dropdown menu in the Windows Terminal to open it in another tab.

Ubuntu in the list of terminals you can open in Windows Terminal
4. Get the latest packages and upgrade:


5. Install build tools:

Install and compile ObjectBox C++ database using CMake

In this section, we will 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:

2. Install these extensions:
Remote — WSL
C/C++
CMake Tools

3. Create CMakeLists.txt – a standard file for installing CMake projects. The following code specifies the location of the library we want to use, which in this case is located in the ObjectBox Git repository. CMake then makes its contents available and adds it to the build. It also creates an executable target called myapp. To learn more about each command and the way CMake works, head over to its official documentation.

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

5. Follow this guide 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:

The directory that you should have by following this tutorial

Running the tasks-list app example

Finally, we will run a couple of simple examples to check that our setup works correctly.
1. Click “Select target to launch” on the status bar and select myapp as the target. Then click “launch”: the app should output the correct ObjectBox version as in the screenshot.

Select mypp as the launch target in VS Code - it will help verify the database version used

"Launch" and "select target to launch" buttons on the status bar of VS code
2. Run objectbox-c-examples-tasks-cpp-gen the same way. This is a simple task list app that can store your To-Dos. If you look at the source code, you will see that it uses a Box for Task objects. Each Task has four properties: id, text (the contents of the task), date_created and date_finished. These are dictated by the objectbox-model.h file, which ObjectBox generates automatically. You can easily change existing properties or add new ones by manipulating the tasklist.fbs file, which initializes them.

Output of the task list C++ app - an example of the ObjectBox database in use

That’s it! Now you have everything you need to start incorporating ObjectBox into your project. You can find more information in the official Docs for ObjectBox C/C++ API. Otherwise, star us on GitHub and feel free to ask questions if anything is unclear.

Spread the love
Posted in News.