How to use Linux ar command to create static libraries

Contents

Shell prompt on a Linux laptop

Use Linux ar command to create function libraries when you are developing software. This tutorial will show you how to create a static library, modify it and use it in a program, complete with sample code.

the ar Command is a true veteran, exists since 1971. Name ar refers to the original intended use of the tool, what was to create archive files. An archive is a single file that acts as a container for other files. Sometimes, for many other files. Files can be added, delete or extract from archive. People looking for that kind of functionality no longer turn to ar. That role has been assumed by other utility companies such as tar.

the ar Despite this, the command is still used for some specialized purposes. ar used to create static libraries. These are used in software development. AND ar it is also used to create package files such as files “.deb” used in the Debian Linux distribution and its derivatives, like Ubuntu.

We will review the steps required to create and modify a static library and demonstrate how to use the library in a program. To do that, we need a requirement for the static library to meet. The purpose of this library is to encode text strings and decode encoded text.

Please note this is a quick and dirty hack for demo purposes. Do not use this encryption for anything of value. It is the simplest in the world surrogate encryption, where A becomes B, B becomes C, and so on.

RELATED: How to compress and extract files using the tar command in Linux

The cipher_encode functions () y cipher_decode ()

Let's work on a directory called “library” and then we will create a subdirectory called “proof”.

We have two files in this directory. In a text file called cipher_encode.c we have the cipher_encode() function:

void cipher_encode(char *text)
{
 for (int i=0; text[i] != 0x0; i++) {
   text[i]++;
 }

} // end of cipher_encode

The respective cipher_decode() The function is in a text file called cipher_decode.c:

void cipher_decode(char *text)
{
 for (int i=0; text[i] != 0x0; i++) {
   text[i]--;
 }

} // end of cipher_decode

The files that contain programming instructions are called source code files.. We are going to create a library file called libcipher.a. It will contain the compiled versions of these two source code files. We will also create a short text file called libcipher.h. This is a header file that contains the definitions of the two functions in our new library.

Anyone with the library and the header file will be able to use the two functions in their own programs. They don't need to reinvent the wheel and rewrite the functions; they just make use of the copies in our library.

Compile the files cipher_encode.c and cipher_decode.c

To compile the source code files, we will use gcc, the standard GNU compiler. the -c (compile, sin link) option says gcc to compile the files and then stop. Produces an intermediary file from each source code file called an object file. the gcc The linker generally takes all the object files and links them to create an executable program. We skip that step using the -c option. We only need the object files.

Let's check that we have the files that we think we have.

ls -l

The two source code files are present in this directory. Let's use gcc to compile them into object files.

gcc -c cipher_encode.c
gcc -c cipher_decode.c

There should be no way out of gcc if everything goes fine.

This generates two object files with the same name as the source code files, but with extensions “.O”. These are the files that we must add to the library file.

ls -l

Creating the libcipher.a library

To create the library file, which is actually an archive file, we will use ar.

We are using the -c (create) option to create library file, the -r (add with replace) to add the files to the library archive, and the -s (index) option to create an index of the files within the library file.

Let's call the library file libcipher.a. We provide that name on the command line, along with the names of the object files that we are going to add to the library.

ar -crs libcipher.a cipher_encode.o cipher_decode.o

If we list the files in the directory, we will see that we now have a libcipher.a file.

ls -l

If we use the -t (table) option with ar we can see the modules inside the library file.

ar -t libcipher.a

Create the header file libcipher.h

The libcipher.h file will be included in any program that uses the libcipher.a library.. The libcipher.h file must contain the definition of the functions that are in the library.

To create the header file, we must write the function definitions in a text editor like gedit. Name the file "libcipher.h" and save it in the same directory as the libcipher.a file..

void cipher_encode(char *text);
void cipher_decode(char *text);

Using the libcipher library

The only safe way to test our new library is to write a small program to use it.. First, we will create a directory called test.

mkdir test

We will copy the library and header files to the new directory.

cp libcipher.* ./test

We will change to the new directory.

cd test

Let's check that our two files are here.

ls -l

We need to create a small program that can use the library and demonstrate that it works as expected. Write the following lines of text in an editor. save the editor's contents to a file called “test.c” at proof directory.

#include <stdio.h>
#include <stdlib.h>

#include "libcipher.h"

int main(int argc, char *argv[])
{
 char text[]="How-To Geek loves Linux";

 puts(text);

 cipher_encode(text);
 puts(text);

 cipher_decode(text);
 puts(text);

 exit (0);

} // end of main

The flow of the program is very simple:

  • Includes the libcipher.h file so you can see the definitions of the library functions.
  • Create a string called “text” and stores the words “How-To Geek loves Linux” in it.
  • Print that string on the screen.
  • call to cipher_encode() function to encode the string, and print the encoded string to the screen.
  • Llama cipher_decode() to decode the string and print the decoded string to the screen.

