Reputation: 26022
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.
Upvotes: 7
Views: 4865
Reputation: 33628
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.
Upvotes: 11
Reputation: 1423
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)."
Upvotes: 4