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

I did not find any correct information if and why I need ranlib / ar -s for static binding.

Suppose I have an application consisting of several modules. Each module has its own code files in its own folder, and object files are created in their own folder: module1/%.c → bin/module1/%.o For each module, I create a .a file: ar -rc bin/module1.a bin/module1/….o . The whole program is compiled using gcc bin/module1.a … moduleN.a -o bin/app .

In this scenario, what creates an index for the .a do file ? Compilation and the program work fine even if I don't add indexes to the .a files. But every example I found called ranlib after the last object file was added to the archive.

The question is not Linux / Mac / Windows.

+5
source share
2 answers

From the “Creating and Using Static and Shared” C Libraries: ( http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html )

“After creating or modifying an archive, you need to index it. This index is later used by the compiler to speed up the search for characters in the library and to ensure that the order of the characters in the library does not matter at compile time (this will be better understood if we delve into the process links at the end of this lesson).

+3
source

If you are using a POSIX-compatible system, no. According to 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 character table after deleting it with the strip command.

+3
source

Source: https://habr.com/ru/post/1200273/


All Articles