title: Errors in linking fortran code that imports a MAT-file
body: <p>I have to import a MAT-file in a fortran program. I followed the <a href="http://www-rohan.sdsu.edu/doc/matlab/techdoc/matlab_external/ch05ma11.html" rel="nofollow">example file</a> but I am facing some problems while linking. The compilation happens fine.</p>
  
  <p>Minimal code:</p>
  
  <pre><code>#include "fintrf.h"
  
  PROGRAM main 
  
  USE ssa
  USE dmotifs
  USE param
  
  IMPLICIT NONE
  
  ! MAT-FILE Declarations !
  
  INTEGER matOpen, matGetDir 
  INTEGER matGetVariableInfo
  INTEGER mp, dir, adir(100), pa
  INTEGER mxGetM, mxGetN, matClose
  INTEGER ndir, i, clstat
  CHARACTER*32 names(100) 
  
  !===========================!
  
  if(all(fnames(:)%fn .NE. argfun)) then
      write(*,*) "No such motif: ",argfun
      write(*,*) "Input format-&gt; main &lt;motifname&gt;"
      stop
  else
  
      fin=fchton(argfun)  
      y0=nM2m*analys(p,argfun)
  
      ! ==&gt; OPEN MAT-file &lt;== !
  
      mp=matOpen('./PRMS_lxr_29Apr15.mat','r')
  
      if (mp .eq. 0) then
           write(6,*) "Can't open MAT-file"
           stop
      end if
  
      dir = matgetdir(mp, ndir)
  
        if (dir .eq. 0) then
           write(6,*) "Can't read MAT-file-directory."
           stop
      endif
  
      call mxCopyPtrToPtrArray(dir, adir, ndir)
  
      do 20 i=1,ndir
           call mxCopyPtrToCharacter(adir(i), names(i), 32)
   20 continue
  
      write(6,*) 'Directory of Mat-file:'
  
      do 30 i=1,ndir
          write(6,*) names(i)
   30 continue
  
      write(6,*) 'Getting Header info from first array.'
      pa = matGetVariableInfo(mp, names(1))
      write(6,*) 'Retrieved ', names(1)
      write(6,*) '  With size ', mxGetM(pa), '-by-', mxGetN(pa)
      call mxDestroyArray(pa)
  
      clstat=matClose(mp)
  
  end if
  END PROGRAM main
  </code></pre>
  
  <p>I am using gfortran 4.8.3 for compiling+linking using the default command:</p>
  
  <pre><code>gfortran main.f90 dmotifs.o param.o ssa.o -o main
  </code></pre>
  
  <p>This code compiles fine (without linking) when I do not include: <code>#include "finitrf.h"</code>, otherwise the compiler says</p>
  
  <pre><code>Warning: main.f90:1: Illegal preprocessor directive
  </code></pre>
  
  <p>I tried renaming <code>finitrf.h</code> to <code>finitrf.f90</code> but it did not make any difference. Nonetheless during linking I am getting these errors:</p>
  
  <pre><code>main.f90:(.text+0x3ea): undefined reference to `matopen_'
  main.f90:(.text+0x487): undefined reference to `matgetdir_'
  main.f90:(.text+0x52b): undefined reference to `mxcopyptrtoptrarray_'
  main.f90:(.text+0x583): undefined reference to `mxcopyptrtocharacter_'
  main.f90:(.text+0x71b): undefined reference to `matgetvariableinfo_'
  main.f90:(.text+0x804): undefined reference to `mxgetm_'
  main.f90:(.text+0x855): undefined reference to `mxgetn_'
  main.f90:(.text+0x89c): undefined reference to `mxdestroyarray_'
  main.f90:(.text+0x8b0): undefined reference to `matclose_'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Do I need a makefile or add additional arguments in the compile command?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I added the <code>-cpp</code> option and that eliminates the problem of <code>Illegal preprocessor directive</code></p>
  
  <p>Now when I am compiling with paths to matlab external components (where finitf.h) is, I am still getting the same error.</p>
  
  <pre><code>gfortran main.f90 dmotifs.o param.o ssa.o -I/usr/local/matlab2008a/extern/include -L/usr/local/matlab2008a/extern/lib -cpp -o main
  </code></pre>
  
  <p>If I provide library path to <code>/usr/local/matlab2008a/bin/glnxa64</code> that contains other matlab libraries including <code>libmat.so</code>, I still get the same errors. </p>