3.3. Compiling and Running Programs


Machine language

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 in 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. They do not contain text that you can read.


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.


Compiling a C++ program

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

  g++ prog.cpp
translates prog.cpp from C++ machine language. By default, it writes a file called a.out. You can change that using command line options, written between g++ and the program name. Options include the following.

-o file

Write the machine language program into file file. If you do not provide a name, the machine-language program will be called a.out.

-Wall

Turn on all commonly used warnings. This is highly recommended.

-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 -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 -g -c prog.cpp
creates object file prog.o, ready to be linked to other object files. You cannot run prog.o by itself.


Running a program

The file name of an executable program is also a command to run that program. So if you compile hailstone.cpp using command

  g++ -o hailstone hailstone.cpp
then g++ writes file hailstone. Command
  hailstone
runs the program.


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 for some reason control-C does not work, there are more involved ways to stop it. You might need to start another terminal. Command

  ps u
shows all processes that you are currently running. Find the one that is your program. It has a process number. If n is the process number, then command
  kill n
will usually stop it. If it doesn't, then
  kill -9 n
will always stop it.


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