<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anna Ivahnenko</title>
    <description>The latest articles on DEV Community by Anna Ivahnenko (@anna_ivahnenko).</description>
    <link>https://dev.to/anna_ivahnenko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F739746%2Fa551f622-f6ee-4036-8230-0efedac7f357.png</url>
      <title>DEV Community: Anna Ivahnenko</title>
      <link>https://dev.to/anna_ivahnenko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anna_ivahnenko"/>
    <language>en</language>
    <item>
      <title>Beginner C++ tutorial: How to install and build ObjectBox</title>
      <dc:creator>Anna Ivahnenko</dc:creator>
      <pubDate>Fri, 29 Oct 2021 12:08:30 +0000</pubDate>
      <link>https://dev.to/anna_ivahnenko/beginner-c-tutorial-how-to-install-and-build-objectbox-ld8</link>
      <guid>https://dev.to/anna_ivahnenko/beginner-c-tutorial-how-to-install-and-build-objectbox-ld8</guid>
      <description>&lt;p&gt;This guide will help anyone without much C++ experience to get started with ObjectBox on Windows. It will show you how to 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.&lt;/p&gt;

&lt;p&gt;We will start with setting up a Linux subsystem (WSL2) and installing such tools as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CMake&lt;/strong&gt; — to generate build files from the ObjectBox source code on Linux;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Git&lt;/strong&gt; — to download the source code from the ObjectBox repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, we will compile ObjectBox and run a simple example app in Visual Studio Code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Windows Subsystem for Linux (WSL2)
&lt;/h2&gt;

&lt;p&gt;1.&lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install"&gt;Install WSL&lt;/a&gt; (Note: this requires a reboot; it also configures a limited HyperV that may cause issues with e.g. VirtualBox).&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;2.(optional, but recommended) install &lt;a href="https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?activetab=pivot:overviewtab"&gt;Windows Terminal&lt;/a&gt; from Microsoft Store and use Ubuntu from there (does not have the copy/paste issue, also supports terminal apps better).&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZgfN5vns--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpuo9ihfooaepbqbabhn.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZgfN5vns--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpuo9ihfooaepbqbabhn.JPG" alt="Windows Terminal in the Microsoft Store" width="880" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.Open Ubuntu in the Windows Terminal by choosing it from the dropdown menu.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--my7xf1QW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aolac1spde4o7zjyod0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--my7xf1QW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aolac1spde4o7zjyod0.jpg" alt="Drop-down menu in Windows Terminal, through which a new tab for Ubuntu can be opened" width="370" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.Get the latest packages and upgrade:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Install build tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install build-essential git cmake ccache gdb
# install LLVM / clang
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
# Make clang-LLVM_VERSION the default clang, and clang the default C/C++ compiler
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
# clang tools
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
# lld is faster than the standard ld linker
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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install and compile ObjectBox using CMake
&lt;/h2&gt;

&lt;p&gt;Now we can open Visual Studio Code to install the build files and compile them.&lt;/p&gt;

&lt;p&gt;1.In Ubuntu, create a new directory called, e.g. &lt;strong&gt;objectbox-ex&lt;/strong&gt;, and open it in Visual Studio Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir objectbox-ex
cd objectbox-ex
code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Install these extensions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl"&gt;Remote — WSL&lt;/a&gt;&lt;br&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools"&gt;C/C++&lt;/a&gt;&lt;br&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools"&gt;CMake Tools&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eqHmlI0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ehdxyodd6ejw0tw9zi2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eqHmlI0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ehdxyodd6ejw0tw9zi2.JPG" alt="Extensions tab in Visual Studio Code, showing what needs to be installed in this tutorial: C/C++, CMake Tools and Remote - WSL " width="391" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.Now we need to make a file called CMakeLists.txt. It will tell CMake to get the ObjectBox source code from its Git repository and link the library to your project. Paste the following code there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FetchContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;FetchContent_Declare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="n"&gt;objectbox&lt;/span&gt;
&lt;span class="n"&gt;GIT_REPOSITORY&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//github.com/objectbox/objectbox-c.git&lt;/span&gt;
&lt;span class="n"&gt;GIT_TAG&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="mf"&gt;.14.0&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;FetchContent_MakeAvailable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;objectbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myapp&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myapp&lt;/span&gt; &lt;span class="n"&gt;objectbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Create a simple main.cpp file to verify the setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include "objectbox.hpp"
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Using ObjectBox version %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obx_version_string&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Select Clang as the compiler, configure and build ObjectBox by following &lt;a href="https://code.visualstudio.com/docs/cpp/cmake-linux#_select-a-kit"&gt;this guide&lt;/a&gt;. As a result, .vscode and build folders will be generated. Your directory should now look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0heBtVzd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmz0pht2aamuq8n1xolm.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0heBtVzd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmz0pht2aamuq8n1xolm.JPG" alt="Explorer tab in Visual Studio Code, showing the two new folders that were generated after a successful build" width="389" height="243"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Running the tasks-list app example
&lt;/h2&gt;

&lt;p&gt;Finally, we can check that everything works by running a couple of simple examples.&lt;/p&gt;

&lt;p&gt;1.Click “Select target to launch” on the status bar, select &lt;strong&gt;myapp&lt;/strong&gt; from the dropdown menu and then click "launch". You should see our program output the correct ObjectBox version as in the screenshot.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_OnmaH56--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwadugl87tlsfvzli94n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_OnmaH56--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwadugl87tlsfvzli94n.png" alt='"Select launch target" menu in Visual Studio Code' width="744" height="161"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4aqHm5Vg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whsrlbvx4ntmjtn4vmt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4aqHm5Vg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whsrlbvx4ntmjtn4vmt7.png" alt="Output of main.cpp, verifying the version of ObjectBox used and demonstrating that the C++ build files were generated correctly." width="103" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;explorer.exe .&lt;/code&gt; to open the current directory in Windows Explorer. Copy the objectbox-generator file in there.&lt;/p&gt;

&lt;p&gt;3.Back in VS Code, run the generator for the example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./objectbox-generator -cpp build/_deps/objectbox-src/examples/cpp-gen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you get a “permission denied” error, try this to make the generator file executable for your user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x objectbox-generator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Now choose &lt;strong&gt;objectbox-c-examples-tasks-cpp-gen&lt;/strong&gt; as the target (same way as we did with &lt;strong&gt;myapp&lt;/strong&gt; in step 1) and run it. It should output the menu of a simple to-do list app as shown in the screenshot. This app 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X7QIYozB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpcwdujvl0r9pioy5br9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X7QIYozB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpcwdujvl0r9pioy5br9.png" alt="Output of the Objectbox C++ tasks-list app example showing its menu with available commands" width="574" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: 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 &lt;strong&gt;objectbox-c-examples-tasks-cpp-gen-sync&lt;/strong&gt;) that you can run after installing the Sync Server.&lt;/p&gt;

&lt;p&gt;Now you can start building your own simple examples with Objectbox or incorporating the database into your project. Hope this helps someone! I would love to hear what you think.&lt;/p&gt;

&lt;h3&gt;
  
  
  You can find more information in the &lt;a href="https://cpp.objectbox.io/"&gt;official documentation for C/C++ APIs&lt;/a&gt;
&lt;/h3&gt;

</description>
    </item>
  </channel>
</rss>
