Pjames
Pjames

Reputation: 1

`undefined reference to `sgtsv_'` when compiling a tridiagonal solver

I'm working on a problem for one of my computing courses and I was getting stuck in an error after trying to work through it for some time.

Specifically, I'm working on this Fortran routine:

program linsolve_sgtsv
implicit none
integer, parameter :: lda=10,ldb=lda,nrhs=1
real :: dl(lda-1),d(lda),du(lda-1),b(ldb,nrhs)
integer :: n,info

n = 10
dl(1:n-1) = 1+(1/11); d(1:n) = 2; du(1:n-1) = 1-(1/11)
b(1:n,1) = (/1,1,1,1,1,1,1,1,1,1/)
call sgtsv(n,nrhs,dl,d,du,b,ldb,info)
if (info==0) then
    print *, '--- x is ----------------'
    print '(g22.16)', b(1:n,1)
else
    print *, 'info = ',info
end if

end program linsolve_sgtsv

This routine solves linear systems using gaussian elimination when the matrices are tridiagonal.

However, when I try to create a makefile using:

gfortran linsolve_sgtsv.f90 -o linsolve.f90

I get the following errors:

tmp/ccCiRHve.o: In function `MAIN__':
linsolve_sgtsv.f90:(.text+0x100): undefined reference to `sgtsv_'
collect2: error: ld returned 1 exit status

Upvotes: 0

Views: 472

Answers (1)

I can't find any direct duplicate. Many people have similar problems, but they usually know a bit more and your question is more basic.

The tridiagonal solver sgtsv() is not a Fortran built-in subroutine. It is provided in a library called LAPACK and many other implementations (e.g., MKL). That means you must tell the compiler it should include (link) that library.

That is done by adding -llapack (-l means "link" and lapack is the name of the library which is stored in a file called liblapack.so or liblapack.a) at the end of the compile command.

You must have his library installed in your computer for the link flag to work. In many Linux distributions it is included in the repositories.

The correct line would be

gfortran linsolve_sgtsv.f90 -o linsolve -llapack

If the library (liblapack.a or liblapack.so) is installed in some directory the compiler can't see, you must use

gfortran linsolve_sgtsv.f90 -o linsolve -L/path/to/liblapack -llapack

See also gfortran LAPACK "undefined reference" error and your compiler's manual for more.


Notice I did -o linsolve instead of -o linsolve.f90. It is a very bad idea to name your executable file with a .f90 extension. Only Fortran source files should be named .f90.


There will be more problems in your code. They will appear when you start to test it. For example, yoy do

 (1/11)

this will not return the mathematical result of 1.0/11.0, it will return 0. Learn about integer division in Fortran. See Why are the elements of an array formatted as zeros when they are multiplied by 1/2 or 1/3? and Simple program, that only produce zeros, bug?

If you will find more problems after you can run your code, you can ask a new question.

I recommend to compile your programs with -g -fcheck=all -Wall to find more bugs and errors more easily.

Upvotes: 2

Related Questions