3.2. Compiling and Running Programs


Machine language

You will be writing programs in C++. But a computer cannot directly run a C++ program. First, the program needs to be translated to machine language, which is the language that the computer's processor understands. A tool that does that translation is called a compiler.

There are two kinds of machine language file.

  1. An executable file is a complete program in machine language. 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 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.

Machine language files are also called binaries since they use binary notation. They do not contain text that you can read.


Compiling a C++ program

Use command 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, which include the following.

-o file

Write the machine language program into file file.

-Wall

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

-c

Do not create an executable file. Instead, create an object file. If no output file name is given, 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.


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 program? Answer