Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of object files in static library

I know that when linking to multiple static libraries or object files, the order matters (dependent libraries should be listed before their dependencies). I want to know if, when creating a library file with ar, this same rule applies and the order within the library matters, or within the same .a file it doesn't make a difference.
I am packing 200+ object files with a complicated dependency graph, and doing

ar rcs mylib.a objs/*.o

is considerably easier then listing them in the correct order.

I am using gcc, if it makes a difference.

like image 302
Baruch Avatar asked Dec 29 '11 23:12

Baruch


People also ask

Does order of static libraries matter?

On Windows, the order of static libraries does matter if a symbol is defined in more than one library. On Linux, it matters when one static library references another.

Why does it matter what order we list libraries on the command line?

It will keep track of the functions used by preceding static libraries, permanently tossing out those functions that are not used from its lookup tables. The result is that if you link a static library too early, then the functions in that library are no longer available to static libraries later on the link line.

Does order matter in gcc?

The gcc program accepts options and file names as operands. Many options have multi-letter names; therefore multiple single-letter options may not be grouped: -dr is very different from -d -r. You can mix options and other arguments. For the most part, the order you use doesn't matter.

What is a static library file?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.


1 Answers

The order within the library used to matter long time ago.

It no longer matters on any UNIX system newer than ~15-20 years. From man ranlib:

   An archive with such an index speeds up linking to the library
   and allows routines in the library to call each other without
   regard to their placement in the archive.

Most non-ancient UNIX systems either produce the __.SYMDEF (which contains above index) automatically while building the archive library, or build it in-memory at link time.

like image 175
Employed Russian Avatar answered Sep 30 '22 21:09

Employed Russian