Conan
Conan

Reputation: 95

Lapack undefined reference

I am new to g++ and lapack, and attempting to use them. I encountered a problem when I tried to compile the following naive code

#include <lapackpp.h>
int main()
{
    LaGenMatDouble A;
    return 0;
}

If I run the command

$g++ -L/usr/local/lib -llapackpp test2.cpp 

where test2.cpp is the name of the cpp file, the terminal would give an error:

test2.cpp:1:22: fatal error: lapackpp.h: No such file or directory

But if I run the command:

$g++ -I/usr/local/include/lapackpp -L/usr/local/lib -llapackpp test2.cpp

the terminal would give an error:

/tmp/ccUi11DG.o: In function `main':  
test2.cpp:(.text+0x12): undefined reference to `LaGenMatDouble::LaGenMatDouble()'  
test2.cpp:(.text+0x23): undefined reference to `LaGenMatDouble::~LaGenMatDouble()'  
collect2: ld returned 1 exit status

BTW, if I run the command

$pkg-config lapackpp --libs

the result is

-L/usr/local/lib -llapackpp 

Could you please help me solve this? Thanks in advance!

Upvotes: 2

Views: 3845

Answers (2)

Attila
Attila

Reputation: 28802

Lapack requires fortran libraries, so that's where the -lgfortran comes from. Moreover, it appears the exact way to provide that library for the compiler depends on the Linux distriburion. From the documentation:

Requirements

This package requires the packages "blas", "lapack" (without the "++"), and a Fortran compiler. On most Linuxes these are available as pre-compiled binaries under the name "blas" and "lapack". For SuSE 10.x, the Fortran compiler is available as package "gfortran". For SuSE 9.x, the Fortran compiler is available as package "gcc-g77".

Not sure why pkg-config lapackpp --libs does not list -lgfortran

The -I/usr/local/include/lapackpp specifes the lapackpp-related header files. Without it the compiler cannot find lapackpp.h when you try to include it (#include <lapackpp.h>) -- see the compiler error in your question

Upvotes: 1

Conan
Conan

Reputation: 95

I finally solved the problem but would still wonder why it has to be so. The only command that can link cpp file to lapackpp library is:

g++ foo.cpp -o foo -lgfortran -llapackpp -I/usr/local/include/lapackpp

It would not work without -lgfortran, or with -I/usr/local/include/lapackpp replaced by -L/usr/local/lib.

Does anyone have an answer?

Upvotes: 0

Related Questions