# Calling a Fortran routine from a library which uses LAPACK

**URL:** <https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2020, 2:46pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124 "2020-10-27T14:46:02Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2020, 4:52pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/5 "2020-10-27T16:52:35Z")

</div>

A minimal example here:

The Fortran code, which calls dsyev from Lapack:

File: uselapack.f90

```fortran
subroutine uselapack(M,A)

  implicit none
  double precision :: M(4,4), A(4)

  ! For dsyev
  double precision :: work(12)
  integer :: info

  ! Computing the eigenvalues 'A' and eigenvectors 'M' of the M matrix
  call dsyev('V','U',4,M,4,A,work,12,info)

end subroutine uselapack

```

Compile it with:

```julia
gfortran uselapack.f90 -llapack -shared -o uselapack.so

```

Then, in Julia:

```julia
julia> M = [i == j ? Float64(i+j) : 0. for i in 1:4, j in 1:4]
4×4 Array{Float64,2}:
 2.0 0.0 0.0 0.0
 0.0 4.0 0.0 0.0
 0.0 0.0 6.0 0.0
 0.0 0.0 0.0 8.0

julia> A = zeros(4)
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0

julia> ccall((:uselapack_,"uselapack.so"),Nothing,(Ref{Float64},Ref{Float64}),M,A)

julia> M
4×4 Array{Float64,2}:
 1.0 -0.0 -0.0 0.0
 0.0 1.0 -0.0 0.0
 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 1.0

julia> A
4-element Array{Float64,1}:
 2.0
 4.0
 6.0
 8.0

```

---

_[View the full topic](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124)._
