C++ L01 – Setting up and Hello World

Video game development is mostly done in C++. A reason behind this is that C++ is close enough to the hardware, that the needed performance can be achieved. Of course there are disadvantages to using C++, since it is not by any means a perfect language. That said, having knowledge of a language like C++ will provide you with valuable insight and a lot of it can be transferred to other languages and environments such as Java and C#.

In order to be able to program in any programming language, it is vital to setup an (efficient) environment. This lesson is here to get you up to speed with programming with C++.

TL;DR; Download Code::Blocks and move on to the Hello World section

For windows make sure to select the mingw option

For Linux you may have to install g++ run in a terminal:

sudo apt-get install g++

Introduction to how computer programming works

Computers work by processing binary signals: on and off/zeroes and ones/High and Low voltage. In order to make a computer do something for you, you have to feed it with the appropriate sequence of zeroes and ones. This kind of sequence is called machine code; code that a machine can execute.

As you can imagine, programming computers that way is very cumbersome, takes a lot of time and it is very error prone. In the very old days when computers spanned entire rooms, they were “programmed” by altering their electrical circuits.

To make things easier, so programmers don’t have to remember 0 and 1 sequences for the processor instructions, assembly was introduced. With assembly instead of writing something like this

0110 0110 1011 1000 0011 0100 0001 
0010 0110 0110 1000 1001 1100 0011

you can write something like this:

mov ax, 0x1234
mov bx, ax

Much easier but still a lot of trouble.

That is why we have moved to high-level programming languages like C++. In high level programming languages, a program is written using natural language keywords and some basic rules. Computers cannot of course “understand” high-level programming languages, so there are a couple of ways that you can “translate” your code to be understood by the machine. In the end, the processor nevertheless processes a signal of high and low voltages.

C++ uses a compiler to translate your code into object code. Then the linker combines this object code with libraries to produce an executable program that your computer can run!

A good starting environment for C++ development is Code::Blocks.

What is Code::blocks? Code::blocks is an IDE (Integrated Development Environment). IDE means that it includes everything you need to write, build, run and debug programs. Other IDEs you can try are Eclipse and Visual Studio.

Alternatively, you can use a text editor such as VSCode, Notepad++, Sublime Text and Vi to author your code. Then using a makefile and the command line you can build your programs.

Hello world

Printing hello world to the terminal is the most basic program in most programming languages. We are going to write such a program so we can test that our environment works.

Run Code::Blocks and select from the menu:
File > New > Project…

The following dialog should pop-up
codeblock_new_console_application
pick console application

You can ignore this slide in the project wizard
01_wizard_0

Select C++ as your language
02_wizard_02

Give your project a name and path
03_wizard_03

Select your compiler. Under normal circumstances you can leave this at the defaults
04_wizard_compiler

Your project is now ready!
Go ahead and open the main.cpp file using the sidebar
05_hello_world

This is your hello world program, already filled out using the project defaults. To run it you can either press Ctrl+F10 or click on the run icon in the toolbar
run_tool_bar

You should see something similar to this
06_terminal

As a bonus, change “world” to your name like this

#include <iostream>

using namespace std;

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

and re-run the program

What is actually happening?

When you run your program through your IDE, in the background it uses command line programs, first the compiler on every source code file and then the linker to generate your executable. Then it uses another command line program to run it. An IDE is a program that manages project configurations and coordinates other programs that do the heavy lifting.

If we wanted, we could do the same ourselves via the command line. Linux example below

cd hello
g++ main.cpp -o main.exe
./main.exe

Next time we are going to take a closer look to the hello world program.

Author: Lefteris Chatzipetrou

Lefteris is a Co-Founder and CTO over at HAM Systems. He has wide experience in electrical engineering including electronics and embedded systems, mobile, web services and video games development.

Leave a Reply

Your email address will not be published. Required fields are marked *