66Chapter3 • Compilers and Assemblers
To create static binaries, you have to use –
static
command line option with
gcc
. To
created dynamically linked output binary files, use –
shared
on the command line.
For example, if we compile the
hello.c
program used earlier in this chapter with shared
libraries, size of the output executable file is 13644 bytes (this can be further reduced using the
strip
utility discussed later in Chapter 7 of this book). However, if you compile it statically,
the size of the output binary file is 1625261 bytes, which is very large compared to the shared
binary. Note that this size can also be reduced using the
strip
utility.
To identify the dependencies of a dynamically linked binary file, you can use the
ldd
command. The following command shows that linked output file
hello
depends upon two
dynamic libraries.
[rr@conformix 4]$ ldd hello
libc.so.6 => /lib/i686/libc.so.6 (0x4002c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
[rr@conformix 4]$
If you copy
hello
to some other host, you also need to make sure that
libc.so.6
and
ld-linux.so.2
exist on the target system.
On most of the Linux systems, dynamic linking is done by default.
3.3.7Compiling Source Code for Other Languages
As mentioned earlier, the GCC set of compilers supports many languages. It can be used
to compile programs other than C language. Following is an introduction to compiling programs
from other languages.
3.3.7.1Compiling C++ Code
C++ source code files have suffixes such as .C, .cpp, .cc, .c++, .cxx or .cp. The
gcc
com-
piler recognizes these extensions and can compile C++ code. However you can also use g++ or
c++ compilers, which are part of the GCC compilers family and are installed with it. These pro-
grams invoke
gcc
with appropriate options to compile C++ code and location of class files.
Using these programs, you can also compile C++ source code files that don’t have the standard
suffixes listed earlier.
3.3.7.2Compiling Objective C Code
Objective files have suffixes such as .m and
gcc
recognizes Objective C files with that
suffix. When you compile Objective C code, you have to pass an option to the linker. This is
done using –
lobjc
. By this option, the linker uses Objective C libraries during the linking pro-
cess. Consider the following sample Objective C code (stored in
hello.m
file) to print “Hello
World” on the standard output.
#include "objc/Object.h"
@interface HelloWorld : Object
{