Thursday, April 24, 2008

Creating Shared Objects in Linux- A primer

Here is some information on the various nuances involved in compiling and using DSO's in linux.

Lets assume the name of the source file is libhello.c. If we wanted to create a dynamic shared object we compile the file with the following flags.
gcc -fPIC -O2 -Wall -shared libhello.c -Wl,-soname,libhello.so.1 -o libhello.so.1.0

Meaning of the compiler switches:
-fPIC => This is a compiler option that instructs GCC to create Position Independant Code.PIC is a mandatory requirement for DSO's.
-O2 => This specifies the level of optimisation to be used by the compiler.
-Wall => Generate all warnings.
-shared => This option instructs GCC to generate a shared object library.

-Wl => This is a compiler switch that is used to pass options to the linker .
-Wl,-soname,libhello.so.1 => this string sends the soname option to linker. The soname option sets the 'soname' for the shared object. The soname attribute is used by the dynamic linker at runtime to make sure the application loads the correct library.
The '1' in so.1 is used as a versioning for the API's exposed by the DSO. If we upgrade the API such that its not backward compatible anymore,we should change the number in the soname.

-o libhello.so.1.0 => generate the shared object with name as libhello.so.1.0

Symbolic links to the DSO:
After generating the so, we need to create some symbolic links.

ln -s libhello.so.1.0 libhello.so.1 ==> This link is used by the dynamic linker during runtime .
ln -s libhello.so.1.0 libhello.so ==> This link is used by the compiler when we link this shared object
as -l hello

Why should we have the so.1 and so.1.0 => The 1 in so.1 is used to version the API level changes. The extra versioning in so.1.0 is used to track any change in the DSO (this may not may not break the API compatibility).
Hence we have 2 versions.

LD_LIBRARY_PATH : Includes the directory of libhello to the LD_LIBRARY_PATH,so that the dynamic linker searches for libraries in this path.

ldconfig: The ldconfig command is used if we want to move our libraries to one of the standard system locations. This command also sets the symbolic links that we did manualy. This is a system administrator command .So ,to run this command you should login as root.


The rpath linker option: Another important linker option used during development is the -Wl,-rpath. During development, there's the potential problem of modifying a library that's also used by many other programs -- and you don't want the other programs to use the `development' library.
To resolve this, we used ld's 'rpath' option, which specifies the runtime library search path of that particular program being compiled.

From gcc, you can invoke the rpath option by specifying it this way:
-Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH)

If you use this option when building the library client program, you don't need to bother with LD_LIBRARY_PATH other than to ensure it's not conflicting, or using other techniques to hide the library.

Thursday, April 17, 2008

Movies 2008: Good Will Hunting.



I wanted to watch this movie for long. The movie was very nice.
I liked the performance of Robbin Williams and Matt Damon in the movie. The movie also has some powerful quotes. Especially the ones that Will Hunting says during his first session with MattDamon.

This movie is classic.I would recommend anyone to watch it at least once.

Movies 2008: No Country for Old Men



I read about this movie at kirukkal.com . When I downloaded the movie I didnt know much about it .Thought it would be a nail biting thriller etc.

The movie starts, and within a few scenes things get quite serious. Javier Bardem's role as Anton was extraordinary. So much so that you feel very bad/frightened each time he comes on scene.

The weapon he uses to kill ... How did the directors thought about it!

There is very little music in the movie but a morbid mood is always there.
At the end, the movie leaves a very bad taste in your stomach . Not a movie to watch on a leisurely day friends. But if you are serious about movies and you like movies like SinCity ,PulpFiction etc this one is for you.

Though I watched once, the scenes are etched in my mind. May be thats why the Coen brothers got their oscars.