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
make
and Makefiles is presented
in Chapter 5.
3.3.1Simple Compilation
Consider the following C source code file, which is named
hello.c
. We shall frequently
refer to this program in this as well as coming chapters.
#include
main ()
{
printf("Hello world\n");
}
To compile this file, you just need to run the following command.
[rr@conformix 4]$ gcc hello.c
[rr@conformix 4]$
By default, this command will generate an output file named
a.out,
which can be exe-
cuted on the command line as follows:
[rr@conformix 4]$ ./a.out
Hello world
[rr@conformix 4]$
Note that regardless of the name of your source code file, the name of the output file is
always
a.out
. You may actually know what an ordinary
a.out
file is, but this isn't one of
them. It is an
elf
file, despite its name. If you want to create an output file with a different
name, you have to use the
o
command line option. The following command creates an output
file with name
hello
.
gcc hello.c -o
hello
As you may have noted, the above commands do both the compiling and linking processes
in a single step. If you dont want to link the program, then simple compilation of
hello.c
file
can be done with the following command. The output file will be
hello.o
and will contain
object code.
gcc c
hello
.c
Note that both
c
and
o
command line switches can be used simultaneously. The follow-
ing command compiles
hello.c
and produces an output file
test.o
which is not yet linked.
gcc c hello.c -o test.o
Usually when you compile many files in a project, you dont create
a.out
files. Either
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.
Next Page >>
<< Previous Page
Back to the Table of Contents