连接 LAPACK/BLAS 库
背景:
我正在开发一个用 C 和 Fortran 77 混合编写的项目,现在需要将 LAPACK/BLAS 库链接到该项目(全部在 Linux 环境中)。有问题的 LAPACK 是来自 netlib.org 的 3.2.1 版本(包括 BLAS)。这些库是使用顶级 Makefile(make lapacklib 和 make blaslib)编译的。
问题:
在链接期间,错误消息声称从 LAPACK 例程调用的某些(不是全部)BLAS 例程未定义。这让我有些头疼,但当(在 Makefile 中)要链接的库的出现顺序发生变化时,问题最终得到解决。
代码:
下面,(a) 给出错误,而(b) 则没有。链接由 (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)
问题:
仅某些例程的未定义引用的原因可能是什么?是什么使出现的顺序相关?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,人们总是将“更基本/基本”库放在“不太基本/基本”库的右侧 - 即,链接器将在文件的右侧查找出现在该文件中的函数的定义。对于现代链接器来说,这不再是必要的,但这始终是一个好主意(就像您的情况一样)。我不知道为什么它只与几个例程有关。
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.
clapack 是否用作 LAPACK 实现?如果没有你可以尝试使用它。
Is clapack used as a LAPACK implementation? If no you can try to use it.
LAPACK 库需要 BLAS 的内容,链接器从左到右搜索。因此,将 BLAS 放在 LAPACK 之后(选项 (b))是有效的。
如果您希望它始终工作,无论顺序如何,您可以使用链接器组:
这告诉链接器循环遍历库,直到所有符号都得到解析(或者直到它注意到再次循环没有帮助)。
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:
That tells the linker to loop through the libraries until all symbols get resolved (or until it notices that looping again won't help).