Saturday, December 1, 2012

Creating Static Libraries using c on linux Fedora.

You have seen the library file  ".lib" extension on Windows and a ".a" on LINUX/UNIX.If you are unknown about what are they and how to create them then you will get answer here.
These are Static Library files, also known as archives, which are simply a collection of ordinary object files. It is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. Simply we can say it permit users to link to programs without having to recompile its code, saving recompilation time.
There are several advantages of static libraries:
1.The most significant is that the application can be certain that all its
    libraries are present and that they are the correct version. This avoids
    dependency problems.
2. Result in a significant performance improvement.
3. Can be shared between many applications leading to space savings.
4. It also allows the library to be updated to fix bugs and security flaws
    without updating the applications that use the library.

One disadvantage of static libraries is that when you run many applications at the same time and they all use functions from the same library, you may end up with many copies of the same functions in memory and indeed many copies in the program files themselves. This can consume a large amount of valuable memory and disk space. Many UNIX systems and Linux-support shared libraries can overcome this disadvantage. A complete discussion of shared libraries and their implementation on different systems is not discussion on this article.

Now, Lets start to create static library using C on fedora linux.
In this example, you create your own small library containing two functions and then use one of them in an example program.Then functions are called reader and writer and just print greetings.

First, create separate source files(imaginatively called writer.c and reader.c) for each function. And lib.h to declares the functions writer and reader for users.
Here's the first:
void writer(char *arg)
printf("writer: I have written %s\n",arg);

Here's the second
void reader(char *arg)
printf("reader: I have read %s\n",arg);

Here's the second
void reader(char *);
void writer(char *);

You can compile these functions individually to produce object files ready for inclusion into a library. Do this by invoking the C compiler with the -c option,which prevents the compiler from trying to create a complete program. Trying to create a complete program would fail because we haven't defined a function called main.

Go to the terminal , staying on directory where above files are saved type the following command.
$ gcc -c reader.c writer.c  (it compile to object file)
#ls *.o                                (to see the list of object file)
reader.o writer.o               (output on the terminal)

Now write the program that calls the function reader and writer. The calling program (program.c) can be very simple. It includes the library header file and calls one of the function from the library.

int main()
   reader("this article");
   writer("this article");
   return 0;

You can now compile the program and test it. For now, specify the boject files explicityly to the compile, asking it to compile your file and link it with the previously compiled object module reader.o and writer.o
$gcc -c program.c
$gcc -c program program.o reader.o writer.o
reader: I have read this article
writer: I have written this article

Now you'll create and use a library. Use the ar program to create the archive and add your object files to it. The program is called ar because it creates archives, or collections, if individual files placed together in one large file. Note that you can also use ar to crate archives of files of any type. (Like many UNIX utilities, ar is a generic tool.) 

$ar crv libfoo.a writer.o reader.o
a - writer.o
a - reader.o

The library is created and the two object files added. To use the library successfully, some systems, notably those derived form Berkeley UNIX, require that a table of contents be created for the library. Do this with the ranlib command. In Linux, this step isn't necessary (but it is harm-less when your're using the GNU software development tools.
$ ranlib libfoo.a

Your library is now ready to use. You can add to the list of files to be used by the compiler to create your program like this:
$ gcc -o program program.o libfoo.a
reader: I have read this article
writer: I have written this article

Finally we have created the static library libfoo.a which include two function reader and writer. 

No comments:

Post a Comment