order of object files in static library
Asked Answered
T

1

7

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.

Tormentor answered 29/12, 2011 at 23:3 Comment(0)
F
9

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.

Fritzsche answered 29/12, 2011 at 23:7 Comment(1)
"The order within the library used to matter long time ago...." - that's not necessarily true due to C++ static object initialization order.Manutius

© 2022 - 2024 — McMap. All rights reserved.