12.16 – Creating and including header files in Linux

by subbu on December 29, 2017

Loading library files in Linux:

In previous session, we have learned how a library and header files can be created and used in Linux. Now in this session we will see how library and header files can be created and deployed in gcc include and lib folders.

The main advantage with this is we no need to specify the path of header and library files with every program we compile.

Displaying the default search path of gcc

Whenever we specify the name of header file with #include<...> preprocessor statement then the gcc searches in different pre specified include and lib folders for specified header and library files.

By using gcc option –v (Verbose) we can see the default include search path of gcc for header files

$gcc –v main.c
#include "..." search starts here
#include<...> search starts here
End of search list.

The default search path of library files is by default /usr/lib

File system of Linux

Before going further, we will see the file system of Linux

File system of linux

../ to specify the previous folder
./  to specifiy the current folder
mv to move a file
sudo to execute a command with administrative privileges

Make sure that, both library and header files must be created with administrative privileges in order to use the resources by all users. sudo is used to issue a Linux command with administrative privileges.

Step 1: Create the library file.

It is mandatory to prefix a library file name with lib and with file extension .a. Say for example a library file with all the mathematical functions is saved as libm.a and with all default input and output functions as libc.a.

The advantage with this name convention is that, we no need to specify the total file name to the linker. It is enough to specify m in case of libm.a and c in case of libc.a with –l option.

$gcc –o demo demo.c –lm

The above command links /usr/lib/libm.a to the program to use mathematical functions

Now we will create a library file “libfox.a” step by step

The file arith.c has two functions sum() and sub(), accept two integers and return their addition and subtraction

$gedit arith.c
int sum(int x,int y)
  return x+y;
int sub(int x,int y)
  return x-y;

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

The file logic.c has two functions biggest() and smallest(), accept two integers and return biggest and smallest

$gedit logic.c
void biggest(int x,int y)
  return x;
  return y;
void smallest(int x,int y)
  return x;
  return y;

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

The gcc option –c compiles the .c files and produces their object files with .o file extension

$sudo gcc –c arith.c logic.c

ar is an archive tool used to create a library file with collection of object files.
-c option to create new archive file
-r option to replace an existed archive if have with the same name
-v option to verbose (display) the details

$sudo ar –crv libfox.a arith.o logic.o
a- arith.o
b- logic.o

By using the ls option –l, we will know that library file has root access

$ls –l libfox.a
-rw-r- - r - - 1 root root 1682 2014-02-12  06:00  libfox.a

By using ar option –t, we will know that library file libfox.a has object files arith.o and logic.o

$ar –t libfox.a

Creating an archive file in Linux

Step 2. Create a header file with function declaration statements

Here fox.h is the header file with the function declaration statements

$sudo gedit fox.h
int sum(int,int);
int sub(int,int);
int biggest(int,int);
int smallest(int,int);

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

Step 3. Deploy the library and header files to the default searching folders of gcc

Move the libfox.a to /usr/lib/ and fox.h to /usr/include/ using the command mv with administrative privileges

$sudo mv libfox.a ../../usr/lib/

Archive file in Linux

$sudo mv fox.a ../../usr/include/

Header file in Linux

Step 4. Link the library with any application that we compile

We can link the library with any application using –l option as –lfox (libfox.a).

$gedit main.c
int main()
int x,y;
printf("Enter any two integers:\n");
printf("Sum %d\n",sum(x,y));
printf("Subtraction %d\n",sub(x,y));
 printf("Biggest number %d\n",biggest(x,y));
 printf("Smallest number %d",smallest(x,y));
return 0;

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

$gcc –o main main.c –lfox
Enter any two integers:
Sum 46
Subtraction -22
Biggest number 34
Smallest number 12

Using a library file in Linux

Previous post:

Next post: