6

I'm attempting to do a release of some software and am currently working through a script for the build process. I'm stuck on something I never thought I would be, statically linking LAPACK on x86_64 linux. During configuration AC_SEARCH_LIB([main],[lapack]) works, but compilation of the lapack units do not work, for example undefiend reference to 'dsyev_' --no lapack/blas routine goes unnoticed.

I've confirmed I have the libraries installed and even compiled them myself with the appropriate options to make them static with the same results.

Here is an example I had used in my first experience with LAPACK a few years ago that works dynamically, but not statically: http://pastebin.com/cMm3wcwF

The two methods I'm using to compile are the following,

gcc -llapack -o eigen eigen.c
gcc -static -llapack -o eigen eigen.c
nlucaroni
  • 47,556
  • 6
  • 64
  • 86

1 Answers1

8

Your linking order is wrong. Link libraries after the code that requires them, not before. Like this:

gcc -o eigen eigen.c -llapack 
gcc -static -o eigen eigen.c -llapack

That should resolve the linkage problems.


To answer the subsequent question why this works, the GNU ld documentation say this:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

........

Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion.

ie. the linker is going to make one pass through a file looking for unresolved symbols, and it follows files in the order you provide them (ie. "left to right"). If you have not yet specified a dependency when a file is read, the linker will not be able to satisfy the dependency. Every object in the link list is parsed only once.

Note also that GNU ld can do reordering in cases where circular dependencies are detected when linking shared libraries or object files. But static libraries are only parsed for unknown symbols once.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • Really, I could have sworn I had read, through my searches last night/today, that modern linkers will reorder them. This works, but it appears it now cannot find my blas routines --even w/ `-lblas`. Cheers. – nlucaroni Aug 24 '11 at 16:25
  • 1
    other libraries necessary for this to work fully, `-llapack -lblas -lgfortran -lm`. – nlucaroni Aug 24 '11 at 16:43
  • The linker will try and adjust the linkage order to satisfy the graph of active dependencies. But linkage statements are still read "left to right", and if you specify a library for which there is no dependency in the current set, it just gets skipped. Which is what I happening here. – talonmies Aug 24 '11 at 16:53
  • What do you mean, "no dependency in the current set"? There is a dependency, my program depends on it. – nlucaroni Aug 24 '11 at 17:39
  • 1
    But your program hasn't been compiled to an object when the library is specified (remember, read "left to right"), so it is skipped. – talonmies Aug 24 '11 at 17:41
  • then why are the libraries required to be listed in reverse order? For example, `-llapack -lm -lblas -lgfortran` fails in compilation; if it reads left to right, then it should be in the active dependencies --lapack and my application would depend on m. – nlucaroni Aug 24 '11 at 18:03
  • See my edit. Lapack requires Blas, they both require the fortran runtime, and that in turn requires the C math library. if you provide the math library before the fortran library, the linker doesn't yet know it needs it. – talonmies Aug 24 '11 at 18:54
  • My confusion is based on, "The linker will try and adjust linkage order to satisfy the graph of active dependencies". I don't see it and the description you gave doesn't mention it. – nlucaroni Aug 25 '11 at 13:43