Widely Vision

  • Real History of Israel

  • Poll

  • Get a PDF version

  • Follow This Blog

  • November 2009
    S M T W T F S
    « Oct   Feb »
    1234567
    891011121314
    15161718192021
    22232425262728
    2930  

Archive for November, 2009

Deeper in Libraries With GCC(1)

Posted by aboelnour on November 29, 2009


peace upon you:
when you open a new file to write some C code you must to include some headers and libraries.
So in this article we will talk about this headers and libraries and how to deal with it throw the gcc with the terminal.
————————————————————-
Any Compiler consist of many parts one of this part is the linker.

To know what we mean with the linker we must know some basics about compiling stages.
when you call the compiler to compile some code it first turn your code to a thing called object file.
this object file contains the machine code which will run your machine.

you can get the object file by this

gcc -c main.c

you will notice that a file called main.o appeared.
In this stage the linker start working it takes this object file and link it to the libraries and headers which the program use it and produce an execution file ready to execute.
you can to call the linker of gcc to deal with the object file like this

gcc main.o -o a.out
This will produce an execution version of your program called a.out you can run it:

./a.out

you can merge more than one source file in one object file:

gcc main.o inlet.o outlet.o genspru.o -o spinout

Object Files in a Static Library:

Object files can be stored in a static library and linked from there in much the same way as they can be linked from separate files, except the linker will automatically search through the contents of the library and include only the object files that are necessary.

If nothing in an object file is referenced from inside the program, it is not included as part
of the executable.

A static library containing object files is known as an archive file, and it’s constructed
and maintained by a utility named ar. The name of an archive file normally has a
prefix of lib and a suffix of .a.the following sequence of commands compiles three
object files and stores a copy of them in a library named libspin.a. Then the linker
uses the object file named main.o and the contents of the library to construct an executable program named spinner:

$ gcc -c inlet.c outlet.c genspru.c
$ ar -r libspin.a inlet.o outlet.o genspru.o
$ gcc main.c libspin.a -o spinner

The first gcc command produces the three object files that are inserted into the static
library by the ar command. The last command compiles main.c into main.o and then
invokes the linker, which reads the contents of libspin.a to try to resolve external
function and data references made in main.o. A module stored in libspin.a is
included as part of the final executable file only if it contains a function or data item
referred to from a module that has already been included as part of the executable.

Inside the static library, along with the object modules, is an index that lists all the
names of global data and functions defined in the library. The linker uses this index to
determine which modules to include and which ones to ignore. Normally, this index is
created by the ar utility when the library is created or updated, but options are available
on the ar utility that can suppress the creation of the index. This can be useful when
maintaining a large library—multiple changes can be made without bothering to update
the index until the modifications have been completed. To create an index or to update
an existing index, you can use the ranlib utility. For example, the following pair of
commands use the -q option of ar to quickly append files to an existing archive without
updating the index, and then it uses ranlib to update the index to reflect the current
status of the archive:

$ ar -q lib.a file1.o file2.o file3.o
$ ranlib lib.a

The order of appearance of the modules in the library can make a difference. If the
same symbol is defined in more than one module, then the linker will find and include
the first module if it is looking for that symbol.
————————————————————-
In this article we talked about the static libraries.
In comming articles isa we will talk about the dynamic libraries and how to deal with it.
any feedback is welcomed
best wishes,
aboelnour.

Posted in C/C++, linux | Leave a Comment »

 
%d bloggers like this: