0% found this document useful (0 votes)
23 views

Libraries

Uploaded by

Soham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Libraries

Uploaded by

Soham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Creating and using libraries

Computing Laboratory

http://www.isical.ac.in/~dfslab

Computing Lab (ISI) Creating and using libraries 1/6


Types of libraries

Static libraries (.a for archive): library becomes part of the


executable
Dynamically linked shared object libraries (.so for shared object):
library is not included in the executable, but functions are called as
needed at runtime

Computing Lab (ISI) Creating and using libraries 2/6


Creating / using static libraries

Compiling: gcc -c abc1.c abc2.c


Creating the library: ar cr libabc.a abc1.o abc2.o
Other options (see man page for more details):
c: create
q: quick (append)
r: replace
u: update
v: verbose
t: list
Using the library:
gcc -o prog prog.c libabc.a
or
gcc -o prog prog.c -L/path/to/library-directory -labc

Computing Lab (ISI) Creating and using libraries 3/6


Creating shared object libraries

position independent code


Compiling: gcc -Wall -fPIC -c *.c
Creating the library: these options are passed to the linker
gcc -shared -Wl,-soname,libabc.so -o libabc.so *.o
Using the library:
gcc -o prog prog.c -L/path/to/library-directory -labc

Computing Lab (ISI) Creating and using libraries 4/6


Using libraries

Compiler flags
-I: specify additional directories to search for header files
-L: specify additional directories to search for library files

Computing Lab (ISI) Creating and using libraries 5/6


Using libraries

Compiler flags
-I: specify additional directories to search for header files
-L: specify additional directories to search for library files

Environment variables
export C_INCLUDE_PATH=/path/to/header-files
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib

Computing Lab (ISI) Creating and using libraries 5/6


Source

http://www.yolinux.com/TUTORIALS/
LibraryArchives-StaticAndDynamic.html

Computing Lab (ISI) Creating and using libraries 6/6

You might also like