UNIX ar Examples: How To Create, View, Extract, Modify C Archive Files (*.a)

by Balakrishnan Mariyappan on August 11, 2010

ar is an archive tool used to combine objects to create an archive file with .a extension, also known as library.

In this article, let us discuss about how to create an user defined static library in C programming using the “ar” utility. The examples shows how to create, extract, and modify the archives using Linux ar command.

To demonstrate the static library creation, let us create two C programs — addition.c and multiplication.c

Using gcc, the object code for these programs are obtained, and the static library libarith.a is created from these two objects.

1. Create Two Sample C Programs

Create addition.c program as shown below.

int addition(int a,int b)
{
int result;
result = a + b;
return result;
}

Create multiplication.c program as shown below.

int multiplication(int a, int b)
{
int result;
result = a * b;
return result;
}

A while back we discussed about fundamental of writing C program using C hello world example.

2. Compile the Programs and Get Object Codes

Use -c option to compile both the c program. Using option -c will create the corresponding .o files.

$ gcc -c addition.c

$ gcc -c multiplication.c

Now, the current working directory contains both the .c and .o files as shown below.

$ ls
addition.c   multiplication.c   addition.o   multiplication.o

3. Create the C Program Static Library using ar utility

Now create the static library “libarith.a” with the addition object file and multiplication object file as follows,

$ ar cr libarith.a addition.o multiplication.o

4. Write C program to Use the Library libarith.a

The library file libarith.a is now ready to usage. Following example indicates how to write a sample C program with the header file to use the libarith.a static library.

Create header.h :

#include <stdio.h>
int addition(int a,int b);
int multiplication(int a,int b);

Create example.c :

#include "header.h"
int main()
{
int result;
result = addition(1,2);
printf("addition result is : %d\n",result);
result = multiplication(3,2);
printf("multiplication result is :  %d\n",result);
}

Note: How to Debug C Program using gdb in 5 Simple Steps provides step-by-step instruction on debugging your C code.

Compile example.c :

$ gcc -Wall example.c -L/home/guest/ -larith -o example

The option -L instructs the compiler to look in the /home/guest directory for library files. From this directory, the compiler takes the libarith library file, compiles it with example.c program.

Another method to Compile example.c :

$ gcc -Wall example.c libarith.a -o example

Execute example executable :

$ ./example
addition result is : 3
multiplication result is : 6

5. View Object Files in an Archive Using ar Command, option t

To list the object files available in the libarith.a:

$ ar t libarith.a
addition.o
multiplication.o

The options in ar command are similar to the tar command.

6. Extract Object Files from an Archive Using ar Command, option x

You can extract the object files available in an archive as follows.

$ mkdir object

$ cp libarith.a object/

$ cd object

$ ar x libarith.a

$ ls *.o
addition.o
multiplication.o

7. Add an Object File into the Existing Archive Using ar, option r

Let assume that you have create another object file called subtraction.o

The following command extends the libarith.a library file, by inserting subtraction.o object as shown below.

$ ar r libarith.a subtraction.o 

$ ar t libarith.a
addition.o
multiplication.o
subtraction.o

While inserting a .o file, it it already exists in the archive, it would be replaced. Without checking for replacements the objects can be added to end of the archive by using -q option.

8. Delete a Specific Archive Member Using ar, option d

In order to delete a specific archive member from the library file, do the following.

$ ar d libarith.a addition.o

$ ar t libarith.a
multiplication.o
subtraction.o

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Tags:

{ 10 comments… read them below or add one }

1 Plip August 11, 2010 at 1:40 pm

Hello,
I love your site and have been following it for some time with my rss reader. :)

But I have a question : why do you write “./file” in some of your commands when “file” would be sufficient ? Is that intended to avoid taking ambiguous symlinks as files ?
(here for example : “$ gcc -Wall ./example.c ./libarith.a -o ./example”, ./ is useless, as far as I know)

I guess this could lead people following actions word for word to some “bad practices” in that they may think it is necessary to put “./” and dangerous not to write it …

Anyway, thanks for your work. ;)
Regards,
plip

2 Daniel Reimann August 12, 2010 at 4:52 am

Absolutely awesome. Thank you.

3 Solomon Homicz August 17, 2010 at 9:53 pm

Would this be fundamentally the same with C++?
Just replace gcc with g++ and .c with .cpp?
Could you also do this with fortran? gfortran with .f90 and .F95?
It would interesting if this could be another way to use c or C++ with fortran subroutines.

4 Ramesh Natarajan August 17, 2010 at 11:52 pm

@Plip,

Thanks for the suggestion. I completely agree with you. “./” is not required. It is just a mistake and I’ve removed it from the article.

5 Solomon Homicz August 19, 2010 at 11:48 pm

Yep, works fine with fortran subroutines. I used a C++ main file, in a header declared an external C struct matching a common block in a fortran header and declared an external C function (void). Compiled the fortran as an object file used ar to make a .a and then compiled with g++, works great! I could write a C++ (or C) wrapper for my fortran subroutines package it up in a library and have an object I could just plug in and use in C++. Way cool!

6 Mars December 14, 2010 at 11:32 pm

informative article
thanks

7 boris May 10, 2012 at 11:00 pm

Hi
It doesn’t work
I get /usr/bin/ld: cannot find -larith.a

Which way to repair it

8 Jonas Kulla August 17, 2012 at 3:44 pm

Thank you very much!
Even though I have been programming for quite a while in C,
I really appreciated detailed / very simply step-by-steps like these,
much better than skimming through manuals..

9 Gagandeep Bali February 27, 2013 at 6:01 am

Wonderful stuff, helped me loads, learning the basics for creating library files and adding my own functions to it. Really full of knowledge. Nice endeavour of yours, will always appreciate what I learned on this blog of yours :-)

10 Anonymous August 9, 2014 at 6:37 am

I think you forgot the -s option for indexing the archive, because I can’t link with the library (statically), because it says that the archive has no index (therefore required). So instead of “ar -cr” use “ar -crs”

Leave a Comment

Previous post:

Next post: