Linking LAPACK/BLAS libraries

2019-07-28 05:43发布

Background:
I am working on a project written in a mix of C and Fortran 77 and now need to link the LAPACK/BLAS libraries to the project (all in a Linux environment). The LAPACK in question is version 3.2.1 (including BLAS) from netlib.org. The libraries were compiled using the top level Makefile (make lapacklib and make blaslib).

Problem:
During linking, error messages claimed that certain (not all) BLAS-routines called from LAPACK-routines were undefined. This gave me some headache but the problem was eventually solved when (in the Makefile) the order of appearance of the libraries to be linked was changed.

Code:
In the following, (a) gives errors while (b) does not. The linking is performed by (c).
(a) LIBS = $(LAPACK)/blas_LINUX.a $(LAPACK)/lapack_LINUX.a
(b) LIBS = $(LAPACK)/lapack_LINUX.a $(LAPACK)/blas_LINUX.a
(c) gcc -Wall -O -o $@ project.o project.a $(LIBS)

Question:
What could be the reason for the undefined references of only some routines and what makes the order of appearance relevant?

3条回答
Animai°情兽
2楼-- · 2019-07-28 06:26

Typically one always puts the "more fundamental/basic" library to the right of the "less fundamental/basic" - ie, the linker will look to the right of a file for the definition of a function appearing in said file. This is supposedly not necessary any more with modern linkers, but it's always a good idea (as in your case). I'm not sure why it only mattered with several routines.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-28 06:39

Is clapack used as a LAPACK implementation? If no you can try to use it.

查看更多
贼婆χ
4楼-- · 2019-07-28 06:42

The LAPACK library needs stuff from BLAS, and the linker searches from left to right. So, putting BLAS after LAPACK (option (b)), worked.

If you want it to always work, regardless of the order, you can use linker groups:

-Wl,--start-group $(LAPACK)/blas_LINUX.a $(LAPACK)/lapack_LINUX.a -Wl,--end-group

That tells the linker to loop through the libraries until all symbols get resolved (or until it notices that looping again won't help).

查看更多
登录 后发表回答