EmbLogic's Blog

CREATING STATIC LIBRARY

Static libraries are collection of ordinary object files. For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.
Steps:
1.Creates .c file, that contains functions in your library and application.
2.Create object file.eg
gcc -c add.c -o add.o
//-c means to create an intermediary object file, rather than an executable.
3.Create static library.This step is to bundle multiple object files in one static library.
ar rcs libststlib.a add.o sub.o
//r means to insert with replacement, c means to create a new archive,
and s means to write an index.
NOTE: A static library must start with ‘lib’ and have the suffix ‘.a’.
4.Making Executable and linking with library.
gcc -o operation operation.o -L. -lststlib
Note: -L. is used to tell that the static library is in current folder or you can provide the path too.
-l searches for the library named library at the time of linking. Linker searches and processes libraries and object files in the order they are specified.
5. Run the program.

Note:
1.You can see the list of object files in your library using
ar -t libststlib.a
2.ar is archive is single file holding a collection of other files.
Screenshot from 2014-11-11 04_20_32

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>