Link to home
Start Free TrialLog in
Avatar of smathew
smathew

asked on

Linking libraries

Why is that the order in which the libraries are linked does matter ?. Otherwise, a symbol referencing error occurs !!!
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of josv
josv

It depends on which OS and/or linker you are using.

Traditionally, Unix uses a one-pass linker. That is, the linker goes through the list of objects and libraries only once. Whenever it encounters and object, it adds that to the executable image. Any unresolved references in that object are added to the global list of "references still to be resolved".

If the linker encounters a library, it sees which objects it must pull from the library, and add to the image. Only objects that are necessary to solve one or more of the currently known unresolved references are retrieved from the library. Any unresolved references in the objects pulled from the library are added to the unresolved reference list.

(Unix has a tool called "lorder" that can be used in combination with ar to sort the objects in a library in reference->referenced order.

If the linker then goes on and encounters another object that needs a reference from a a library that it has already processed, tough luck!

I know that modern linkers repeat the searching through the libraries until they have either resolved all references, or references remain unresolved (i.e. are not in any of the libraries).

++Jos
Avatar of smathew

ASKER

Thanks josv for the detailed explaination.
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.
Moondancer
Moderator @ Experts Exchange