5 Jul 2012

General compiling steps & Library creation

General Steps of compilation of C file:

               \ /
                |                 .c    
                |                  |
      Preprocessing      |                gcc -E file.c
                |                 .i
                |                  |
        Compiling           |               gcc -c file.c
                |                 .s
                |                  |
       Assembler           |               gcc -S file.c
                |                 .o (asm)
                |                  |
          Linker              |      
                |                .exe
          executable                       gcc  file.c  -o  foo

   General execution:
               gcc file.c                -->  gives executable file
               gcc  -c  file.c          -->  gives the object file

Creating the Dynamic and Static Libraries  :

 Static Library:
    Static library has the extention of  (.a) . You should know that when ever you call the function of this library the whole code is dumped into the main code while compilation time so that the whole size of the program increases but the execution speed increases. But the main requirement is the file should be small and the speed should be also fast right . so we go to the dynamic libraries.
   Ok lets discuss how to create a static library and how to link it to the required file.

1. All file first make them as object files using the command 
                gcc   -c  file1.c file2.c       -->   file1.o  file2.o
2. Now you create using this object files your static library file
               ar cr  libswap.a  file1.o file2.o   --> gives the output of libswap.a  
3. Now you have created the library now link it to the main program
               gcc -Wall main.c  libswap.a  -o  swap
      The above command tells that main in linked with libswap.a  library and executable is renamed as the swap if the library (.a) is in the same file other wise you should give the path of the library file where it is located. This i will dicuss in the dynamic library. ok just here i showed creating and linking the main and library in the same file.

Dynamic Library:
    The good thing about this is when a function is called then pointer goes to the location where it is located and execute over there and come back to the main when it is done with the function . It just saves lot of memory and execution speed is also good.That is why most of the operating systems like unix and linux uses such type of libraries mostly.

    So lets learn how to create the Dynamic Libraries:
1. gcc -shared  -fPIC  -I ../../include   swap1.c  -o  ../../library/libswap.so 
          Ya now i feel you feel some difficulty its very simple i will tell you 
-shared   -->  is used to produce shared object which can be linked with other objects to form an executable.
-fPIC      -->  compiler directive to output position independent   code

2. gcc  -I  ../include/   -lswap  -L ../Library/  swap_main.c  -o  swap_main

here it tells that including the folder name where the .h files are located and linking the library where it is located (the folder paths we should specify from current directory) thats it very simple na. 

Don't still deep into it you should know one thing in embedded field or any other field read only what is required . Don't try read the whole stuff from scratch it will be helpful but you will loose your valuable time which is precious if you don't have any work like targets then you can.

No comments:

Post a Comment