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

Share

Fatmawati Achmad Zaenuri/Shutterstock.com

Use Linux’s ar command to create function libraries when you’re 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 real veteran—it has been around since 1971. The name ar references the original intended use for the tool, which was to create archive files. 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 looking for that type of functionality no longer turn to ar. That role has been taken over by other utilities such as tar.

The ar command is still used for a few specialist purposes, though. ar is used to create static libraries. These are used in software development. And ar is also be used to create package files such as the “.deb” files used in the Debian Linux distribution and its derivatives such as Ubuntu.

We’re going to run through 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 fulfill. The purpose of this library is to encode strings of text and to decode encoded text.

Please note, this is a quick and dirty hack for demonstration purposes. Don’t use this encryption for anything that is of value. It is the world’s simplest substitution cipher, where A becomes B, B becomes C, and so on.

RELATED: How to Compress and Extract Files Using the tar Command on Linux

The cipher_encode() and cipher_decode() Functions

We’re going to be working in a directory called “library,” and later we’ll create a subdirectory called “test.”

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

Read the remaining 88 paragraphs

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