I have given a brief introduction to STL and its implementations. From this post on, I will focus on one of the implementations - BerkeleyDB STL. Compared to other implementations such as libc++/libstdc++, BerkeleyDB STL is unique - it is based on BerkeleyDB and leverages all the advantages of a database implementation. Thanks to the developers of BerkeleyDB STL, most of the time we can program just like we would with the common implementations mentioned before because the database-based STL is almost 99% compatible with the standard.
Well, I believe that programming should be joyful, so I will include every detail in the following chapters until you can run a “Hello World” yourself.
Firstly, we need to download BerkeleyDB from Oracle’s website. Like many other software/libraries provided by Oracle, we need to have an Oracle account to access the download page.
After successfully logging in, choose “Berkeley DB 18.1 (18.1.40)” to proceed to the next page. There are two options for platforms, Linux x86-64 and Solaris - choose Linux x86-64.
The filename may seem a bit odd, but don’t worry, wait patiently and extract the downloaded file.
1unzip V997917-01.zip -d /home/user/code
above path “/home/user/code” is the destination to the extracted files. You can modify it to wherever you want. As there are already some “hello world” examples on the project, just pick the simplest one to try on, go to “db-18.1.40/examples/stl”, then find cpp file “StlAccessExample.cpp”, here a map container of type db_map will be created an accept input from the user. At this stage, we do not need to know more details, we just want to compile it and have a quick run. Before that, we absolutely need to install BerkeleyDB in our machine. We firstly navigate to directory “build_unix” and then build it with make
wait some time and then
BerkeleyDB comes with “make uninstall” so if you do not want it anymore at some future time stamp, simply apply
you can remove it from your machine! After installation, the build files will be installed to the default path “/usr/lib”. I did not want it be installed in the default path, so I changed it to my “/opt/” path. Then, we go back to the example file “StlAccessExample.cpp”, and compile it with g++,
1sudo g++ -std=c++17 -O2 -I/opt/bdb/include/ StlAccessExample.cpp -o StlAccessExample -L/opt/bdb/lib/ -WL,-R/opt/bdb/lib -ldb_stl
In the above command, we set the optimisation level of g++ to “O2”. It is okay to just omit this parameter. You need to replace directories after “-I”, “-L”, and “-R” with yours. They are the header files search path, linker search path, and dynamic(runtime) linker search path. And do not for get the last linkage parameter “-ldb_stl” which is very important. If no issues have been triggered, it compiles successfully! Run the binary with
You will see a prompt “input”, just type some random things. When you want to end the program, type
to halt it. That’s it, we made it! From the aspects of programmers, it seems as easy as using the default STL implementation of the system(usually libstdc++). Next time, I will show how to program with BerkeleyDB STL beyond the example program.