Libraries in C

What are libraries and why use libraries in c ?

Types of libraries in C:

Static libraries

Shared or dynamic libraries

How static libraries work

What is the difference between them

Creating A Static “C” Library

gcc -c *.c
ar -rc liball.a *.o
ranlib liball.a
  1. Use 'ranlib' to re-generate the index.
  2. When copying the archive file to another location, use 'cp -p', instead of only 'cp'. The '-p' flag tells 'cp' to keep all attributes of the file, including its access permissions, owner (if "cp" is invoked by a superuser) and its last modification date. This will cause the compiler to think the index inside the file is still updated. This method is useful for makefiles that need to copy the library to another directory for some reason.

How to use libraries in C

Example:

Creating A Dynamic “C” Library

gcc -c -fPIC *.c
gcc -shared -o liball.so *.o
nm -D liball.so

How to use it

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
gcc -L . 0-main.c -l all -o example

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store