title: LAPACKE C++ linking error. Unable to find function
body: <p>I'm looking to use the LAPACKE library to make C/C++ calls to the LAPACK library.  On multiple devices, I have tried to compile a simple program, but it appears LAPACKE is not linking correctly.</p>
  <p>Here is my code, slightly modified from <a href="https://stackoverflow.com/questions/3519959/computing-the-inverse-of-a-matrix-using-lapack-in-c/41474839">this example</a>:</p>
  <pre><code>#include &lt;cstdio&gt;
  
  extern &quot;C&quot;
  {
  #include &quot;lapacke.h&quot;
  }
  // extern &quot;C&quot; {
  //     // LU decomoposition of a general matrix
  //     void LAPACK_dgetrf(int* M, int *N, double* A, int* lda, int* IPIV, int* INFO);
  
  //     // generate inverse of a matrix given its LU decomposition
  //     void LAPACK_dgetri(int* N, double* A, int* lda, int* IPIV, double* WORK, int* lwork, int* INFO);
  // }
  
  extern &quot;C&quot;
  {
      #include &lt;lapacke.h&gt;
  }
  
  void inverse(double* A, int N)
  {
      int *IPIV = new int[N];
      int LWORK = N*N;
      double *WORK = new double[LWORK];
      int INFO;
  
      dgetrf_(&amp;N,&amp;N,A,&amp;N,IPIV,&amp;INFO);
      dgetri_(&amp;N,A,&amp;N,IPIV,WORK,&amp;LWORK,&amp;INFO);
  
      delete[] IPIV;
      delete[] WORK;
  }
  
  int main(){
  
      double A [2*2] = {
          1,2,
          3,4
      };
  
      inverse(A, 2);
  
      printf(&quot;%f %f\n&quot;, A[0], A[1]);
      printf(&quot;%f %f\n&quot;, A[2], A[3]);
  
      return 0;
  }
  </code></pre>
  <p>I have installed blas, lapack and lapacke using the apt package manager:</p>
  <pre><code>sudo apt-get install libblas-dev liblapack-dev liblapacke-dev
  </code></pre>
  <p>I am compiling with:</p>
  <pre><code>g++ -lblas -llapack -llapacke -I /usr/include main.cpp
  </code></pre>
  <p>the -I shouldn't be neccisary as far as I know, but I put it in anyway to be sure.  I get the following error:</p>
  <pre><code>'/usr/bin/ld: /tmp/ccJSTpTf.o: in function `inverse(double*, int)':
  main.cpp:(.text+0x9f): undefined reference to `dgetrf_'
  /usr/bin/ld: main.cpp:(.text+0xc8): undefined reference to `dgetri_'
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>This problem persists regardless if I compile lapacke from source, import the entire header, declare the functions themselves as extern, or change the names of the function to any of the following: <strong>dgetrf()</strong>, <strong>dgetrf_()</strong>, or <strong>LAPACK_dgetrf()</strong>, all common names I have seen for LAPACK-sytle calls.</p>
  <p>Looking through the Lapacke header, it looks like LAPACK_dgetrf() translates to a lapack function call dgetrf_().  Unwrapping the static archives to look for dgetrf, I can find that liblapack.a has a function object dgetrf.o, and liblapacke.a has a function lapack_dgetrf.o.  I'm thinking the root of the problem is that LAPACK_dgetrf() is basically dgetrf_(), and the linker cannot find dgetrf_(), because LAPACK has the function called dgetrf(), not dgetrf_().  I'm very confused though, as the compiler did not complain that any of the linked libraries didn't exist, and nobody else seems to have this issue on their devices, while I've had it now on 3 separate ones (all ubuntu based).</p>
  <p>What do I need to do to get LAPACKE to compile, not just for C, but for C++?</p>