Common Problems
The successful linkage above relies upon the given order of the libraries. In the abstract case, if a
library A depends upon symbols defined in library B, then A must appear first in the list supplied to
the linker. In our case, LAPACK routines call BLAS routines and so liblapack.a must be
appear before librefblas.a in the given list. If we disrupt that ordering we will precipitate an
error:
% gfortran dgesv.f90 -L$HOME/lapack/lapack-3.4.2 -lrefblas -llapack -o
dgesv-fort-netlib.exe
/home/ggdagw/lapack/lapack-3.4.2/liblapack.a(dgetrf.o): In function
`dgetrf_':
dgetrf.f:(.text+0x3f3): undefined reference to `dtrsm_'
dgetrf.f:(.text+0x4d0): undefined reference to `dgemm_'
/home/ggdagw/lapack/lapack-3.4.2/liblapack.a(dgetrs.o): In function
`dgetrs_':
dgetrs.f:(.text+0x18b): undefined reference to `dtrsm_'
dgetrs.f:(.text+0x1fd): undefined reference to `dtrsm_'
dgetrs.f:(.text+0x375): undefined reference to `dtrsm_'
dgetrs.f:(.text+0x3e7): undefined reference to `dtrsm_'
/home/ggdagw/lapack/lapack-3.4.2/liblapack.a(dgetf2.o): In function
`dgetf2_':
dgetf2.f:(.text+0x1cb): undefined reference to `idamax_'
dgetf2.f:(.text+0x21d): undefined reference to `dswap_'
dgetf2.f:(.text+0x38c): undefined reference to `dger_'
dgetf2.f:(.text+0x3f0): undefined reference to `dscal_'
collect2: ld returned 1 exit status
Another problem commonly encountered is mismatched symbols. When a source code file is
compiled and turned into object code, the names of routines are 'mangled'. They are typically
decorated with leading or trailing underscore (which we can see in the last example). Unfortunately
different compilers often use different mangling strategies and so libraries compiled with compiler
A are not guaranteed to link with other objects compiled with compiler B. In general it is highly
advisable to ensure that all objects given to the linker have been prepared using the same compiler.
If you receive ' undefined reference' errors, and have passed the linker all the required
libraries, then different name-mangling is a likely source of the problem. We can simulate the use
of different compiler by instructing gcc to adopt a different name-mangling strategy for the
program code compared to that used when creating the library:
% gcc dgesv.c -fleading-underscore -I$HOME/lapack/lapack-
3.4.2/lapacke/include -L$HOME/lapack/lapack-3.4.2 -llapacke -llapack
-lrefblas -lgfortran -o dgesv-c-netlib.exe
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In
function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccaognV1.o: In function `_main':
dgesv.c:(.text+0x11a): undefined reference to `_LAPACKE_dgesv'
dgesv.c:(.text+0x146): undefined reference to `_printf'
Gethin Williams 2013