Book Image

Advanced C++

By : Gazihan Alankus, Olena Lizina, Rakesh Mane, Vivek Nagarajan, Brian Price
5 (1)
Book Image

Advanced C++

5 (1)
By: Gazihan Alankus, Olena Lizina, Rakesh Mane, Vivek Nagarajan, Brian Price

Overview of this book

C++ is one of the most widely used programming languages and is applied in a variety of domains, right from gaming to graphical user interface (GUI) programming and even operating systems. If you're looking to expand your career opportunities, mastering the advanced features of C++ is key. The book begins with advanced C++ concepts by helping you decipher the sophisticated C++ type system and understand how various stages of compilation convert source code to object code. You'll then learn how to recognize the tools that need to be used in order to control the flow of execution, capture data, and pass data around. By creating small models, you'll even discover how to use advanced lambdas and captures and express common API design patterns in C++. As you cover later chapters, you'll explore ways to optimize your code by learning about memory alignment, cache access, and the time a program takes to run. The concluding chapter will help you to maximize performance by understanding modern CPU branch prediction and how to make your code cache-friendly. By the end of this book, you'll have developed programming skills that will set you apart from other C++ programmers.
Table of Contents (11 chapters)
7
6. Streams and I/O

Installation and Setup

Before you embark on this book, you will need to install the following libraries used in this book. You will find the steps to install these here.

Installing CMake

We will use CMake version 3.12.1 or later. We have two options for installation.

Option 1:

If you are using Ubuntu 18.10, you can install CMake globally using the following command:

sudo apt install cmake

When you run the following command:

cmake –version

You should see the following output:

cmake version 3.12.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

If the version you see here is lower than 3.12.1 (for example, 3.10), you should install CMake locally using the following instructions.

Option 2:

If you are using an older Linux version, you may get a CMake version that is lower than 3.12.1. Then, you need to install it locally. Use the following commands:

wget \

https://github.com/Kitware/CMake/releases/download/v3.15.1/cmake-3.15.1-Linux-x86_64.sh

sh cmake-3.15.1-Linux-x86_64.sh

When you see the software license, type y and press Enter. When asked about the installation location, type y and press Enter again. This should install it to a new folder in your system.

Now, we will add that folder to our path. Type the following. Note that the first line is a bit too long and the line breaks in this document. You should write it as a single line, as follows:

echo "export PATH=\"$HOME/cmake-3.15.1-Linux-x86_64/bin:$PATH\"" >> .bash_profile

source .profile

Now, when you type the following:

cmake –version

You should see the following output:

cmake version 3.15.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

3.15.1 is the current latest release at the time of writing this document. Since it is newer than 3.12.1, this will suffice for our purposes.

Installing Git

Test the current installation by typing the following:

git --version

You should see a line such as the following:

git version 2.17.1

If you see the following line instead, you need to install git:

command 'git' not found

Here is how you can install git in Ubuntu:

sudo apt install git

Installing g++

Test the current installation by typing the following:

g++ --version

You should see an output such as the following:

g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0

Copyright (C) 2017 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If it is not installed, type the following code to install it:

sudo apt install g++

Installing Ninja

Test the current installation by typing the following:

ninja --version

You should see an output such as the following:

1.8.2

If it is not installed, type the following code to install it:

sudo apt install ninja-build

Installing Eclipse CDT and cmake4eclipse

There are multiple ways of installing Eclipse CDT. To get the latest stable version, we will use the official installer. Go to this website and download the Linux installer: https://www.eclipse.org/downloads/packages/installer.

Follow the instructions there and install Eclipse IDE for C/C++ Developers. Once you have installed it, run the Eclipse executable. If you did not change the default configuration, typing the following command in the terminal will run it:

~/eclipse/cpp-2019-03/eclipse/eclipse

You will choose a workspace folder and then you will be greeted with a Welcome tab in the main Eclipse window.

Now, we will install cmake4eclipse. An easy way to do this is to go to this website and drag the Install icon to the Eclipse window: https://github.com/15knots/cmake4eclipse#installation. It will ask you to restart Eclipse, after which you are ready to modify the CMake project to work with Eclipse.

Installing GoogleTest

We will install GoogleTest in our system, which will also install other packages that are dependent on it. Write the following command:

sudo apt install libgtest-dev google-mock

This command installs the include files and source files for GoogleTest. Now, we need to build the source files that we installed to create the GoogleTest library. Run the following commands to do this:

cd /usr/src/gtest

sudo cmake CMakeLists.txt

sudo make

sudo cp *.a /usr/lib