Contents

Background

C++ is a high level, compiled programming language. When you write a program, you are writing a set of instructions that humans can make sense of. CPUs work at a much lower level, and so we need to convert our programs into machine code. To do this, we use a program called a compiler. A compiler takes the C++ code we write and translates it into a set of low level instructions that your CPU can run. 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 and CPU architectures. 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 jEdit) and a compiler. See these instructions for setting up an environment on your computer. In a text editor, create a new file. Save it to a directory for this class and name it helloWorld.cpp.

The structure of a C++ program.

At the top is the header. This consists of library imports (those lines that start with #'s) 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.

Now, let's go through this line by line. On line one 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++.

Okay, back to \ref. Lines 2 and 3 are part of the header. Line two 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 called 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.

On to the body. Line 6 is the start of the main function. 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 run when the code 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. There is no footer, so there is nothing more to the program.

(Back to top)

Compiling a program

Before we actually take a look at how to compile, let's first look at the compile process from a high level. What actually 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 to 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 pastes it into your file (in memory, not in the actual file you've saved). There are lots of other types of preprocessor directives, but for now, we are only going to mention this one.

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

3. C++ AssemblerObject code4. C++ 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 code from libraries you rely on. That doesn't need to make sense quite yet. 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 or PowerShell.

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 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 helloWorld.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 (i.e., a Unix/Linux terminal on Mac and *nix platforms or PowerShell on Windows), and navigate to the location of your C++ file. See this page for information about navigating through the command line. 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):

> ls
helloWorld.cpp

To compile, we will use the command g++. This command takes a few parameters. One parameter is the name of the source code file. In our case, that's helloWorld.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++ helloWorld.cpp -o helloWorld

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

> ls
helloWorld.cpp  helloWorld

or in PowerShell...

PS C:\Users\hfeild\Desktop> ls


    Directory: C:\Users\hfeild\Desktop


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          1/3/2014  10:40 PM        143 helloWorld.cpp
-a---          1/3/2014  10:40 PM      92778 helloWorld.exe
(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:

> ./helloWorld
Hello world!

You can also run executables in Windows by double clicking them in Windows Explorer. However, what you will see is a black box quickly appear and then disappear, and nothing else. What happens is that Windows will open a command prompt (which is slightly different than PowerShell), run the executable, which then outputs text to the command prompt, and then, once the executable is finished running, exits the command prompt. 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)