beginner - linking error with lapacke in c++

Post here if you have a question about linking your program with LAPACK or ScaLAPACK library

beginner - linking error with lapacke in c++

Postby huyaoyu » Mon May 07, 2012 3:29 am

Hi everyone,

I am new to lapack and I want to use the standard c interface in my c++ code. But I run into the following problem while I was trying to link the program to lapacke.a library.

I complied the 3.4.1 version of lapack with the default make.inc file. And then I compiled the lapacke library. The tests for lapacke worked very well.

I copied the code from lapack-3.4.1/lapacke/example/example_DGESV_rowmajor.c directly into a cpp file and began to compile.

I use eclipse and it gives me the following information:

********* information begins **********
Invoking: GCC C++ Linker
g++ -o "testfortranc" ./src/main.o -llapack -lrefblas -ltmglib -llapacke -L/home/huyaoyu/Libraries/lapack-3.4.1 -lm
/home/huyaoyu/Libraries/lapack-3.4.1/liblapacke.a(lapacke_dgesv_work.o): In function `LAPACKE_dgesv_work':
lapacke_dgesv_work.c:(.text+0x175): undefined reference to `dgesv_'
lapacke_dgesv_work.c:(.text+0x23c): undefined reference to `dgesv_'
collect2: ld returned 1 exit status
make: *** [testfortranc] Error 1
********* information ends *******

I noticed that there were likely problems that posted by other users. But I sill could not find a way to deal with it.

The example program uses gfortran as linker but I want to use g++ instead. Is there a way to use lapacke directly in c++ code with g++ as linker?

Thanks in advance! :D
huyaoyu
 
Posts: 2
Joined: Mon May 07, 2012 2:56 am

Re: beginner - linking error with lapacke in c++

Postby admin » Tue May 15, 2012 2:16 pm

Your linking order is wrong : should be lapacke, then lapack, then blas, then gfortran library (if you use g++ to link)

I took the same test and just put it with a .cpp extension and it works with the optimized LAPACK/BLAS LIBRARY from Apple
Code: Select all
g++ -c  prog.cpp -I~/opt/mylib/ -DHAVE_LAPACK_CONFIG_H -DLAPACK_COMPLEX_CPP
g++  prog.o -o prog.exe  -L/Users/julie/lapack-3.4.1 -llapacke -framework veclib -lgfortran

but linking with the reference LAPACK, I got some undefined references, to solve them I need to use gfortran as the loader. (see viewtopic.php?f=2&t=570)
Code: Select all
gfortran  prog.o -o prog.exe  -L/Users/julie/lapack-3.4.1 -llapacke -llapack -lrefblas -lgfortran


Hope it helps
admin
Site Admin
 
Posts: 486
Joined: Wed Dec 08, 2004 7:07 pm

Re: beginner - linking error with lapacke in c++

Postby huyaoyu » Tue Jun 05, 2012 5:07 am

Thank you admin!

The method you provide does work! The example code now can be compiled and linked with g++. I certainly forgot to check the order of libraries before linking.

One thing to mention, I drop the "-framework veclib" option for g++ while linking, becaus the linker says it cannot find "veclib". And the final linking command looks like this:

g++ -o "testfortranc" ./src/main.o -L/home/huyaoyu/Libraries/lapack-3.4.1 -llapacke -llapack -lrefblas -ltmglib -lgfortran.


admin wrote:Your linking order is wrong : should be lapacke, then lapack, then blas, then gfortran library (if you use g++ to link)

I took the same test and just put it with a .cpp extension and it works with the optimized LAPACK/BLAS LIBRARY from Apple
Code: Select all
g++ -c  prog.cpp -I~/opt/mylib/ -DHAVE_LAPACK_CONFIG_H -DLAPACK_COMPLEX_CPP
g++  prog.o -o prog.exe  -L/Users/julie/lapack-3.4.1 -llapacke -framework veclib -lgfortran

but linking with the reference LAPACK, I got some undefined references, to solve them I need to use gfortran as the loader. (see viewtopic.php?f=2&t=570)
Code: Select all
gfortran  prog.o -o prog.exe  -L/Users/julie/lapack-3.4.1 -llapacke -llapack -lrefblas -lgfortran


Hope it helps
huyaoyu
 
Posts: 2
Joined: Mon May 07, 2012 2:56 am


Return to Linking Problem

Who is online

Users browsing this forum: No registered users and 1 guest

cron