首页 > 解决方案 > 在 Fedora Linux 上使用 gfortran 与 LAPACK 链接

问题描述

我需要一些关于在 Fedora 机器上安装 LAPACK(Fortran 95)的帮助。

在尝试了此论坛中相关查询的所有答案后,我正在写这篇文章。

我尝试了两种不同的选择:

方法一

我直接从这里下载了lapack95.a库(http://www.netlib.org/lapack95/,文件名为lapack95_linux_redhat.tgz。解压后,我可以得到文件lapack95.a

在寻找有关如何链接和使用库的一些答案后,我编写了一个试用代码并保存在文件 try.f90 中,然后执行以下操作

gfortran -c try.f90

gfortran -o try try.o -L/(name of directory where lapack95.a
                          was present. I put it in the same directory
                          as try.f90)/ -llapack95

fortran 文件 try.f90 和 lapack95.a 在同一目录中

我收到以下错误:

/usr/bin/ld: cannot find -llapack95
collect2: error: ld returned 1 exit status

我在互联网上搜索,发现这里 ( https://forums.fedoraforum.org/archive/index.php/t-248227.html ) 我们需要安装 lapack-devel。我这样做了,结果也一样。

方法二

我也尝试运行上述命令(在方法 1 中),但 / 和 -llapack95 之间没有空格,即:

gfortran -o try try.o -L/(name of directory where lapack95.a 
                          was present I put it in the same directory
                         as try.f90)/-llapack95

并收到以下错误:

try.o: In function `MAIN__':
try.f90:(.text+0xdb): undefined reference to `sgesv_'
collect2: error: ld returned 1 exit status

方法三

没有成功后,我觉得直接下载的lapack95.a可能不好用。我应该从原始文件构建它。

我意识到我可能需要通过适当的方式安装 lapack95,然后从同一个链接 http://www.netlib.org/lapack95

我下载了文件 lapack95.tgz 并按照自述文件中的说明进行操作。(说只是在 SRC 中运行 make 文件)

我做了一些改变:

FC1      = f95 -fixed  to 

FC1      = f95 

OPTS0    = -u -V -dcfuns -dusty -ieee=full 

OPTS0    = -u -V 

(如https://www.reddit.com/r/linux4noobs/comments/7tgmb9/fortran_library_netlib_lapack95_installation_error/上的建议)

我预计该库将在 /usr/lib/ 或 usr/local/lib 中创建为 LAPACK3 但找不到它。

事实上有一个错误(因此不能有任何 .a 文件)

f95 -free -c -u -V - -I./../lapack95_modules f77_lapack_double.f90 f95: 错误:当输入来自标准输入时需要 -E 或 -x make: *** [../make.inc: 45: f77_lapack_double.o] 错误 1

我不确定我的任何方法是否都可以。我希望方法 1 能够工作,因为我在本地链接库,但在所有 3 种情况下都失败了。

请建议。谢谢

标签: fortranlinker-errorsfedoragfortranlapack

解决方案


Lapack 库是一个非常有用的工具,可以快速进行矩阵运算。在 linux 机器上,使用内置工具安装 lapack 非常简单:dnf/yum 或 apt-get [2]。在我的 Fedora 桌面上,安装是这样的:

sudo dnf install lapack64-devel.x86_64

这是一个关于如何使用 lapack lib 的小例子。源代码是求解线性方程为linear_eqs.f [3]。

    Program LinearEquations
c solving the matrix equation A*x=b using LAPACK
    Implicit none
c declarations, notice single precision
    Real*4 A(3,3), b(3)
    integer i, j, pivot(3), ok
c define matrix A
    A(1,1)=3.1
    A(1,2)=1.3
    A(1,3)=-5.7
    A(2,1)=1.0
    A(2,2)=-6.9
    A(2,3)=5.8
    A(3,1)=3.4
    A(3,2)=7.2
    A(3,3)=-8.8
c define vector b, make b a matrix and you can solve multiple
c equations with the same A but different b
    b(1)=-1.3
    b(2)=-0.1
    b(3)=1.8
c find the solution using the LAPACK routine SGESV
    call SGESV(3, 1, A, 3, pivot, b, 3, ok)
c
c parameters in the order as they appear in the function call
c    order of matrix A, number of right hand sides (b), matrix A,
c    leading dimension of A, array that records pivoting, 
c    result vector b on entry, x on exit, leading dimension of b
c    return value   
c   
c print the vector x
    do i=1, 3
       write(*,*) b(i)
    end do
    end

在此代码中,使用了一个 lapack 函数“sgesv()”。要执行此文件,请运行以下命令:

gfortran linear_eqs.f -llapack -o lineqs.out
./lineqs.out

输出将是:

1.00000024    
1.00000036    
1.00000036 

参考

[1] https://github.com/fxcoudert/gfortran-for-macOS/releases

[2] BLAS、ATLAS、LAPACK 共享库最小示例

[3] http://physics.oregonstate.edu/~landaur/nacphy/lapack/codes/linear-f.html


推荐阅读