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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With