Search code examples
linkergnubinutilsunix-ar

Do I need "ranlib" / "ar -s" for static linking?


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.


Solution

  • 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)."