قالب وردپرس درنا توس
Home / Tips and Tricks / How to Use the ar Command of Linux to Create Static Libraries

How to Use the ar Command of Linux to Create Static Libraries



  Shell Command Prompt on a Linux Laptop
Fatmawati Achmad Zaenuri / Shutterstock.com

Use the ar command from Linux to create function libraries when developing software. In this tutorial, you will learn how to create, modify, and use a static library in a sample code program.

The ar command is a true veteran ̵

1; it's been around since 1971. The name ar refers to the original use intended for the tool where archive files were created. An archive file is a single file that acts as a container for other files. Sometimes for many other files. Files can be added to, removed from, or extracted from the archive. People who are looking for this kind of functionality no longer apply to ar . This role was taken over by other utilities such as tar .

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

We will take the necessary steps to create and modify a static library and demonstrate the use of the library in a program. To do this, we need a request that the static library must meet. The purpose of this library is to encode text strings and decode coded text.

Please note that this is a fast and dirty hack for demonstration purposes. Do not use this encryption for anything of value. It is the simplest replacement cipher in the world, where A becomes B, B becomes C, and so on.

RELATED: Compressing and Extracting Files Using the tar Command on Linux

The cipher_encode () and cipher_decode () Functions

We will work in a directory called "library" later create a subdirectory named "test".

There are two files in this directory. In a text file named 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 appropriate function cipher_decode () is in a text file named 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 statements are called source code files. We will create a library file named libcipher.a. It contains the compiled versions of these two source code files. We also create a short text file named libcipher.h. This is a header file containing the definitions of the two functions in our new library.

Anyone who has the library and header file can use the two functions in their own programs. You do not have to reinvent the wheel and rewrite the functions. You simply use the copies in our library.

Compile the cipher_encode.c and cipher_decode.c files

To compile the source code files, we use gcc the default GNU compiler. The option -c (compile, no join) instructs gcc to compile and then stop the files. From each source code file, called an object file, an intermediate file is created. The linker gcc usually takes all object files and links them to an executable program. We skip this step with the option -c . We only need the object files.

Let's see if we have the files that we believe we have.

  ls -l 

 ls in a terminal window

The two source code files exist 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 output of gcc . if everything goes right.

 gcc output in a terminal window

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

  ls -l 

 ls -l output in a terminal window

Creating the libcipher.a library

Create the library file, which is actually an archive file. We use ar .

We use the option -c (create) to create the library file. -r (Add with Replace) to add the files to the library file, and the option -s (Index) to create an index of the files in the library file. [19659004] We will call the libcipher.a library file. We specify this name in the command line along with the name of the object files to 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

When we list the files in the directory, Let's see that we now have a libcipher.a file.

ls -l

 Issue of ls in a terminal window

If we can use the option -t (table) with ar we see the modules in the library file. [19659013] ar -t libcipher.a

 ar -t libcipher.a in a terminal window

Creating the header file libcipher.h

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

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

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

Using the libcipher Library

The only sure way to test our new library is to write a small program to use. First we create a directory named test.

  mkdir test 

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

  cp libcipher in the new directory. 

  cd test 

Let's see if our two files are here.

  ls -l 

 cp libcipher. * ./test in a terminal window [19659004] We need to create a small program that can use the library and prove that it works as expected. Enter the following lines of text in an editor. Save the contents of the editor in a file named "test.c" in the directory test .

  #include 
#include 

#include "libcipher.h"

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

sets (text);

cipher_encode (text);
sets (text);

cipher_decode (text);
sets (text);

Exit (0);

} // end of main 

The program flow is very simple:

  • Contains the file libcipher.h so that the definitions of the library functions are displayed Words "How-To Geek Loves Linux".
  • This string is displayed on the screen.
  • The function cipher_encode () is called to encode the string, and the encoded string is printed on the screen.
  • It calls cipher_decode () to decode the string and outputs 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 option -o (output) tells gcc how to invoke the generated executable program.

  gcc test.c libcipher.a -o test 

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

If gcc you tacitly to the command prompt everything is fine. Let's test our program. Moment of truth:

  ./test 

 ./ test in a terminal window