To generate the test Program, we need to compile the program test.c and the link in the library. the -o (Exit) option says gcc how to call the executable program it generates.

gcc test.c libcipher.a -o test

And gcc silently returns it to the command prompt, everything is fine. Now let's test our program. Moment of truth:

./test

And we see the expected output. the test The program prints the plain text, prints the encrypted text, and then prints the decrypted text. You are using the functions within our new library. Our library is working.

Success. But, Why stop there?

Add another module to the library

Let's add another function to the library. We will add a function that the programmer can use to display the version of the library he is using. We will need to create the new function, compile it and add the new object file to the existing library file.

Write the following lines in an editor. Save the content of the editor in a file called cipher_version.c, at Library directory.

#include <stdio.h>

void cipher_version(void)
{
 puts("How-To Geek :: VERY INSECURE Cipher Library");
 puts("Version 0.0.1 alpha");

} // end of cipher_version

We need to add the definition of the new function to the header file libcipher.h. Add a new line to the end of that file, to make it look like this:

void cipher_encode(char *text);
void cipher_decode(char *text);
void cipher_version(void);

Save the modified libcipher.h file.

We need to compile the file cipher_version.c so that we have an object file cipher_version.o.

gcc -c cipher_version.c

This creates a file cipher_version.o. We can add the new object file to the libcipher.a library with the following command. the -v (detailed) makes the option generally silent ar tell us what you have done.

ar -rsv libcipher.a cipher_version.o

The new object file is added to the library file. ar print confirmation. The “a” it means “aggregate”.

We can use the -t (table) option to see what modules are inside the library file.

ar -t libcipher.a

Now there are three modules within our library file. Let's make use of the new function.

Using the cipher_version function ().

Let's remove the above library and header file from the test directory, let's copy the new files and then change back to the test directory.

We will remove the previous versions of the files.

rm ./test/libcipher.*

We will copy the new versions in the test directory.

cp libcipher.* ./test

We will change to the test directory.

cd test

And now we can modify the test.c program to use the new library function.

We need to add a new line to the test program.c which calls cipher_version() function. We will place this before the first puts(text); line.

#include <stdio.h>
#include <stdlib.h> 

#include "libcipher.h" 

int main(int argc, char *argv[]) 
{
 char text[]="How-To Geek loves Linux"; 

 // new line added here
 cipher_version(); 

 puts(text); 
 
 cipher_encode(text); 
 puts(text); 
 
 cipher_decode(text); 
 puts(text); 

 exit (0); 

} // end of main

Save this as a test.c. Now we can compile it and test that the new feature is operational.

gcc test.c libcipher.a -o test

Let's run the new version of test:

The new feature is working. We can see the version of the library at the beginning of the output of test.

But there may be an obstacle.

Replacing a module in the library

this is not the first version of the library; is the second. Our version number is incorrect. The first version did not have cipher_version() function in it. This one does.. Then this should be the version “0.0.2”. We need to replace the cipher_version() function in the library with a corrected.

A misty valley seen from the top of a forested mountain, ar makes it very easy to make.

First, let's edit the file cipher_version.c in the Library directory. Change the text “Version 0.0.1 Alfa” a “Version 0.0.2 Alfa”. It must look like this:

#include <stdio.h>

void cipher_version(void)
{
 puts("How-To Geek :: VERY INSECURE Cipher Library");  
 puts("Version 0.0.2 alpha"); 

} // end of cipher_version

save this file. We need to compile it again to create a new object file cipher_version.o.

gcc -c cipher_version.c

Now we will do it. replace the existing cipher_version.o object in the library with our newly compiled version.

We have used the -r (add with replace) before, to add new modules to the library. When we use it with a module that already exists in the library, ar will replace the old version with the new one. the -s The option (index) will update the library index and -v (detailed) the option will ar tell us what you have done.

ar -rsv libcipher.a cipher_version.o

This time ar reports that you have overridden the cipher_version.o module. The “r” means replaced.

Using the cipher_version function () updated

We should use our modified library and check that it works.

We will copy the library files to the test directory.

cp libcipher.* ./test

We will change to the test directory.

cd ./test

We need to compile our test program again with our new library.

gcc test.c libcipher.a -o test

And now we can test our program.

./test

The result of the test program is what we expected. The correct version number is displayed in the version string and the encryption and decryption routines are working.

Delete modules from a library

It seems a shame after all that, but let's remove the cipher_version.o file from the library file.

To do this, we will use the -d (delete) option. We will also use the -v option (detailed), so that ar tells us what he has done. We will also include the -s (index) option to update index in library file.

ar -dsv libcipher.a cipher_version.o

ar reports that you have removed the module. The “d” it means “removed”.

If we ask ar to list the modules within the library file, we will see that we are back to two modules.

ar -t libcipher.a

If you are deleting modules from your library, remember to delete your definition from the library header file.

Share your code

Libraries make code shareable in a practical but private way. Anyone who you give the library file and header file to can use your library, but your actual source code remains private.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.