theory of order of linking libraries

P

post1000

I've recently been compiling numeric programs involving both C and
FORTRAN libraries. It would seem that the order of the libraries
linked seems critical, which, I suppose, is not surprising. What I am
trying to get a handle on is if there is general rules for the link
order. My operating system is linux and I am using the gcc/gfortran
compiliers (I can give more information on the versions if it would be
helpful). I am linking statically and the libraries that I am
explicitly linking are

npsol - a FORTRAN library for constrained optimization
lapack - a FORTRAN library of high level linear algebra algorithms
blas - a FORTRAN library of low level linear algebra algorithms
libgfortran.a - the gfortran standard library
libm.a - the standard C math library

The libraries first in the list make calls to libraries later in the
list. If I link in this order, then the program will compile, but not
in other orders (though I have not tried all other orders).

My question is this: Must one link so that libraries linked first can
call routines in libraries linked later, but not vice versa, or is
this simply an accident of the particular libraries I am using.

Thanks in advance for any advise on this question

Daniel
 
T

Tim Prince

I've recently been compiling numeric programs involving both C and
FORTRAN libraries. It would seem that the order of the libraries
linked seems critical, which, I suppose, is not surprising. What I am
trying to get a handle on is if there is general rules for the link
order. My operating system is linux and I am using the gcc/gfortran

www.gnu.org/software/binutils/manual/ld-<your chosen version>

Questions may be topical on binutils, gcc-help, or gfortran mail lists.
 
G

Gordon Burditt

The libraries first in the list make calls to libraries later in the
list. If I link in this order, then the program will compile, but not
in other orders (though I have not tried all other orders).

My question is this: Must one link so that libraries linked first can
call routines in libraries linked later, but not vice versa, or is
this simply an accident of the particular libraries I am using.

For many linkers, this is the way it works for static linking. If
you have circular references, it may be necessary to list the
libraries TWICE (e.g. -lA -lB -lC -lA -lB -lC). If you have
circular references, the libraries are likely poorly designed, but
you may not have control over that.
 
T

Tim Prince

Gordon said:
For many linkers, this is the way it works for static linking. If
you have circular references, it may be necessary to list the
libraries TWICE (e.g. -lA -lB -lC -lA -lB -lC). If you have
circular references, the libraries are likely poorly designed, but
you may not have control over that.
OP asked about linux (OT), implying the linker is some version of gnu
binutils. Recent versions support the begin-group .... end-group clauses
for dealing with circular references in static libraries. There is far
too much detail to do more than suggest OP start with the docs for the
version of choice, and look in the content of more topical mail lists.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top