title: Compiling Fortran code using BLAS (LAPACK) modules
body: <p>I've just started to use Fortran and have a problem with using BLAS modules from (?) LAPACK in my program.</p>
  
  <p>I'm working on Ubuntu 18.04, I've already installed (?) LAPACK and BLAS packages by command <code>sudo apt-get install libblas-dev liblapack-dev</code>. I've also downloaded LAPACK from <a href="http://www.netlib.org/lapack/" rel="nofollow noreferrer">http://www.netlib.org/lapack/</a> website and install it according to <a href="https://www.youtube.com/watch?v=5N1QYWxAAL4" rel="nofollow noreferrer" title="Downloading and installing LAPACK">this video</a>.</p>
  
  <p>My program is supposed to use <code>blas_sparse</code> module, so I wrote something like that:</p>
  
  <pre><code>program example
  
  use blas_sparse
  
  implicit none
  
      ! some code ...
  
  end program example
  </code></pre>
  
  <p>I'm trying to compile my code (<code>example.F90</code>) using <code>gfortran</code> with a following command:</p>
  
  <pre><code>gfortran example.F90 -llapack -lblas
  </code></pre>
  
  <p>like on <a href="https://www.youtube.com/watch?v=TsX2cRsDdNI" rel="nofollow noreferrer" title="Installing BLAS and LAPACK">this example video</a> (1:11). However it does not work, the compiler returns:</p>
  
  <pre><code>example.F90:3:8:
  
   use blas_sparse
      1
  Fatal Error: Can't open module file ‘blas_sparse.mod’ for reading at (1): No such file or directory
  </code></pre>
  
  <p>What's the problem in here? I see three potential reasons:</p>
  
  <ol>
  <li>The code is wrong.</li>
  <li>The command I'm using to compile is incorrect.</li>
  <li>LAPACK and BLAS are not really installed.</li>
  </ol>
  
  <p>ad. 1.
  Maybe I should add some module named <code>blas_sparse</code> to my code? Moreover it just moves the problem to another place bacause I still need to import somehow procedures from BLAS. Unfortunately I don't really know how does <code>use</code> command work, so I don't know what should I try to change.</p>
  
  <p>ad. 2.
  Maybe my compiler just don't know where <code>-llapack</code> and <code>-lblas</code> libraries are. I was trying trying to write it explicitly using </p>
  
  <pre><code>gfortran example.F90 -llapack -lblas -L/usr/local/lib
  </code></pre>
  
  <p>comand, because when installing LAPACK and BLAS I've created symbolic links to <code>liblapack.a</code> and <code>liblapack.a</code> (see ad. 3.). I don't know what are exactly <code>-llapack</code> and <code>-lblas</code> flags and it makes me difficult to check if the compiler 'can see' them.</p>
  
  <p>ad. 3.
  What I did to install BLAS and LAPACK on my computer is:</p>
  
  <ul>
  <li>downloading LAPACK 3.8.0 from netlib.org/lapack website</li>
  <li>using following commands to extract the library and install the files on my computer</li>
  </ul>
  
  <p>:</p>
  
  <pre><code>tar zxvf lapack-3.8.0.tar.gz
  cd lapack-3.8.0/
  cp make.inc.example make.inc
  make blaslib
  make lapacklib
  sudo ln -s /home/Download/lapack-3.8.0/liblapack.a /usr/local/lib/liblapack.a
  sudo ln -s /home/Download/lapack-3.8.0/librefblas.a /usr/local/lib/librefblas.a
  </code></pre>
  
  <p>And also installing it via <code>apt-get</code>:</p>
  
  <pre><code>sudo apt-get install libblas-dev liblapack-dev
  </code></pre>
  
  <p>I'll be very thankful for any help to solve this problem.</p>
  
  <p>EDIT:</p>
  
  <p>I've actually found out that I should create those <code>.mod</code> files using <code>blas_sparse.f90</code>, <code>blas_sparse_namedconstant.f90</code> and <code>blas_sparse_proto.h</code> files from <a href="http://www.netlib.org/blas/blast-forum/" rel="nofollow noreferrer" title="BLAS Technical Forum">BLAST website</a>. First two of them I've just added to my code as modules and the gfortan compiler creates proper (I hope) <code>.mod</code> files. However the third one is written in C language, so I can't just put it into my code.</p>
  
  <p>So now the question is how to bound the <code>.h</code> file with my code. Do I need to create some <code>makefile</code> or compile <code>.h</code> file to create the <code>.mod</code> file?</p>
  
  <p>EDIT2:</p>
  
  <p>It looks that just I didn't install sparse BLAS from the <a href="http://www.netlib.org/blas/blast-forum/" rel="nofollow noreferrer" title="BLAS Technical Forum">BLAST website</a>. What I have should done was:</p>
  
  <ul>
  <li>downloading the library:</li>
  </ul>
  
  <pre><code>wget -P target/directory/ http://www.netlib.org/toms/818
  </code></pre>
  
  <ul>
  <li>unpacking the file:</li>
  </ul>
  
  <pre><code>mv 818 818.gz
  gunzip 818.gz
  </code></pre>
  
  <ul>
  <li>removing first three lines and ading <code>.sh</code> extension:</li>
  </ul>
  
  <pre><code>tail -n +4 818 &gt; 818.sh; rm 818
  </code></pre>
  
  <ul>
  <li>installing the library:</li>
  </ul>
  
  <pre><code>bash 818.sh
  # before doing the following step read the notes below
  bash INSTALL # this file has been created by 818.sh
  ./TESTER/test_all # running test
  </code></pre>
  
  <p>What I also needed to do, in the last step, was to edit <code>SOFTWARE/Makefile</code> and <code>INSTALL</code> files and create <code>TESTER/Makefile.GNU</code> file to use <code>gfortran</code> as a compiler.</p>
  
  <p>After all I copied <code>blas_sparse.mod</code> file to the directory where my program is:</p>
  
  <pre><code>cp SOFTWARE/blas_sparse.mod blas_sparse.mod
  </code></pre>
  
  <p>and now I can compile the program using <code>gfortran</code> compiler:</p>
  
  <pre><code>gfortran example.F90 -llapack -lblas
  </code></pre>