The ar program (from archiver) is a standard operating system utility whose purpose is to create, modify, and extract collections of files.
It is the heart of creating a static library in C.
The ar rcs command is the fundamental instruction in C to create a static library (.a)
Its function is to take one or more binary object code files (.o) and package them into a single library file (.a) that the linker (ld) can then use to build your executable program.
The main purpose of the ar rcs command is precisely to take many binary object code (.o) files and combine them into a single static library (.a) file.
You can list all the .o files you want to include, separated by spaces, after the destination library.
If you have three implementation files (math_utils.c, string_utils.c, and file_io.c), you first compile them to .o and then package them
The letters rcs are the flags that tell ar the specific actions to perform
- r = replace = grabs the .o file and puts it in the library. If there’s an old one, it replaces it.
- c = create = creates a new library (.a)
- s = creates an index, so functions can be searched quickly. = allows the linker (ld) to find the location of specific functions (like get_pi()) within the .a file quickly and efficiently.
By running ar rcs…, you are creating the key binary piece that your main program will use.
The final result is the *.a file, which acts as a repository for executable code. When you compile your program (main.c), the linker simply goes to this repository, extracts the code for the functions used from the library, and injects it directly into your final executable.
- ar rcs is not a native command of the C programming language
- ar is an operating system command-line utility (typically Unix, Linux, and similar systems, and included in tool collections such as GNU Binutils), which stands for archiver
- The ar rcs command is an external tool used in the C compile and link process to create static libraries
- The command is used to create a .a file (static file or static library) from one or more .o object files
ar rcs <library_name.a> <file1.o> <file2.o> …
- The analogy of “putting objects (.o) into a box (.a)” is perfect for understanding the concept of a static library
- .o : These contain the compiled code of a source function or module, but cannot yet be executed on their own
- .a : Groups multiple .o files into a single file to simplify the linking process with other programs
- ar rcs: The utility that creates and maintains the .a file
- Remember: ar rcs is not a C utility but an operating system utility.
When you compile a main C program that uses a static library (.a), the linker looks inside that “box” and extracts only the .o files it needs to include in the final executable, making the program complete.
example:
Compile implementations to object (.o) files
gcc -c math_utils.c -o build/math_utils.o
gcc -c string_utils.c -o build/string_utils.o
gcc -c file_io.c -o build/file_io.o
Package all .o files into a single library (.a)
ar rcs build/libutils.a \
build/math_utils.o \
build/string_utils.o \
build/file_io.o
The build/libutils.a file will now contain the compiled code for the three utility functions (math, string, and file)
When another project needs to use any of those functions, it only has to link against a single library (-lutils), and the linker will extract only the functions it really needs from that large archive
two repositories you can experiment with: