Sunday, July 22, 2012

Creating Static Libraries


Step1:-create the source files( .c files)

#vim main.c

#vim add.c

#vim sub.c

#vim multi.c

#vim div.c 

 

Step2:-create a relocatable files to the corresponding source files

#gcc -c main.c -o main.o

#gcc -c add.c -o add.o

#gcc -c sub.c -o sub.o

#gcc -c multi.c -o multi.o

#gcc -c div.c -o div.o

 

Step3:-By using a archiver tool("ar")to create static libraries

#gcc rcs libstatic.a add.o sub.o multi.o div.o

A static library must start with the three letters 'lib' and have the suffix '.a'

 

Step4:- Linking static library

#gcc -static main.c libstatic.a -o main 

 

Step5:-finally run the executable

#./main

Compileing Stages

Step1:- Source file-------------------------------------------------------->.c File

#gcc<space>-E<space>.c filename<space>-o<space>targetfilename1.i

By using -E flag preprocessor is invoked 

#gcc -E Hello.c -o Hello.i

#vim Hello.i file


Step2:-Preprocessor file------------------------------------------------>.i File

#gcc<space>-S<space>targetfilename1.i<space>-o<space>targetfilename2.s

By using -S flag Compiler is invoked

 

Step3:-Assembly file---------------------------------------------------->.s File

#gcc<space>-c<space>targetfilename2.s<space>-o<space>targetfilename3.o

By using -c flag Assembler is invoked to generate Relocatable file.


Step4:-Relocatable file------------------------------------------------>.o File

#gcc<space>targetfilename3.o<space>-o<space>targetfilename.

Finally Executable file will be created.