Home | Download | Documentation | Programming | FAQ | About Me
How to create your own library ?

First of all, what's library - in real world,library is collection of books.In computer terms,library has collection of functions. For example,printf() is a library function.That means,this printf functions definition is available in gcc libraries - libc.
Let's create a new directory called c-lib,

[[email protected] ~]$ mkdir c-lib
[[email protected] ~]$ cd c-lib/

create a new file called "hi.c" with a function named fun1.

[[email protected] c-lib]$ cat > hi.c
void fun1(){                    
printf("\n Fun1");              
}                               
^C   #press ctrl+C to signal end of input from keyboard.   
now create a new file called "hi2.c" with a function named fun2.

[[email protected] c-lib]$ cat > hi2.c
void fun2(){                     
printf("\n Fun2");               
}                                
^C  #press ctrl+C to signal end of input from keyboard.

So here is the two files we have created.

                             
[[email protected] c-lib]$ cat  hi.c
void fun1(){                   
printf("\n Fun1");             
}                              
[[email protected] c-lib]$ cat  hi2.c
void fun2(){                    
printf("\n Fun2");              
}

Now create a header file called lib.h

                              
[[email protected] c-lib]$ cat > lib.h
void fun1();                     
void fun2();                     

Now are going to use above two functions fun1 and fun2 in final C program named hi_main.c

[[email protected] c-lib]$ cat > hi_main.c
#include "lib.h"                     
main(){                              
fun1();                              
fun2();                              
}                                    
^C                                   
You can see both functions are called inside main(). Now compile the c program to create intermediate object file named hi_main.o
[[email protected] c-lib]$ gcc  -c hi_main.c 
[[email protected] c-lib]$ ls -l             
total 20                                
-rw-r--r-- 1 lg lg  34 2009-07-16 10:49 hi2.c
-rw-r--r-- 1 lg lg  34 2009-07-16 10:49 hi.c 
-rw-r--r-- 1 lg lg  43 2009-07-16 10:50 hi_main.c
-rw-r--r-- 1 lg lg 784 2009-07-16 10:51 hi_main.o
-rw-r--r-- 1 lg lg  26 2009-07-16 10:50 lib.h    

Now compile our function files to create .o files.

[[email protected] c-lib]$ gcc -c hi.c hi2.c
hi.c: In function â??fun1â??:              
hi.c:2: warning: incompatible implicit declaration of built-in function â??printfâ??
hi2.c: In function â??fun2â??:                                                      
hi2.c:2: warning: incompatible implicit declaration of built-in function â??printfâ??
[[email protected] c-lib]$ ls -l                                                      
total 28                                                                         
-rw-r--r-- 1 lg lg  34 2009-07-16 10:49 hi2.c                                    
-rw-r--r-- 1 lg lg 816 2009-07-16 10:51 hi2.o                                    
-rw-r--r-- 1 lg lg  34 2009-07-16 10:49 hi.c                                     
-rw-r--r-- 1 lg lg  43 2009-07-16 10:50 hi_main.c                                
-rw-r--r-- 1 lg lg 784 2009-07-16 10:51 hi_main.o                                
-rw-r--r-- 1 lg lg 816 2009-07-16 10:51 hi.o                                     
-rw-r--r-- 1 lg lg  26 2009-07-16 10:50 lib.h                                    
As you can see two new files hi.o and hi2.o are created. We finally create an executable binary using these three object files.
[[email protected] c-lib]$ gcc -o hello hi_main.o hi.o hi2.o
[[email protected] c-lib]$ ls -l                            
total 36                                               
-rwxr-xr-x 1 lg lg 6482 2009-07-16 10:51 hello*        
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi2.c         
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi2.o         
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi.c          
-rw-r--r-- 1 lg lg   43 2009-07-16 10:50 hi_main.c     
-rw-r--r-- 1 lg lg  784 2009-07-16 10:51 hi_main.o     
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi.o          
-rw-r--r-- 1 lg lg   26 2009-07-16 10:50 lib.h         

