Chapter 0: Setting up your programming environment
C++ is a standardised programming language. This feature, enable the users to enjoy the same code in a myriad of operating systems. However, installing and using the tools in each operating systems (OS) is different, and in general, it is necessary to understand the details for each particular OS.
This book includes examples of how to set up your environment. Nonetheless, all the cases will assume that you will use GNU/Linux distribution that uses the Advanced Package Tool (APT) developed by Debian. Some of these distributions are Debian, Ubuntu and Mint.
If you use a different distribution, like CentOS or Fedora, or even an operating system entirely different from GNU/Linux like Solaris or Minix, you could use this book as well as long as you know how to install the tools that you will need for this and the next chapters — the C++ code is exactly the same.
If you are have experience installing and modifying your system, these steps would be quite easy. However, if you are a novice in GNU/Linux consider asking for help to someone experienced. Upon execution of these instructions, you might have unexpected behaviour, a cryptic message, lack of permissions or all them together. Most of the first encounters with this kind of tasks are quite frustrating and demand a lot of patience. Keep the spirit!
Installing the compiler
The C++ code needs to be transformed into something that the operating system could execute. That is the function of the compiler. We will use the GNU C++ compiler. Type in some terminal this command and then enter,
If you got a message like "Command 'g++' not found" then you need to install it. Before installing anything, make sure that your system is up to date. Execute these commands,
someuser@somechine:~$ sudo apt-get update someuser@somechine:~$ sudo apt-get upgrade
The first fetch the list of available updates and the second upgrades the current packages. If the system upgraded the kernel, then you will need to reboot. Then after, execute this command,
someuser@somechine:~$ sudo apt install g++
Verify it, execute,
someuser@somechine:~$ g++ --version g++ \(Ubuntu 7.3.0-27ubuntu1~18.04\) 7.3.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.
Installing an editor
To write code, you will need an editor. An editor like Gedit will suffice. However, using Geany would be a significant improvement. To install it, execute,
someuser@somechine:~$ sudo apt install geany
Write your first program In your editor write these lines,
std::cout << "Hello World\n";
Save this file as helloWorld.cpp. In a terminal compile this file using this command,
someuser@somechine:~$ g++ helloWorld.cpp -o hello
If there are no errors, the compiler created the executable file 'hello'. Run this program with this command,
someuser@somechine:~$ ./hello Hello World