Linking to LAPACK from g++ in cygwin64

  • Context:
  • Thread starter Thread starter pasmith
  • Start date Start date
  • Tags Tags
    Lapack
Click For Summary

Discussion Overview

The discussion centers around linking the LAPACK library using g++ in a Cygwin64 environment, specifically addressing an 'undefined reference to 'zcgesv_'' error encountered during the linking process. Participants explore various aspects of linking, compilation options, and library configurations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant encounters an 'undefined reference to 'zcgesv_'' error when trying to link LAPACK with g++ and seeks assistance.
  • Another participant suggests checking the symbol table using the 'nm' command to verify the actual function name in the library.
  • A participant reports that removing the underscore from the function name results in a linker error indicating that 'zcgesv' is not defined.
  • One participant mentions compiling their own version of LAPACK after locating the source code, but still faces the same linking error.
  • Another participant proposes using the '-fno-underscoring' compile switch with gfortran as a potential solution.
  • A participant successfully resolves the issue by using the '-fno-underscoring' option, but notes it caused problems when linking to the FORTRAN library.
  • One participant suggests using 'pkg-config' to retrieve the necessary command line switches for LAPACK, mentioning its utility in other environments like Mac OS.

Areas of Agreement / Disagreement

Participants express differing views on the correct approach to resolve the linking issue, with some proposing specific compile options while others suggest verifying library configurations. The discussion remains unresolved regarding the best practices for linking LAPACK in Cygwin64.

Contextual Notes

Participants note limitations related to the recognition of file formats by the 'nm' command and the potential need for correct library paths in the linking process. There are also unresolved issues regarding the interaction between different compilation options and library dependencies.

pasmith
Science Advisor
Homework Helper
Messages
3,348
Reaction score
1,891
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
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   Reactions: pasmith
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:
gfortran has a -fno-underscoring compile switch you might explore.
 
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.
 
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:

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
7K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
5K