Important takeaways

  • C++ programs have a particular structure that must be followed
  • C++ files need to be compiled into executables that the computer can run
  • To compile a program, use the g++ program from a command line interface (CLI)
  • To run a C++ executable named my-program from the command line, do: ./my-program

Contents

Background

In order to instruct a computer how to do something, we need a language that we humans can read and write and the computer can process. Such a language is called a programming language and there are lots and lot and lots of them; Wikipedia has a very incomplete list of them here. We use programming languages to create programs, which are a set of instructions a computer needs to follow to complete some task. We also refer to pieces of a program as computer code, or just code.

There are many analogies that one can use to explain a program to someone unfamiliar, many of which break down in the details, but are useful in spirit. One such analogy is to think of a program like a recipe. A recipe is nothing more than one cook's instructions to another about how to prepare a meal or dessert. Leave a step out or carry out the steps in the wrong order, and your souffle might collapse or your bread won't rise. However, recipes rely on some amount of assumed knowledge and often involve inferred steps—this is why recipes don't always turn out exactly the same when carried out by two different people. If we were to give instructions to a computer in the same way as a recipe, the computer wouldn't be able to fill in all the gaps that you and me as humans can. So, we have to be very specific and tell the computer exactly what to do, though we can make use of other programs and libraries of existing code routines to carry out a task.

There are high level languages that make it easier for us humans to read and write a program. There are also low level languages that can be difficult to read and write, but allow us to program hardware more directly and have more control. Programs of any level must be, one way or another, converted to machine code, which is a series of 1's and 0's that your computer's central processing unit (CPU) can interpret as instructions. CPUs are, at their heart, rather simple devices and can really only do a few things. They mostly move data around in memory and add numbers (they can't event multiply—that's done by repeatedly adding!). To write a full program for a CPU by hand would be incredibly tedious. So, it's generally much easier for us to write programs in a high level language, and then convert it to the language of the CPU.

C++ is a high level, compiled programming language. To convert our C++ program to machine code, we use a program called a compiler. We call the machine code file that the compiler produces an executable. On Windows machines, these often time have a .exe extension.

One caveat is that your C++ programs have to be compiled separately on different operating systems (which have different libraries available to them) and CPU architectures (which support different instruction sets). So if you compile a program on a Windows machine and then try to run the compiled executable on a Mac, it will fail.

(Back to top)

The structure of a C++ program

To write a simple C++ program, we need an editor (such as Sublime Text or Atom), and to run it, we need a compiler. For the latter, see these instructions for getting a compiler.

Once you have an editor and your compiler installed, create a new file in your editor. Save it to a directory for this class and name it hello-world.cpp.

The structure of a C++ program.

