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.
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,
./frogruns the executable file called frog in the current directory.
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.
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.
Use g++ to compile a C++ program. Command
g++ prog.cpptranslates 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.
Write the machine language program into file file. If you do not provide a name, the machine-language program will be called a.out. |
Turn on all commonly used warnings. This is highly recommended. |
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. |
Write information into the machine language that allows a debugger, such as gdb, to provide better information about the program. |
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.cppcreates 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.cppcreates object file prog.o, ready to be linked to other object files. You cannot run prog.o by itself.
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.cppthen g++ writes file hailstone. Command
hailstoneruns the 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 ushows 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 nwill usually stop it. If it doesn't, then
kill -9 nwill always stop it.
What is the difference between an executable file and an object file? Answer
What does a compiler do? Answer
How can you compile assn1.cpp and create an executable file called assn1, with commonly used warning requested? Answer
Suppose that assn1 is an executable file in the current directory. How can you run assn1? Answer
How can you stop a running program? Answer