5

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

Vlada
  • 61
  • 1
  • 2
  • 1
    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 Aug 15 '17 at 17:41
  • I tried ia32 , but it did not work too. – Vlada Aug 15 '17 at 20:24
  • 1
    As you found out, it would be easier to read the docs than to go by trial and error. – tim18 Aug 15 '17 at 20:38

2 Answers2

5

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.

M. Chinoune
  • 409
  • 5
  • 10
0

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

Ehsan
  • 380
  • 4
  • 9
  • Welcome to Stack Overflow! While links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Aug 09 '18 at 14:04