Undefined reference to LAPACK and BLAS subroutines

2020-02-07 07:07发布

I'm trying to understand how BLAS and LAPACK in Fortran work and so on, so I made a code that generates a matrix and inverts it.

Here's the code

program test
Implicit none
external ZGETRF
external ZGETRI
integer ::M  
complex*16,allocatable,dimension(:,:)::A
complex*16,allocatable,dimension(:)::WORK
integer,allocatable,dimension(:)::IPIV
integer i,j,info,error

Print*, 'Enter size of the matrix'
Read*, M 

Print*, 'Enter file of the matrix'
READ(*,*), A

OPEN(UNIT=10,FILE = '(/A/)' ,STATUS='OLD',ACTION='READ')

allocate(A(M,M),WORK(M),IPIV(M),stat=error)
    if (error.ne.0)then
        print *,"error:not enough memory"
        stop
    end if

 !definition of the test matrix A
  do i=1,M
  do j=1,M
    if(j.eq.i)then
       A(i,j)=(1,0)
    else 
       A(i,j)=0
    end if
 end do
end do  

 call ZGETRF(M,M,A,M,IPIV,info)
 if(info .eq. 0) then
   write(*,*)"succeded"
 else
  write(*,*)"failed"
 end if

 call ZGETRI(M,A,M,IPIV,WORK,M,info)
  if(info .eq. 0) then
   write(*,*)"succeded"
 else
  write(*,*)"failed"
 end if
 deallocate(A,IPIV,WORK,stat=error)
 if (error.ne.0)then
  print *,"error:fail to release"
  stop
 end if  
 close (10)    
end program test

The matrix A is in a file, which I'm calling, and also I say the size of the matrix (M ). When I copile them with gfortran I get these message

/tmp/ccVkb1zY.o: In function MAIN__': test.f03:(.text+0x751): undefined reference tozgetrf_' test.f03:(.text+0x85d): undefined reference to `zgetri_' collect2: error: ld returned 1 exit status

I have installed BLAS and LAPACK installed so I don't know if I'm calling in a right way the library.

Any suggestion?

1条回答
干净又极端
2楼-- · 2020-02-07 07:32

It looks like you might not have linked to the libraries. Try:

gfortran -o test test.f03 -llapack -lblas

This causes the linker (the program which joins all the program parts together; usually called "ld" on UNIX) to include the library code for the LAPACK call (or a dynamic link to it) in your program.

If the result of the above line is "cannot find -llapack" or similar, there are two common problems:

  1. Libraries can be "shared" (names ending ".so") or "static" (names ending ".a"); the linker will look for the shared one, so if you only have the static one you should add "-static" before the library link:

    gfortran -o test test.f03 -static -llapack -lblas
    

    This will also make it look for the static version of BLAS; if you need the shared version, add "-shared" in front of the "-lblas":

    gfortran -o test test.f03 -static -llapack -shared -lblas

    You might find this page helpful.

  2. The linker isn't looking in the right directory for the libraries. You need to locate the actual library (called something like "liblapack.so" or "liblapack.a") and make sure the directory it's in is included in the directories the linker looks in, e.g. to get it to look in "/mylibs/maths" as well:

    gfortran -o test test.f03 -L/mylibs/maths -llapack -lblas
    
查看更多
登录 后发表回答