Compiling a Program61
This means that if you use command
gcc hello.c
, GCC will consider
hello.c
as a
C program and will invoke appropriate helper programs to build the output. However, if you use
gcc hello.cpp
command, GCC will consider
hello.cpp
as a C++ program and will
compile it accordingly. You can also select a language type with a particular file using –
x
com-
mand line option. Table 3-2 lists languages that can be selected with this option.
Note that –
x
option applies to all the files that follow until you turn it off using the –
x
none
option on the command line. This is especially important when you use the GNU
make
utility discussed in Chapter 5.
By default, the object file created by GCC has
.o
extension, replacing the original exten-
sion of the file. You can create output file with a particular name using –
o
command line option.
The following command creates
test.o
file from
hello.c
.
gcc –c hello.c –o test.o
3.3.3Compiling to Intermediate Levels
The compilation process involves many steps like preprocessing, assembling and linking.
By default GCC carries out all of these processes and generates executable code as you have
seen earlier. However, you can force GCC not to do all of these steps. For example, using the –
c
command line option, the
gcc
command will only compile a source code file and will not gen-
erate executable object code. As you already know, the following command compiles file
hello.c
and creates an object file
hello.o
.
gcc –c hello.c
If you look at the type of the newly created object file using the
file
command, you can
see that this is not a linked file. This is shown in the following command output.
[root@conformix chap-03]# file hello.o
hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1,
not stripped
[root@conformix chap-03]#
Table3-2 Selecting languages with –x option.
OptionLanguage
-x c (lowercase c)C language selection
-x c++C++ file
-x objective-cObjective C
-x assemblerAssembler file
-x f77Fortran file
-x javaJava language file
Next Page >>
<< Previous Page
Back to the Table of Contents