How to Use Linux’s ar Command to Create Static Libraries

Shell prompt on a Linux laptopFatmawati Achmad Zaenuri / Shutterstock.com

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

The ar command is a true veteran – it has existed since 1971. The ar name refers to the intended use at the origin of the tool, which was create archive files. An archive file is a single file that serves as a container for other files. Sometimes for many other files. Files can be added, deleted or extracted from the archive. People looking for this type of feature no longer look to ar. This role has been taken over by other utilities such as tar.

The ar command is still used for some specialized purposes. ar is used to create static libraries. These are used in software development. And ar is also used to create package files such as ".deb" files used in the Debian Linux distribution and its derivatives, such as Ubuntu.

We will follow the steps required to create and modify a static library and show how to use the library in a program. To do this, we need a requirement to complete by the static library. The purpose of this library is to encode text strings and to decode encoded text.

Please note that there is a quick and dirty hack for demonstration purposes. Do not use this encryption for anything that has value. It's the easiest in the world Substitution number, where A becomes B, B becomes C, and so on.

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

The functions cipher_encode () and cipher_decode ()

We will work in a directory called "library", then we will create a subdirectory called "test".

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

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

} // end of cipher_encode

The corresponding function cipher_decode () 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

Files containing programming instructions are called source code files. We will 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 containing the definitions of the two functions of our new library.

All users of the library and the header file will be able to use both functions in their own programs. They do not need to reinvent the wheel and rewrite the functions; they just use copies of our library.

Compiling cipher_encode.c and cipher_decode.c files

To compile the source code files, we will use gcc, the standard GNU compiler. The -c (compile, no link) option tells gcc to compile the files and then stop them. It produces an intermediate file from each source code file called object file. The gcc linker usually takes all the object files and binds them to create an executable program. We skip this step using the -c option. We just need the object files.

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

ls -l

ls in a terminal window

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 encryption code.c

If all goes well, there should be no gcc output.

gcc output in a terminal window

This generates two object files with the same name as the source code files, but with the ".o" extensions. These are the files we need to add to the library file.

ls -l

exit ls -l in a terminal window

Creating the libcipher.a library

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

We use the -c (create) option to create the library file, the -r option (add with replace) to add the files to the library file, and the -s (index) option to create an index of the files that it contains. the library file.

We will call the libcipher.a library file. We provide this name on the command line, as well as the names of the object files that we will add to the library.

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

ar -crs libcipher.a cipher_encode.o cipher_decode.o in a terminal window

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

ls -l

exit from ls in a terminal window

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

ar -t libcipher.a

ar -t libcipher.a in a terminal window

Creating the libcipher.h header file

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 present in the library.

To create the header file, we need to type the function definitions in a text editor such as 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);

Use the libcipher library

The only sure 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 into the new directory.

cp libcipher. * ./test

We will change in the new directory.

cd test

Let's check that our two files are here.

ls -l

cp libcipher. * ./test in a terminal window

We need to create a small program that can use the library and prove that it works as expected. Type the following lines of text in an editor. Save the contents of the editor in a file named "test.c" in the test directory.

#understand
#understand

#include "libcipher.h"

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

puts (text);

cipher_encode (text);
puts (text);

cipher_decode (text);
puts (text);

output (0);

} // end of the hand

The program is very simple:

It includes the libcipher.h file so that it can see the definitions of library functions.
He creates a string called "text" and stores the words "How-To Geek likes Linux".
It prints this string on the screen.
it calls the function cipher_encode () to encode the string and displays the encoded string on the screen.
It calls cipher_decode () to decode the string and displays the decoded string on the screen.

To generate the test program, we need to compile the test.c program and bind it to the library. The -o (output) option tells gcc how to call the executable program that it generates.

gcc test.c libcipher.a -o test

gcc test.c libcipher.a -o test in a terminal window

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

./test

./test in a terminal window

And we see the expected exit. The test program prints the plain text, prints the encrypted text, and decrypts the text. It uses the functions of our new library. Our library works.

Exit of the test program in a terminal window

Success. But why stop there?

Addition of 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 uses. We will need to create the new function, compile it, and add the new object file to the existing library file.