And we see the expected output. The test program prints the plain text, prints the encrypted text and then prints the decrypted text. It uses the features in our new library. Our library works.

 Output of the test program in a terminal window

Success. But why stop here?

Adding another module to the library

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

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

  #include 

void cipher_version (void)
{
puts ("How-To Geek :: VERY INSECURE Encryption Library");
sets ("Version 0.0.1 Alpha  n");

} // end of cipher_version 

We need to add the definition of the new function to the header file libcipher.h. Add a new line at the end of this 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 we have an object file cipher_version.o.

  gcc -c cipher_version.c [19659014]   gcc -c cipher_version.c in a terminal window  

This creates a cipher_version.o file. With the following command we can add the new object file to library libcipher.a. The option -v (detailed) lets the normally silent ar say what she did.

  ar -rsv libcipher.a cipher_version.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

With the option -t (table) you can see which modules are inside 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 feature.

Using the cipher_version () function.

Remove the old library and the header file from the test directory, copy the new files, and then go back to the test directory. [19659004] We delete the old versions of the files.

  rm ./test/libcipher.*[19659014[Composingnewversionsinthetestdirectory[196659013] cc libcipher. * ./Test[19659014] We change to the test directory. 

  cd test 

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

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

We need to add a new line to the program test.c calling the function cipher_version () . We place this before the first line of text; line.

  #include 
#include 

#include "libcipher.h"

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

// Added new line here
cipher_version ();

sets (text);

cipher_encode (text);
sets (text);

cipher_decode (text);
sets (text);

Exit (0);

} // end of main 

Save this as test.c. We can now compile it and test if the new feature is functional.

  gcc test.c libcipher.a -o test 

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

Let's see the new version of test :

 Output of the test program in a terminal window

The new function works. The version of the library is displayed at the beginning of the output of test .

There may be a problem, however.

Replacing a module in the library

This is not the first version of the library; it is the second one. Our version number is wrong. The first version contained no function cipher_version () . This one does it. So that should be the version "0.0.2". We have to replace the function cipher_version () in the library with a corrected one.

Fortunately, ar makes this very easy.

First we edit the file cipher_version.c in the directory library . Change the text "Version 0.0.1 Alpha" to "Version 0.0.2 Alpha". It should look like this:

  #include 

void cipher_version (void)
{
puts ("How-To Geek :: VERY INSECURE Encryption Library");
sets ("Version 0.0.2 Alpha  n");

} // end of cipher_version 

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

  gcc -c cipher_version.c 

 gcc -c cipher_version.c in a terminal window

Now we will Replace the existing object cipher_version.o in the library through our newly compiled version.

We previously used the option -r (Add with Replace) to add new modules to the library. If we use it with a module that already exists in the library, ar will replace the old version with the new one. The option -s (index) updates the library index, and -v (verbose) specifies ar what it did. [19659013] ar -rsv libcipher.a cipher_version.o

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

This time ar reports that it replaces that has module cipher_version.o. The "r" means replaced.

 is output in a terminal window.

Using the updated function cipher_version ()

We should use our modified library and see if it works.

We copy the library files to the test directory.

  cp libcipher. * ./Test[19659014<We'llswitchtothetestdirectory

 cd ./test[19659014<Wecancreateourtestprogramusingournewlibrary

 gcc test.c libcipher.a -o test 

And now we can test our program.

  ./test 

 cp libcipher. * ./test in a terminal window

The output of the test program meets our expectations. The correct version number is displayed in the version string, and the encryption and decryption routines work.

Deleting modules from a library

After all, it seems a shame, but we delete the file cipher_version.o from the library file.

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

  ar -dsv libcipher.a cipher_version.o 

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

ar reports that the module has been removed. The "d" means "deleted".

If we ask or to list the modules in the library file, we will find that we have two modules again.

  or -t libcipher .a 

 ar -t libcipher.a in a terminal window

If you want to delete modules from your library, you must remove their definition from the library header file.

Sharing Your Code

Libraries allow you to share code in a convenient but private way. Anyone to whom you submit the library file and the header file can use your library, but your actual source code remains private.




Source link