Building on the Command Line

Compiling Your First Program

We need to take our friendly, human-readable C++ code, and turn it into instructions that your CPU understands. Most instructions that your CPU understands are primitive – things like, “take number x, add to number y, move result to memory address z”. Here is a pictorial summary of the process we are about to perform:

Fig. 1: Compiling and linking

To demonstrate how it’s done, we’ll first need something to compile. Open up a terminal (pressing alt+enter should do the trick in the virtual machine). Then navigate to ~/tests/hello/, open your editor and create a file called hello.cpp.1 Here are the two commands to accomplish the above steps:

$ cd ~/tests/hello/
$ edit hello.cpp

Now type the following into your editor, and save the file:

#include<iostream>
using std::cout;

int main()
{
    cout << "Hello world.\n";
    return 0;
}

Go back to your terminal,2 and run the command ls. You should now see your new file:

$ ls
hello.cpp

Great. Now let’s turn that thing into a runnable program:

$ g++ -c hello.cpp

Now look at the directory contents again and note the presence of a new file, hello.o:

$ ls
hello.cpp   hello.o

This is the object code corresponding to the program you just wrote (labeled “object file” in the diagram above). The file hello.o contains the tiny instructions that make sense to the computer, but does not yet have the form of a complete working program that the OS can run, and only contains references to many functions that we used from elsewhere, e.g. all the stuff that makes cout work. Referring again to the diagram, this is where the “library files” come in.3

To put everything together into a runnable program (“everything” meaning our object code and stuff we are borrowing from libraries), we must invoke the linker. This can be done as follows:4

$ g++ hello.o -o hello

This will pull in all the needed references together with our object file and package it into an executable file called hello. Run ls yet again, and you should see it sitting there (should be colored green):

$ ls
hello  hello.cpp  hello.o

To run it, just execute $ ./hello, and you’ll see the message printed. Note: the ./ before the program name is essential, as the current directory probably is not in your search path.

Congratulations, you’ve compiled and run your first C/C++ program!

Note: in the simplest of cases, you can just run the command

$ g++ hello.cpp

This will compile and link all in one step, and produce a runnable program named a.out by default. It’s fine to use this instead, but I want the distinction between compiling and linking to be clear in your mind, which is why I’ve shown you the separate steps first.

NOTE: running g++ manually can get cumbersome, especially if you have multiple source files to compile and link together. It is common to instead use a Makefile or some other build system to run the compiler for you. After the first week or so, we will transition to using Makefiles. I won’t cover them much in class, but would be happy to discuss if you are curious.


Back to the 103 homepage


  1. If not on the virtual machine, you can make this directory using the command mkdir -p ~/tests/hello.↩︎

  2. You can alt-tab through windows. You can also kind of tile windows to one side or the other by pressing the meta key and one of the arrow keys. “Meta” should be the window key on a non-Mac…and maybe the “option” key on a mac?↩︎

  3. If you want to see the missing references, you can list them with nm -uC hello.o.↩︎

  4. It may be confusing that the same command (g++) is being used to compile and link. It turns out g++ is really just a wrapper for a handful of other tools (including a linker, called ld). The g++ command figures out what to do in part based on the arguments you give it, and by default ultimately tries to produce an executable.↩︎