Type the following lines in an editor. Save the contents of the editor in a file named cipher_version.c, located in the library directory.

#understand

void cipher_version (void)
{
putting ("How-To Geek :: TRES INSECURE Encryption Library");
met ("Version 0.0.1 Alpha n");

} // end of the version_cipher

We need to add the definition of the new function to the libcipher.h header file. Add a new line to the bottom of this file, so that it looks 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 cipher_version.c file in order to have a cipher_version.o object file.

gcc -c version_cipher.c

gcc -c cipher_version.c in a terminal window

This creates a cipher_version.o file. We can add the new object file to the libcipher.a library using the following command. The -v option (verbose) allows the usually silent user to tell us what he has done.

ar -rsv libcipher.a version_cipher.o

ar -rsv libcipher.a cipher_version.o in a terminal window

The new object file is added to the library file. ar prints the confirmation. The "a" means "added".

output of ar in a terminal window

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

ar -t libcipher.a

ar -t libcipher.a in a terminal window

There are now three modules in our library file. Let's use the new function.

Using the cipher_version () function.

Delete the old library and the header file from the test directory, copy them into the new files, and then go back to the test directory.

We will delete old versions of the files.

rm ./test/libcipher.*

We will copy the new versions into the test directory.

cp libcipher. * ./test

We will change in the test directory.

cd test

rm ./test/libcipher.* in a terminal window

And now we can modify the test.c program so that it uses the new library function.

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

#understand
#understand

#include "libcipher.h"

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

// new line added here
version_cipher ();

puts (text);

cipher_encode (text);
puts (text);

cipher_decode (text);
puts (text);

output (0);

} // end of the hand

Save this under test.c. We can now compile it and test that the new function is operational.

gcc test.c libcipher.a -o test

gcc test.c libcipher.a -o test in a terminal window

Let's launch the new version of the test:

Exit of the test program in a terminal window

The new function works. We can see the version of the library at the beginning of the test output.

But there may be a problem.

Replacement of a module in the library

This is not the first version of the library; it's the second. Our version number is incorrect. The first version did not contain a cipher_version () function. This one does. So, this should be the "0.0.2" version. We need to replace the cipher_version () function in the library with a corrected function.

Fortunately, this is very easy to do.

Let's start by editing the cipher_version.c file in the library directory. Change the text "Version 0.0.1 Alpha" to "Version 0.0.2 Alpha". It should look like this:

#understand

void cipher_version (void)
{
putting ("How-To Geek :: TRES INSECURE Encryption Library");
met ("Version 0.0.2 Alpha n");

} // end of the version_cipher

Save this file. We need to recompile it to create a new cipher_version.o object file.

gcc -c version_cipher.c

gcc -c cipher_version.c in a terminal window

We will now replace the existing cipher_version.o object in the library with our newly compiled version.

We have already used the -r (add with replace) option to add new modules to the library. When we use it with an existing module in the library, ar will replace the old version with the new one. The option -s (index) will update the index of the library and the -v (verbose) option will make sure that it tells us what it has done .

ar -rsv libcipher.a version_cipher.o

ar -rsv libcipher.a cipher_version.o in a terminal window

This time, he indicates that he has replaced the cipher_version.o module. The "r" means replaced.

an output in a terminal window

Using the update function cipher_version ()

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

We will copy the library files into the test directory.

cp libcipher. * ./test

We will change in the test directory.

CD. / test

We have 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

cp libcipher. * ./test in a terminal window

The result of the test program is what we expected. The correct version number is indicated in the version string, and the encryption and decryption routines work.

Deleting modules from a library

It seems a shame, but let's delete the cipher_version.o file from the library file.

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

ar -dsv libcipher.a version_cipher.o

ar -dsv libcipher.a cipher_version.o in a terminal window

ar indicates that he deleted the module. The "d" means "deleted".

If we ask ar to list the modules in the library file, we will see that we have returned to two modules.

ar -t libcipher.a

ar -t libcipher.a in a terminal window

If you plan to remove modules from your library, be sure to remove their definition from the library header file.

Share your code

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

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.