At the top is the header. This consists of library imports (e.g., the line that start with #) and other statements, such as line 3. At the bottom is the footer—we don't currently have anything in there. Finally, all the stuff in between is called the body.

C++ programs are processed by the compiler from top to bottom. Any libraries we need have to be imported before we can use them in the code, so that's why they go at the top of the file. Now, let's go through this program line by line. On line 1 we see a comment. Comments are text that the computer will completely ignore. In C++, we have two ways of writing comments. The first is the one demonstrated in the example above. Everything on a line after two forward slashes will be ignored. The other method starts a comment with /* and closes it with */. For example, /* Everything in here is commented out. */. The advantage of the second method is that it can be used for multi-line comments. For example:

Examples of comment styles in C++.

Moving back to \ref, lines 2 and 3 are part of the header. Line 2 is something called a preprocessor directive. We'll see what that means in a little bit. The next line is something that saves us a little time. Without line 3, line 8, which reads as cout << "Hello world!"; would have to be written as std::cout << "Hello world!";. For the curious, std (short for, and pronounced as, standard), is an instance of a namespace. A namespace is the label given to a group of functions and variables (we'll learn what those are in the coming weeks). For this example, and in most examples in the future, we are interested in the std namespace because it gives us access to really useful functions, such as cout, which lets us display text on the screen (as you'll learn in a couple chapters).

On to the body of the program. Line 6 is the start of the main function. As mentioned before, we'll learn more about functions in a few weeks. For now, just know that the code within the body of main (everything between the curly braces) will be executed in order from top to bottom when the compiled executable is run. The first line of main (line 7) displays the text "Hello world!" to the screen. The next line exits out of the main function and therefore out of the program.

After the body of the program, we have the footer. However, there is no footer in this particular program—most single-file programs will not have footers; once you have two or more files involved, however, footers become useful.

(Back to top)

Compiling a program

Before we actually take a look at how to compile, let's first look at the compilation process from a high level. A reasonably detailed view of what happens is represented in \ref. This is overwhelming, I'm sure, so don't sweat it—you don't need to understand everything that's going on just yet. Let's look at it step-by-step. The first thing that happens is that we take the source code file, e.g., the content of \ref saved in a file, and we run it through a preprocessor. The preprocessor looks for all of the lines in the source file that start with # and does something based on what follows. For instance, line 2 from \ref is #include <iostream>. When the preprocessor sees this, it copies code from a library stored on your computer (stored in a file called iostream.h) and combines it with the code from your source code file. Usually, you will only be including a header file, which describes what code a library makes available, but not the actual code to make that happen. The code that makes things happen is incorporated in a later step. There are lots of other types of preprocessor directives, but for now, I'm only going to mention this one.

The complete compile process for a C++ program.
C++ source file1. C++ Preprocessor2. C++ CompilerAssembly code

3. AssemblerObject code4. LinkerExecutable

In step 2 of the compilation process, the C++ code generated after running the preprocessor is passed to the C++ compiler. The compiler takes all of the code and translates it into something called assembly code. Assembly code is a set of instructions that, when converted to binary, a central processing unit (CPU) can understand. See this page for a list of examples of assembly. Step 3 takes the assembly code and converts it to machine (or object) code. This is unreadable by humans (well, it's all 1's and 0's). Finally, step 4 will link the code from your program with the the object code from libraries your code relies on (e.g., the ones associated with header files copied in during the preprocessor stage). This step produces the final executable.

That view of the compilation process is more complicated than you need to understand right now. So, lets take a look at a simplified version:

A simplified view of the C++ compilation process.
C++ source fileC++ CompilerExecutable

For now, this is the level you should be comfortable with. The source code file goes into a program called the C++ compiler and it outputs an executable.

Note: In the directions that follow, we assume that you have a C++ compiler installed and have access to a terminal of some sort with a command line interface (CLI) (e.g., ConEmu on Windows, Terminal on OSX, or gnome-terminal on Linux).

To compile \ref, first save it to your a location on your computer. You can do this by using a text editor (e.g., Sublime Text, Atom, or jEdit), opening a blank file, pasting the code from the program up above, then saving it. For the compilation example, we assume you've named it hello-world.cpp. Whatever you name it, make sure to use a .cpp extension. While it is not strictly required, it is a convention that is universally followed.

Open a command line terminal and navigate to the location of your C++ file. See this page for information about navigating through the CLI. You can verify that you are in the same directory as your file by using the ls command. You should see something like this (the $ at the beginning is just to show you where the command prompt is, you shouldn't actually enter that as part of the command; from now on, anything colored like this is text you should enter and everything else is stuff printed to the screen by the terminal or a program):

$ ls
hello-world.cpp

To compile, we will use the program g++. This program takes a few command line parameters. One parameter is the name of the source code file. In our case, that's hello-world.cpp. It also lets you specify the name of the output file (without doing so, it'll create a file called a.out or a.exe). To do this, you need to provide a flag -o followed by a space and then the name of the desired output file, usually this should be the name of the source file but without the .cpp extension. The full command will look something like this:

$ g++ hello-world.cpp -o hello-world

Now when we do an ls, we should see a file—our executable—called hello-world (on Mac/*nix systems) or hello-world.exe (on Windows):

$ ls
hello-world.cpp  hello-world
(Back to top)

Running an executable

Running, or executing, an executable on the command line is easy: we just need to type ./ followed by the executable's name. For our example program, it will display the text "Hello world!" on the command line when executed:

$ ./hello-world
Hello world!

You can also run executables on OSX or in Windows by double clicking them in Finder/Windows Explorer. However, what you will see is a box quickly appear and then disappear, and nothing else. What happens is that your computer opens a new terminal, runs the executable, which may output text to the terminal, and then, once the executable is finished running, exits the terminal. This all happens super quick, so you never have time to see the fruits of your labor! So, rather than resorting to double clicking, for now just execute from the command line.

(Back to top)