Wednesday, January 26, 2011

How to Create Dynamic Library in Linux


Steps In Creating Dynamic Library
Click here to understand compilation stages in Linux

Step1: implement the library source. Let us assume there are two files namely one.c and two.c each implementing one function as follows
$ vim one.c
#include<stdio.h>
void fun1(){
printf("This is function 1\n");
}
~
$ vim two.c
#include<stdio.h>
void fun2(){
printf("This is function 2\n");
}
~
Step2: compile the sources to generate relocatables. Relocatable binary is created in two forms
position dependent : in this relocated code is bound to offset
position independent : in this relocated code is not bound to offset, because of which sharing of libraries among multiple applications becomes easy.
Preferably we compile them to get position independent relocatables because of some advantages (will cover latter on).
Note: ‘.so’ is the extension for dynamic libraries.

$ gcc -c -fpic one.c 
        ( to create a relocatable one.o )
$ gcc -c -fpic two.c 
       ( to create a relocatable two.o ) 
$ gcc -shared -o libmyown.so one.o two.o
        ( libmyown.so is the name of the dynamic library to be created )
         - shared flag used to create a shared library 

$ vim mylib.h
void fun1();
void fun2();

to make use of the library above created write a test application as follows.
$ vim test.c

#include <stdio.h>
#include <mylib.h>

main()
{
    printf("This is test to create own library\n");
    fun1();/*call to function */
    fun2();/*call to function */
}

Now compile the source file as follows.

$ gcc -I ./ test.c -o testdynamic ./libmyown.so 
         - I option to let the compiler to check for "mylib.h" in current working directory
         
$ ./testdynamic
This is test to create own library
This is function 1
This is function 2

                                   
Please leave comment :-)                                                Queries are at free of cost

6 comments:

  1. easy to under stand the concepts,good explanation..thanku for posting this artical

    ReplyDelete
  2. Adding info to the post:

    -------------------------------------------------------------
    /* one.c */
    #include
    int mymult(int a, int b)
    {
    return a*b;
    }

    -------------------------------------------------------------
    /* two.c */
    #include
    int myadd(int a, int b)
    {
    return a+b;
    }
    -------------------------------------------------------------
    /* mylib.h */

    #ifndef MYLIB_H
    #define MYLIB_H

    int mymult(int a, int b);
    int myadd (int a, int b);

    #endif /* MYLIB_H */
    -------------------------------------------------------------
    /* test.c */

    #include
    #include "mylib.h"

    int main()
    {
    printf("Example: Dynamically linked at run time but statically aware\n");

    printf("Sum of 3,4 = %d\n", myadd(3,4));
    printf("Product of 3,4 = %d\n", mymult(3,4));
    return 0;
    }
    --------------------------------------------------------------------
    $ gcc -c -fpic one.c
    $ gcc -c -fpic two.c
    $ gcc -shared -o libmine.so one.o two.o
    $ gcc ./test.c -o test ./libmine.s
    $ ./test

    Output:
    Dynamically linked at run time but statically aware.
    Sum of 3,4 = 7
    Product of 3,4 = 12

    ReplyDelete
  3. it is easy to under stands.
    very very thanks.

    ReplyDelete
  4. Hi there would you mind stating which blog platform you're using? I'm going to start my own blog soon but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something completely unique.
    Option Tips

    ReplyDelete