I did not find any proper information if and why I need ranlib
/ ar -s
for static linking.
Assume I have an application that consists of multiple modules. Each module has its code files in its own folder, and the object files are created in their own folder: module1/%.c → bin/module1/%.o
. For each module I create an .a
file: ar -rc bin/module1.a bin/module1/….o
. The whole program gets compiled with gcc bin/module1.a … moduleN.a -o bin/app
.
In this scenario what does creating an index for the .a
file do? The compilation and program works just fine even if I don't add indexes to the .a
files. But every example that I found called ranlib
after the last object file was added to the archive.
The question is not Linux / Mac / Windows specific.
ranlib command in Linux is used to generate index to archive. ranlib generates an index to the contents of an archive and it will be stored in the archive. The index lists each symbol defined by a member of an archive which is simply relocatable object file.
Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.
ar -rcs is the most likely command you would use when using a Makefile to compile a library. r means that if the library already exists, replace the old files within the library with your new files. c means create the library if it did not exist.
A static library, e.g. libfoo. a is not an executable of any kind. It is simply an indexed archive in unix ar format of other files which happen to be ELF object files.
If you're on a POSIX-compatible system, no. According to the specification:
Whenever the ar utility is used to create or update the contents of such an archive, the symbol table is rebuilt.
The only use for ar -s
or ranlib
is to rebuild the symbol table after it has been stripped with the strip
command.
From 'Building And Using Static And Shared "C" Libraries': (http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html)
"After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won't matter during compilation (this will be better understood when we take a deeper look at the link process at the end of this tutorial)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With