Tools and Environment Setup
*********************************************
.. epigraph::
The purpose of a programming language is to help express ideas in code.
-- Bjarne Stroustrup
Operating System
=================================
All the examples in this book works in `Ubuntu 20.04.4 LTS `_.
.. code-block:: bash
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
C++ Standard and Compiler
=================================
In the past 10 years, C++ has drmatically changed. The C++ standard has evolved from C++98 to C++20 in order to embrace more modern programming language features. We will use C++20 as the standard for this book. However, in the interview, the focus is on data structure and algorithm. Unless there is a special requirement for candidates' C++ familiarity, C++98 is enough for the candidate to do the work.
GNU g++, LLVM clang++, Microsoft Visual C++ are popular C++ compilers. In this book, clang++ 15.0.0 is used though it is still under development at the time of this writing [#]_.
.. code-block:: bash
$ clang++ --version
Ubuntu clang version 15.0.0-++20220225053013+37e84d9be06d-1~exp1~20220225173121.170
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
To install clang 15, add the two lines in `/etc/apt/sources.list`:
.. code-block:: bash
deb http://apt.llvm.org/focal/ llvm-toolchain-focal main
deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
Add gpg key to retrieve the archive signature:
.. code-block:: bash
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
Then use apt to update and install all necessary packages:
.. code-block:: bash
sudo apt update
sudo apt install clang lld lldb clang-format clang-tidy -y
Not all the language and library features in C++ standard are supported by compilers.
.. rubric:: Footnotes
.. [#] The latest updates are available at LLVM Debian/Ubuntu nightly packages (https://apt.llvm.org)
C++ REPL
=================================
C++ is a static language, in contrast with dynamic langues like Python. This makes it easy to write high performance program but sometimes hard to learn. Fortunately there is a REPL(read–eval–print loop) tool **cling** to help. A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. **cling** is developed by CERN(European Organization for Nuclear Research) and hosted under its `ROOT project `_, where you can download Linux and Mac version.
At the time of this writing, the latest version of **cling** is `cling_2020-11-05_ROOT-ubuntu2004.tar.bz2`. Run the following command to install **cling**:
.. code-block:: bash
wget https://root.cern.ch/download/cling/cling_2020-11-05_ROOT-ubuntu2004.tar.bz2
tar xjvf cling_2020-11-05_ROOT-ubuntu2004.tar.bz2
sudo mv cling_2020-11-05_ROOT-ubuntu2004 /usr/local
export PATH=/usr/local/cling_2020-11-05_ROOT-ubuntu2004/bin:$PATH
exec $SHELL
Now you can run **cling** in the terminal and try some C++ code.
.. code-block:: bash
:linenos:
$ cling -std=c++2a
****************** CLING ******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ #include
[cling]$ #include
[cling]$ #include
[cling]$ #include
[cling]$ namespace fs = std::filesystem;
[cling]$ fs::current_path(fs::temp_directory_path());
[cling]$ fs::create_directories("sandbox/1/2/a");
[cling]$ fs::create_directory("sandbox/1/2/b");
[cling]$ fs::permissions("sandbox/1/2/b", fs::perms::others_all, fs::perm_options::remove);
[cling]$ fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
[cling]$ std::system("ls -l sandbox/1/2");
total 12
drwxrwxr-x 2 henry henry 4096 Feb 27 10:19 a
drwxrwx--- 2 henry henry 4096 Feb 27 10:19 b
drwxrwx--- 2 henry henry 4096 Feb 27 10:19 c
- Line 1 is to launch `cling` REPL environment.
- Line 12 is to change current directory to system's temporary folder
- Line 13, 14 and 16 are to create folders
- Line 15 is to change permissions of one folder
- Line 17 is to run a system command to print the contents of one folder
The code snippet above shows the new standard does make the programmer life easier. With built-in filesystem library support in modern C++, we don't have to use third-party library to write cross-platform code.
To save the typing, we can set alias:
.. code-block:: bash
alias cling='cling -std=c++2a'
And put all the common C++ header files into one `sein.hpp`:
.. code-block:: c++
#pragma once
#include // Stream
#include
#include // STL
#include
#include
#include