Lapack undefined reference

Go To StackoverFlow.com

2

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!

2012-04-04 20:58
by Conan


1

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

2012-04-06 10:16
by Attila
Thank you very much for your reply, Attila. I did a lot of google search when I tried to solve the problem. Everybody seems to use "-L/usr/local/lib" instead of "-I/usr/local/include/lapackpp", but this way seems not work on my computer. I am very curious about why. And I did not see many of them include explicitly "-lg2c" or "-lgfortran" in their linking command, but it seems it works fine on their computers. I am wondering why - Conan 2012-04-06 13:06
They might have their LD_LIBRARY_PATH environment variable set to include some common libs for convenience. Note that -L (also, LD_LIBRARY_PATH, see above) specifes where the linker should look for the libs specified with the -l (small L) options, whereas -I (capital i) specifies where the compiler should look for the headers mentioned in #include directive - Attila 2012-04-06 14:13
I got it. Thanks a lot - Conan 2012-04-06 14:48


0

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?

2012-04-06 03:32
by Conan
Ads