title: Failing to link c code to lapack: undefined reference
body: <p>I am trying to use lapack functions from C.</p>
  
  <p>Here is some test code, copied from <a href="https://stackoverflow.com/questions/24427802/fail-to-link-c-code-to-lapack-blas-undefined-reference">this question</a></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include "clapack.h"
  #include "cblas.h"
  
  void invertMatrix(float *a, unsigned int height){
    int info, ipiv[height];
    info = clapack_sgetrf(CblasColMajor, height, height, a, height, ipiv);
    info = clapack_sgetri(CblasColMajor, height, a, height, ipiv);
  }
  
  void displayMatrix(float *a, unsigned int height, unsigned int width)
  {
    int i, j;
    for(i = 0; i &lt; height; i++){
      for(j = 0; j &lt; width; j++)
        {
          printf("%1.3f ", a[height*j + i]);
        }
      printf("\n");
    }
    printf("\n");
  }
  
  
  int main(int argc, char *argv[])
  {
    int i;
    float a[9], b[9], c[9];
    srand(time(NULL));
    for(i = 0; i &lt; 9; i++)
      {
        a[i] = 1.0f*rand()/RAND_MAX;
        b[i] = a[i];
      }
    displayMatrix(a, 3, 3);
    return 0;
  }
  </code></pre>
  
  <p>I compile this with gcc:</p>
  
  <pre><code>gcc -o test test.c  \
      -lblas -llapack -lf2c
  </code></pre>
  
  <p>n.b.: I've tried those libraries in various orders, I've also tried others libs like latlas, lcblas, lgfortran, etc.</p>
  
  <p>The error message is:</p>
  
  <pre><code>/tmp//cc8JMnRT.o: In function `invertMatrix':
  test.c:(.text+0x94): undefined reference to `clapack_sgetrf'
  test.c:(.text+0xb4): undefined reference to `clapack_sgetri'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>clapack.h is found and included (installed as part of atlas).  clapack.h includes the offending functions --- so how can they not be found?</p>
  
  <p>The symbols are actually in the library libalapack (found using <code>strings</code>).  However, adding -lalapack to the gcc command seems to require adding -lcblas (lots of undefined cblas_* references).  Installing cblas automatically uninstalls atlas, which removes clapack.h.</p>
  
  <p>So, this feels like some kind of dependency hell.</p>
  
  <p>I am on FreeBSD 10 amd64, all the relevant libraries seem to be installed and on the right paths.</p>
  
  <p>Any help much appreciated.</p>
  
  <p>Thanks</p>
  
  <p>Ivan</p>