12.15 – Creating a library file in Linux

by subbu on December 28, 2013

How to create a library file in Linux

Library file is a group of compiled object files saved with .a file extension in case of Linux. Generally functions, enumerations, user defined types and constants shared by more than one application are broken out of the application source code, compiled and bundled as a library file. All the predefined functions are managed in standard libraries of gcc. We can also create our own library and can be used in any application. The main advantages with libraries are

  • Same library can be shared by multiple applications
  • Vendors can release libraries as APIs (Application Program Interface)
  • It reduces the applications source size and saves the compilation time
  • We no need to specify different object files while linking to make a build

Now in this session, we will see how a static library can be created and used in Linux step by step

Step 1: Implement the library source files and save them with .c file extension.
Here “first.c” has a function adding() that takes two integers and returns sum of them, “second.c” has a function subtraction() that takes two integers and returns subtraction of them.

$gedit first.c
int adding(int x,int y)
{
  return x+y;
}

Select Save (Alt+s), Quit (Alt+Q)

$gedit second.c
int subtraction(int x,int y)
{
 return x-y;
}

Select Save (Alt+s), Quit (Alt+Q)

Step 2: Compile and generate object files of both .c files using gcc compiler
-c is an option to generate only object files with .o file extension

$gcc –c first.c second.c

Step 3: Create the library file using archive tool ar. It groups all the object files and generates a library file with .a file extension.
-c is an option to create a new archive
-r is an option to replace an existed archive if existed with the same name
-v to verbose (display) the contents of library

$ar –crv mylib.a first.o second.o
first.o
second.o
$

-t is an option to display the contents of a library file

$ar –t mylib.a
first.o
second.o
$

Creating a library file in Linux

Step 4: Create a header file with the prototypes of library functions and save with .h file extension

$gedit mylib.h
int adding(int,int);
int subtraction(int,int);

Select Save (Alt+s), Quit (Alt+q)

Step 5: We can use both the library functions and header file in any C program within the same folder

$gedit main.c
#include<stdio.h>
#include "mylib.h" /* to access library from the same folder */
int main()
{
int x,y,sm,sb;
x=10;
y=5;
sm=adding(x,y);
sb=subtraction(x,y);
printf("Sum %d",sm);
printf("\nSubtraction %d",sb);
return 0;
}

Select save (Alt+S), Quit (Alt+Q)

Step 6: Compile and execute the program
While linking the library, specify the name of library file and the location of header file along with the program name
-I to specify the path of header file ( ./ means same folder in Linux)
-o to specify the name of output file

$gcc –I ./ -o main main.c ./mylib.a
$./main
Sum 15
Subtraction 5

Linking library to a C program

Static Vs. Dynamic Library:

By default the executable file created by the gcc is dynamically linked with the library. The executable file created by the compiler can be only executed in the same system in which library file is created or loaded but, can’t be executed on another system.

In the above example main.out (executable file) uses the library file mylib.a during execution. The program may not be executed on deleting mylib.a

Dynamic library

By using an option –static we can statically link the executable file with the library, that means libraries are added to the executable code so that, executable file can be loaded on to any machine and executed.

Statically link library

By using the command file we come to know that, by default gcc follows dynamic linking and can be statically linked using the –static flag with gcc

$gcc –I ./ -o main main.c ./mylib.a
$./main
Sum 15
Subtraction 5
$file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically 
linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
$gcc –static –I ./ -o main main.c ./mylib.a
$./main
Sum 15
Subtraction 5
$file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically 
linked , for GNU/Linux 2.6.15, not stripped

File command in linux

By using the the option –l with the ls command we come to know that, the size of executable file created with static link is nearly 60 times greater than the file created with dynamic linkage (default)

$gcc –I ./ -o main main.c ./mylib.a
$./main
Sum 15
Subtraction 5
$ls –l main
-rwxr-xr-x 1 codingfox codingfox 8399 2014-02-10 18:52 main
$
$gcc –static –I ./ -o main main.c ./mylib.a
$./main
Sum 15
Subtraction 5
$ls –l main
-rwxr-xr-x 1 codingfox codingfox 579184 2014-02-10 18:52 main
$

Ls command in linux

One more thing to discuss is, in the above output there is something like “not stripped”. Which means there is some extra information like metadata in the executable. That extra unnecessary information can be removed using the tool strip. After stripping the file, the sizes may be reduced by few kilo bytes.

$strip main
$

Stripping a file in linux

Previous post:

Next post: