Compiling a Program59
is used. These Makefiles contain information about how the compiler will be invoked and what
command line switches will be used. Information about GNU
and Makefiles is presented
in Chapter 5.
Consider the following C source code file, which is named
. We shall frequently
refer to this program in this as well as coming chapters.
To compile this file, you just need to run the following command.
[rr@conformix 4]$ gcc hello.c
By default, this command will generate an output file named
which can be exe-
cuted on the command line as follows:
[rr@conformix 4]$ ./a.out
Note that regardless of the name of your source code file, the name of the output file is
. You may actually know what an ordinary
file is, but this isn't one of
them. It is an
file, despite its name. If you want to create an output file with a different
name, you have to use the –
command line option. The following command creates an output
file with name
gcc hello.c -o
As you may have noted, the above commands do both the compiling and linking processes
in a single step. If you don’t want to link the program, then simple compilation of
can be done with the following command. The output file will be
and will contain
Note that both –
command line switches can be used simultaneously. The follow-
ing command compiles
and produces an output file
which is not yet linked.
gcc –c hello.c -o test.o
Usually when you compile many files in a project, you don’t create
you compile many files into object files and then link them together into an application or make
executables with the same name as the source code file.