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
Comments on this entry are closed.
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
Absolutely awesome. Thank you.
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.
@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.
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!
informative article
thanks
Hi
It doesn’t work
I get /usr/bin/ld: cannot find -larith.a
Which way to repair it
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..
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 🙂
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”
Hi,
Very useful information in detailed way.
I was able to get .o files from an archive file extension .a
Can anybody please let me know is there any tool or set of commands to do the reverse engineering (conversion from .a to c files or to any readable format).
Thank you.
Clear notes