Linux ar Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux ar command, which is used to create, modify, and extract content from static libraries. Static libraries are archives of object files that can be linked into programs to provide functionality. We will learn how to create static libraries, extract and list their contents, and understand the basic usage of the ar command. The lab covers key skills in the area of compression and archiving on Linux systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/tar -.-> lab-422552{{"`Linux ar Command with Practical Examples`"}} linux/rm -.-> lab-422552{{"`Linux ar Command with Practical Examples`"}} linux/nano -.-> lab-422552{{"`Linux ar Command with Practical Examples`"}} end

Understanding the ar Command

In this step, we will explore the Linux ar command, which is used to create, modify, and extract content from static libraries. Static libraries are archives of object files that can be linked into programs to provide functionality.

Let's start by understanding the basic usage of the ar command:

sudo ar --help

Example output:

Usage: ar [OPTIONS] ARCHIVE [MEMBERS]...
Modify an archive file

 The most commonly used options for the "ar" command are:

 -a            add files to the end of the archive
 -c            create the archive if it does not already exist
 -d            delete files from the archive
 -p            print the contents of the specified files
 -r            insert or replace files in the archive
 -t            display a table of contents of the archive
 -x            extract files from the archive

As you can see, the ar command provides various options to manage static libraries. Let's go through some of the common use cases.

Creating Static Libraries with ar

In this step, we will learn how to create static libraries using the ar command.

First, let's create a simple C file that we will use to build the static library:

cd ~/project
nano hello.c

Add the following content to the hello.c file:

#include <stdio.h>

void sayHello() {
    printf("Hello, from the static library!\n");
}

Next, we will compile the hello.c file into an object file:

gcc -c hello.c

This will create the hello.o object file.

Now, we can use the ar command to create a static library from the object file:

sudo ar rcs libhello.a hello.o

The r option replaces or inserts files into the archive, c creates the archive if it doesn't exist, and s updates the archive symbol table.

You can list the contents of the static library using the t option:

sudo ar t libhello.a

Example output:

hello.o

The static library libhello.a has been created and now contains the hello.o object file.

Extracting and Listing Contents of Static Libraries

In this step, we will learn how to extract and list the contents of static libraries using the ar command.

First, let's create a simple program that uses the static library we created in the previous step:

cd ~/project
nano main.c

Add the following content to the main.c file:

#include <stdio.h>

extern void sayHello();

int main() {
    sayHello();
    return 0;
}

Now, let's compile the main.c file and link it with the libhello.a static library:

gcc -o main main.c -L. -lhello

The -L. option tells the compiler to look for libraries in the current directory, and -lhello links the libhello.a library.

You can run the compiled program to see the output:

./main

Example output:

Hello, from the static library!

To list the contents of the libhello.a static library, you can use the t option:

sudo ar t libhello.a

Example output:

hello.o

To extract the contents of the static library, you can use the x option:

sudo ar x libhello.a

This will extract the hello.o object file from the libhello.a static library.

Summary

In this lab, we learned how to use the Linux ar command to create, modify, and extract content from static libraries. We started by understanding the basic usage of the ar command and its various options. Then, we created a simple C file, compiled it into an object file, and used the ar command to create a static library from the object file. We also learned how to list the contents of the static library using the ar command. Overall, this lab provided a practical introduction to working with static libraries on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like