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 )
( to create a relocatable one.o )
$ gcc -c -fpic two.c
( to create a relocatable two.o )
( 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
main()
{
( 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
- 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
easy to under stand the concepts,good explanation..thanku for posting this artical
ReplyDeletevery very thanks you.
ReplyDeleteAdding info to the post:
ReplyDelete-------------------------------------------------------------
/* 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
it is easy to under stands.
ReplyDeletevery very thanks.
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.
ReplyDeleteOption Tips