format

5.28.2017

C++ Run Down Series: requirements and "hello world"

C++ Run Down Series: requirements and "hello world"

Abstract  

In this series I'll explore C++ language and how it applies to modern software development; this week I'll present the basis of the language and a few historic notes. This week we will look into what is necessary to compile a C++ "Hello world" program, and discuss one example.

What is C++ 

C++ is a language developed by Bjarne Stroustrup in 1979, that is one of the major computer programming languages in the world. Based on C, it has similar syntax with its predecessor and several other C-inspired languages.
However, unlike C, C++ is a object oriented language that supports also procedural programming and can be even adapted to a functional programming pattern, which means it is a overall specially suited language for many realms of software development from embedded system development through kernel development up to client applications.

The major users of C++ are performance-critical system developers, those who need the systems to respond in real (or near real time). Thus, we can find C++ being employed in financial trading where a single microsecond may impact the net result of a transaction (check this TED talk for a discussion on this subject; check this C++ API of a financial trader for its clients) to game industry where one has multiprocessor systems to compute and react to world changes, user input, task management all within a 30 fps limit.

These high demanding environments motivated the development of the language into a high level language with multi-paradigm support with concerns with performance.

For a wannabe game developer like myself, this is a must-learn, must-use language.
I've been fortunate to develop my skills in many projects, each with different challenges than the next, and I'll try to guide through an awesome, but also gigantic language; hopefully, by the end of this C++ course, you'll be able to write your own programs with C++, meaning that you'll know what to search for when looking to accomplish a task.

Requirements for following the series: a Linux or OSX machine (preferred) or a Windows box.
If you have Linux, I advise you to install your package manager's build tools. All distros package managers have a package that represent your distro's build tools. For most Debian-derived distros something like "apt-get install build-essential g++" would suffice.

For OS X, you'll need the XCode command line tools, and you'll be good to go. You can install the package through XCode, in the Preferences->Downloads, choosing Components, and clicking Install next to Command Line Tools. Alternatively you can use Terminal, and use the command string "
xcode-select --install".

If you use Windows you can choose one (or more) paths. The Microsoft's way would be to use Visual Studio. There was segment of the product called Visual Studio Express which was for non commercial usage. Nowadays you can get a Community Edition which is promoted as a full featured IDE.

If you use Windows but which to make cross compatible software from a free software tool set, I would recommend using MingW (with MSYS) and a open source IDE like CodeBlocks (I used this software for years, it is one hell of a IDE, props to the team). The MingW (with the MSYS) project allows you to use many open source tools like Make which are not native to Windows.

The other way you can go about this is using CygWin. CygWin project creates a Unix like environment on your Windows box, allowing you to create code that can be compiled on a CygWin-able Windows and a Unix machine.


If the difference between CygWin and MingW isn't clear, think about it this way: if you compile code with CygWin, you'll only be able to run the binary in a Windows with CygWin; if you compile code with MingW, all windows can run the binary. On the other hand, you can write fully POSIX compliant binaries with CygWin, while on MingW you'll have access to all POSIX support Microsoft mustered, and Win C++ API.

In doubt: CodeBlocks and MingW + MSYS because I can actually respond questions about those tools.

The rest of the series will present the code from a Linux's user perspective; if you have a Mac you'll probably be able to follow all the steps in order. If you have a Windows, CodeBlocks is straight forward.


So, the hello world example is overdue.

Open your terminal, and check if g++ is installed with

 
g++ --version

which should give the g++ version information if the installation was completed successfully. Open your tool of preference, either it being an IDE or a text editor (Linux users are encouraged to use vim or gvim). Lets begin. Our first program will be a "hello world" example. This is a simple, standard way, to approach a language. The main idea is to write a program that outputs a "Hello world" message.

#include <iostream>

int main() {
  std::cout << "Hello world" << std::endl;
  return 0;
}

The above C++ code has several interesting properties for such a simple program, each of which we'll explore in future posts of the series, as we'll dwell deeper into language.

In the first line, we are including part of the standard library of C++. This line gives the rest of the program access to the defined primitives in the iostream header.
The iostream header contains all the standard, cross-platform, definitions of Input and Output. It is this header file that declares the "std::cout" name we use a few lines down.

The "int main() {" line, declares and starts the definition of a function called "main". This function is the entry point of a program; execution starts at this function (unless otherwise configured) for all C and C++ programs.


The next line uses a name not declared on our program: std::cout. This names the "STandarD Console OUTput". The console is another name for the system's text terminal.
The name is also a object, a class instantiation -- we will explore object oriented programming at a later time -- which exports a way for the programmer to write to the console.
The "std::endl" is a cross platform name for the line terminator which is "\n" or "\r\n" depending on the system. This name also causes the program to write to the screen immediately .

The last instruction causes the control flow to terminate the program with the result 0.

Next week, we will start discussing variables, control flow conditionals and loops.
See you next week!