time to run the binary.

[[email protected] c-lib]$ ./hello                          

 Fun1
 Fun2
Yeah,we got the desired output :) You can use nm command to view list of functions on a specific object file.
[[email protected] c-lib]$ nm hi.o
00000000 T fun1              
         U printf      

got it ? hi.o has function called fun1. "U" refers those functions are  unresolved.      
[[email protected] c-lib]$ nm hi2.o
00000000 T fun2               
         U printf             
[[email protected] c-lib]$ nm hi_main.o
         U fun1                   
         U fun2                   
00000000 T main                   
[[email protected] c-lib]$ nm hello 
080494a8 d _DYNAMIC            
08049574 d _GLOBAL_OFFSET_TABLE_
0804847c R _IO_stdin_used       
         w _Jv_RegisterClasses  
08049498 d __CTOR_END__         
08049494 d __CTOR_LIST__        
080494a0 D __DTOR_END__         
0804949c d __DTOR_LIST__        
08048490 r __FRAME_END__        
080494a4 d __JCR_END__          
080494a4 d __JCR_LIST__         
08049594 A __bss_start          
0804958c D __data_start         
08048430 t __do_global_ctors_aux
080482f0 t __do_global_dtors_aux
08049590 D __dso_handle         
         w __gmon_start__       
0804842a T __i686.get_pc_thunk.bx
08049494 d __init_array_end      
08049494 d __init_array_start    
080483c0 T __libc_csu_fini       
080483d0 T __libc_csu_init       
         U __libc_start_main@@GLIBC_2.0
08049594 A _edata                      
0804959c A _end                        
0804845c T _fini                       
08048478 R _fp_hw                      
08048250 T _init                       
080482c0 T _start                      
08049594 b completed.6610              
0804958c W data_start                  
08049598 b dtor_idx.6612               
08048350 t frame_dummy                 
08048398 T fun1                        
080483ac T fun2                        
08048374 T main                        
         U printf@@GLIBC_2.0           
Now we could use ar command archive our object files (hi.o and hi2.o) into a library file named libhi.a
[[email protected] c-lib]$ ar crv libhi.a hi.o hi2.o
a - hi.o                                       
a - hi2.o

here is the list of files :

[[email protected] c-lib]$ ls -ltr
total 40
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi.c
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi2.c
-rw-r--r-- 1 lg lg   26 2009-07-16 10:50 lib.h
-rw-r--r-- 1 lg lg   43 2009-07-16 10:50 hi_main.c
-rw-r--r-- 1 lg lg  784 2009-07-16 10:51 hi_main.o
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi.o
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi2.o
-rwxr-xr-x 1 lg lg 6482 2009-07-16 10:51 hello*
-rw-r--r-- 1 lg lg 1842 2009-07-16 10:53 libhi.a
Now we use the -L option to mention our own library file,while creating binary file named hello1. -lhi is the short form for -libhi.
[[email protected] c-lib]$ gcc -o hello1 hi_main.o -L . -lhi
[[email protected] c-lib]$ ls -ltr
total 48
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi.c
-rw-r--r-- 1 lg lg   34 2009-07-16 10:49 hi2.c
-rw-r--r-- 1 lg lg   26 2009-07-16 10:50 lib.h
-rw-r--r-- 1 lg lg   43 2009-07-16 10:50 hi_main.c
-rw-r--r-- 1 lg lg  784 2009-07-16 10:51 hi_main.o
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi.o
-rw-r--r-- 1 lg lg  816 2009-07-16 10:51 hi2.o
-rwxr-xr-x 1 lg lg 6482 2009-07-16 10:51 hello*
-rw-r--r-- 1 lg lg 1842 2009-07-16 10:53 libhi.a
-rwxr-xr-x 1 lg lg 6482 2009-07-16 10:54 hello1*

you can see binary file is created successfully 

[[email protected] c-lib]$ ./hello1

 Fun1
 Fun2

Cool,we got the output too. So we have used our own library to create a binary file. 

Powered by
Open Source Programmers