Linking to LAPACK from g++ in cygwin64

  • Thread starter pasmith
  • Start date
  • Tags
    Lapack
In summary, the conversation revolved around an issue with linking the LAPACK library using g++ on cygwin64. The error message "undefined reference to 'zcgesv_'" was encountered and the conversation involved trying to troubleshoot and solve the issue. Suggestions were made to check the symbol table using nm, try different compile switches such as -fno-underscoring, and use pkg-config to retrieve the necessary command line switches. Ultimately, the issue was resolved by correctly setting the PKG_CONFIG_PATH and using the correct libraries in the make rules.
  • #1
pasmith
Science Advisor
Homework Helper
3,072
1,639
I get an 'undefined reference to 'zcgesv_'' error when I try to link the LAPACK library using g++ on cygwin64.

What am I missing? Google has not turned up anything which solves the problem.

(Documentation for zcgesv here )

My Makefile is as follows:
Code:
GCC := /usr/bin/i686-pc-cygwin-g++
LIBS := -L/usr/lib -llapack -lblas

lapacktest.o: lapacktest.cpp
    $(GCC) -c -o lapacktest.o lapacktest.cpp

lapacktest.exe: lapacktest.o
    $(GCC) -o lapacktest.exe $(LIBS) lapacktest.o

lapacktest.cpp contains the following:
Code:
#include<iostream>

using namespace std;

typedef struct{double real;double imag;} Complex;

extern "C" 
     void zcgesv_(
        int*,            // N 
        int*,            // NRHS
        Complex*,         // A
        int*,             // LDA
        int*,             // IPIV
        Complex*,         // B
        int*,             // LDB
        Complex*,         // X
        int*,             // LDX
        Complex*,        // WORK
        Complex*,        // SWORK
        double*,        // RWORK
        int*,            // ITER
        int*            // INFO
    );

    
int main(int argc, char *argv[])
{
    Complex *A, *b, *x, *work, *swork;
    double * rwork;
    int n, nrhs, lda, ldb, ldx, iter, info, *ipiv;
    
    int i,j;
    
    n = 3;
    nrhs = 1;
    lda = ldb = ldx = n;
    
    
    A = new Complex[n*n];
    b = new Complex[n*nrhs];
    x = new Complex[n*nrhs];
    ipiv = new int[n];
    work = new Complex[n*nrhs];
    swork = new Complex[n*(n+nrhs)];
    rwork = new double[n];
    
    for(i = 0; i < n; i++)
    {
        b[i] = Complex{1.0,0.0};
        
       // The matrix I'm interested in is neither diagonal nor sparse, but this is a simple test.

        for(j = 0; j < n; j++)
            A[n*j + i] = (i == j) ? Complex{2.0,1.0} : Complex{0,0};
    }
    
    
    zcgesv_(&n,&nrhs, A, &lda, ipiv, b, &ldb, x, &ldx, work, swork, rwork, &iter, &info);
    
    for(i = 0; i < n; i++)
        cout << "(" << x[i].real << "," << x[i].imag << ")" << endl;
    
    delete[] A;
    delete[] b;
    delete[] x;
    delete[] work;
    delete[] swork;
    delete[] rwork;
    delete[] ipiv;
    
    return 0;
}
 
Technology news on Phys.org
  • #2
The first thing is the _ on the function name. I would dump the symbol table (use nm) and see what it's actually called in the library.
 
  • Like
Likes pasmith
  • #3
Paul Colby said:
The first thing is the _ on the function name. I would dump the symbol table (use nm) and see what it's actually called in the library.

I tried that; sadly nm told me "file fornat not recognised".

Every example I've seen of linkin g LAPACK includes the trailing underscore; in any event, removing the underscore resulted in the linker telling me that "zcgesv" was not defined.

I have now located the source code for LAPACK 3.9.0 and will attempt to compile my own lapack.a

EDIT: nm can cope with the new liblapack.a, but I still get the same error from the linker.

Running nm over zcgesv.o gives this:
Code:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 r .rdata$zzz
00000000 t .text
         U _cgetrf_
         U _cgetrs_
         U _clag2z_
         U _dlamch_
         U _izamax_
         U _xerbla_
         U _zaxpy_
00000000 T _zcgesv_
         U _zgemm_
         U _zgetrf_
         U _zgetrs_
         U _zlacpy_
         U _zlag2c_
         U _zlange_
 
Last edited:
  • #4
gfortran has a -fno-underscoring compile switch you might explore.
 
  • #5
Success!

THe -fno-underscoring option caused issues with linkning to the FORTRAN library.

I was able to get it to compile and run with the following make rules:

Code:
lapacktest.o: lapacktest.c
    $(GPP) -static -c -o lapacktest.o lapacktest.cpp

lapacktest.exe: lapacktest.o
    $(GPP)  -static -o lapacktest.exe lapacktest.o ~/lapack-3.9.0/liblapack.a ~/lapack-3.9.0/librefblas.a ~/lapack-3.9.0/librefblas.a -lgfortran -lquadmath

This does suggest tat the linker isn't actually looking for libraries in directories where you tell it to look.
 
  • #6
Cool.

Another thing to look at is pkg-config. If the lapack library install by sygwin has a config then

pkg-config --cflags --libs lapack

will return the needed command line switches.

On Mac OS using brew I installed lapack using homebrew. Also installed is lapacke - C Standard Interface to LAPACK. pkg-config works but one needs to set PKG_CONFIG_PATH correctly. The Mac OS may have an indigenous lapack with which brew interferes.
 
Last edited:

1. How do I link LAPACK to g++ in cygwin64?

In order to link LAPACK to g++ in cygwin64, you will need to include the following flags in your g++ command: -llapack -lblas.

2. Do I need to install LAPACK separately in cygwin64?

Yes, LAPACK is not included in the default cygwin64 installation. You will need to install LAPACK separately using the cygwin installer.

3. How do I check if LAPACK is installed in cygwin64?

To check if LAPACK is installed in cygwin64, you can use the command "cygcheck -c lapack" in the cygwin terminal. If LAPACK is installed, it will show up in the list of installed packages.

4. Can I use other linear algebra libraries instead of LAPACK in cygwin64?

Yes, you can use other linear algebra libraries such as BLAS or Eigen in cygwin64. However, you will need to make sure that you include the necessary flags in your g++ command to link the library.

5. Are there any additional steps required to link LAPACK to g++ in cygwin64?

In some cases, you may need to specify the location of the LAPACK library using the -L flag in your g++ command. You may also need to specify the name of the LAPACK library using the -l flag. It is recommended to consult the documentation of your specific LAPACK implementation for more detailed instructions.

Similar threads

  • Programming and Computer Science
Replies
7
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
947
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top