01.09.2014 Views

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CH03.fm Page 66 Monday, October 7, 2002 8:33 PM<br />

66 Chapter 3 • Compilers <strong>and</strong> Assemblers<br />

To create static binaries, you have to use –static comm<strong>and</strong> line option with gcc. To<br />

created dynamically linked output binary files, use –shared on the comm<strong>and</strong> line.<br />

For example, if we compile the hello.c program used earlier in this chapter with shared<br />

libraries, size of the output executable file is 13644 bytes (this can be further reduced using the<br />

strip utility discussed later in Chapter 7 of this book). However, if you compile it statically,<br />

the size of the output binary file is 1625261 bytes, which is very large compared to the shared<br />

binary. Note that this size can also be reduced using the strip utility.<br />

To identify the dependencies of a dynamically linked binary file, you can use the ldd<br />

comm<strong>and</strong>. <strong>The</strong> following comm<strong>and</strong> shows that linked output file hello depends upon two<br />

dynamic libraries.<br />

[rr@conformix 4]$ ldd hello<br />

libc.so.6 => /lib/i686/libc.so.6 (0x4002c000)<br />

/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)<br />

[rr@conformix 4]$<br />

If you copy hello to some other host, you also need to make sure that libc.so.6 <strong>and</strong><br />

ld-linux.so.2 exist on the target system.<br />

On most of the <strong>Linux</strong> systems, dynamic linking is done by default.<br />

3.3.7 Compiling Source Code for Other Languages<br />

As mentioned earlier, the GCC set of compilers supports many languages. It can be used<br />

to compile programs other than C language. Following is an introduction to compiling programs<br />

from other languages.<br />

3.3.7.1 Compiling C++ Code<br />

C++ source code files have suffixes such as .C, .cpp, .cc, .c++, .cxx or .cp. <strong>The</strong> gcc compiler<br />

recognizes these extensions <strong>and</strong> can compile C++ code. However you can also use g++ or<br />

c++ compilers, which are part of the GCC compilers family <strong>and</strong> are installed with it. <strong>The</strong>se programs<br />

invoke gcc with appropriate options to compile C++ code <strong>and</strong> location of class files.<br />

<strong>Using</strong> these programs, you can also compile C++ source code files that don’t have the st<strong>and</strong>ard<br />

suffixes listed earlier.<br />

3.3.7.2 Compiling Objective C Code<br />

Objective files have suffixes such as .m <strong>and</strong> gcc recognizes Objective C files with that<br />

suffix. When you compile Objective C code, you have to pass an option to the linker. This is<br />

done using –lobjc. By this option, the linker uses Objective C libraries during the linking process.<br />

Consider the following sample Objective C code (stored in hello.m file) to print “Hello<br />

World” on the st<strong>and</strong>ard output.<br />

#include "objc/Object.h"<br />

@interface HelloWorld : Object<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!