4
votes

I have a problem to link lapack to fortran example program. Here is the program example.f95

Program LinearEquations
! solving the matrix equation A*x=b using LAPACK
Implicit none

! declarations
double precision :: A(3,3), b(3)
integer :: i, pivot(3), ok

! matrix A
A(1,:)=(/3, 1, 3/)
A(2,:)=(/1, 5, 9/)
A(3,:)=(/2, 6, 5/)

! vector b
b(:)=(/-1, 3, -3/)
!b(:)=(/2, 2, 9/)

! find the solution using the LAPACK routine DGESV
call DGESV(3, 1, A, 3, pivot, b, 3, ok)

! print the solution x
do i=1, 3
  write(*,9) i, b(i)
end do  

9  format('x[', i1, ']= ', f5.2)  
end program LinearEquations

I have installed libraries here

/opt/intel/compilers_and_libraries_2017.4.196/linux/mkl/lib/intel64_lin/libmkl_lapack95_ilp64.a

I am using gfortran to compile program:

gfortran -o example example.f95 -L/opt/intel/compilers_and_libraries_2017.4.196/linux/mkl/lib/intel64_lin/libmkl_lapack95_ilp64.a

it complain

 /tmp/ccWtxMFP.o: In function `MAIN__':
 example.f95:(.text+0xf0): undefined reference to `dgesv_'
 collect2: error: ld returned 1 exit status

Can someone help me with this issue please ? Many thanks

2
You appear to be using default 32-bit integers while specifying a library which requires 64-bit integers. Refer to Intel mkl link advisor applet for a complete set of required libraries. - tim18
I tried ia32 , but it did not work too. - Vlada
As you found out, it would be easier to read the docs than to go by trial and error. - tim18

2 Answers

4
votes

There are two types of linking :

  • Static Linking : you link your program with static library.

    example: gfortran program.f90 /path-to-lib/libmy.a -o program.x

  • Dynamic Linking : you link your program with a shared library :

    example : gfortran program.f90 -L/path-to-lib -lmy -o program.x

    which link your program with libmy.so .

According to MKL Advisor you should use this:

-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_gf_lp64.a ${MKLROOT}/lib/intel64/libmkl_sequential.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl

for static linking. or :

-L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl

for dynamic linking.

Where ${MKLROOT} is the path to MKL.

0
votes

In fact, Intel has a nice tool which provides you the correct linking and compiling flags after you specify your toolchain, platform etc. You may find it here (which is behind the Intel login page): https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor