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

2

7

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.

Bailee answered 13/8, 2014 at 21:5 Comment(1)
M
4

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

Morgan answered 18/8, 2014 at 9:57 Comment(0)
T
11

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.

Tom answered 7/7, 2016 at 11:22 Comment(0)
M
4

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

Morgan answered 18/8, 2014 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.