3.3. Compiling and Running Programs


Compiling a C++ program using g++

Use g++ to compile a C++ program. Command

  g++ -Wall -W -o name prog.cpp
translates prog.cpp from C++ machine language, creating executable program name. Option -Wall requests all normal warnings. Option -W requests a few more that are useful. Option -o gives the name that you want the executable file to have. For example,
  g++ -Wall -W -o hailstone hailstone.cpp 
compiles hailstone.cpp and calls the executable file hailstone. (In Linux, executable files normally have no extension, unlike Windows, where executable files normally have extension .exe.)


Running your program

To run file hailstone, use command

  ./hailstone

A program normally reads from the standard input (the keyboard, by default) and writes to the standard output (the terminal by default).

You can redirect the standard input or the standard output (or both) so that they are attached to files. Command

  ./hailstone <data1.txt
runs hailstone, but it reads from file data1.txt instead of from the keyboard. Command
  ./hailstone <data1.txt >out.txt
additionally causes the output from hailstone to go into file out.txt.


Using make

The make utility makes it easy to compile software. Each assignment comes with a file called Makefile. It contains instructions for make that tell it how to build a particular piece of software. The instructions consist of a sequence of targets, dependencies and commands.

Linux command

  make tgt
tells make to build target tgt, according to the instructions in Makefile. Linux command
  make
tells make to make the first target in Makefile.


Stopping a rogue program

If your program goes into an infinite loop, you will need to stop it. Type control-C. That will usually stop it. If that does not work, see killing a process.


Machine language

What follows is only important for programs that consist of two or more modules.

A computer processor runs programs written in the machine language of that processor. Machine language files are binary, and you cannot read them directly unless you have some very special skills.

There are two kinds of machine language file.

  1. An executable file is a complete machine language program. You can run it as a command simply by typing its name (or a path) as a command. If you want to run an executable file that is in the current directory, write it with ./ in front of it. For example,

      ./frog
    
    runs the executable file called frog in the current directory.

  2. An object file is a partial machine language program. It is designed to be linked to other object files to produce an executable file. You cannot run an object file by writing its name as a command.

Machine language files are also called binaries since they use binary notation.


Compilation

People do not write programs directly in machine language. Instead, they write in a much more readable form and rely on a piece of software called a compiler to translate to machine language. We will use a compiler that translates from C++ to machine language. In general, a compiler translates a source language, such as C++, into a target language, such as machine language.

G++ is described briefly above. Here are some additional options.

-c

Without this option, g++ creates an executable file. With this option, it creates an object file. If no output file name is given (by option -o), the object file name for file prog.cpp is prog.o.

-g

Write information into the machine language that allows a debugger, such as gdb, to provide better information about the program.

-O

Request optimization. The compiler improves the machine-language code to make it more efficient. This can substantially speed up the program, but it also slows down compilation.

During optimization the compiler performs dataflow analysis, in which it examines how information flows in a function definition. That allows it to detect whether a variable is used before it is initialized. So requesting optimization can lead to more useful warnings.


For example, command

  g++ -Wall -W -g -o prog prog.cpp
creates executable file prog from C++ file prog.cpp, with common warnings turned on and with debugger information written into it. Command
  g++ -Wall -W -g -c prog.cpp
creates object file prog.o, ready to be linked to other object files. You cannot run prog.o by itself.


Exercises

  1. What is the difference between an executable file and an object file? Answer

  2. What does a compiler do? Answer

  3. How can you compile assn1.cpp and create an executable file called assn1, with commonly used warning requested? Answer

  4. Suppose that assn1 is an executable file in the current directory. How can you run assn1? Answer

  5. How can you stop a running program? Answer