Ubuntu: How to to use gcc with LAPACK and external header file?



Question:

I am trying to run a C program that uses an external header file with LAPACK AND LBLAS libraries, every time when i try to run the program i get an error lapack not found, How do i properly use the libraries to run the program.

ubu@sanjay-pc:~$ ./code  *** glibc detected *** ./code: free(): invalid next size (normal): 0x08f64eb8 ***  ======= Backtrace: =========  /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb6cf4ee2]  /usr/lib/libblas.so.3gf(ATL_dtrsmKLUNN+0x603)[0xb6ad8973]  ======= Memory map: ========  08048000-0804a000 r-xp 00000000 08:0c 697362     /home/ubu/code  0804a000-0804b000 r--p 00001000 08:0c 697362     /home/ubu/code  0804b000-0804c000 rw-p 00002000 08:0c 697362     /home/ubu/code  08f30000-08f72000 rw-p 00000000 00:00 0          [heap]  b57cb000-b57cc000 ---p 00000000 00:00 0   b57cc000-b5fcc000 rw-p 00000000 00:00 0   b5fcc000-b5fcd000 ---p 00000000 00:00 0   b5fcd000-b67ce000 rw-p 00000000 00:00 0   b67ce000-b6840000 r-xp 00000000 08:0c 5026       /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0  b6840000-b6841000 r--p 00071000 08:0c 5026       /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0  b6841000-b6842000 rw-p 00072000 08:0c 5026       /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0  b6842000-b6843000 rw-p 00000000 00:00 0   b6843000-b685a000 r-xp 00000000 08:0c 689952     /lib/i386-linux-gnu/libpthread-2.15.so  b685a000-b685b000 r--p 00016000 08:0c 689952     /lib/i386-linux-gnu/libpthread-2.15.so  b685b000-b685c000 rw-p 00017000 08:0c 689952     /lib/i386-linux-gnu/libpthread-2.15.so  b685c000-b685e000 rw-p 00000000 00:00 0   b685e000-b687a000 r-xp 00000000 08:0c 656309     /lib/i386-linux-gnu/libgcc_s.so.1  b687a000-b687b000 r--p 0001b000 08:0c 656309     /lib/i386-linux-gnu/libgcc_s.so.1  b687b000-b687c000 rw-p 0001c000 08:0c 656309     /lib/i386-linux-gnu/libgcc_s.so.1  b687c000-b697a000 r-xp 00000000 08:0c 10434      /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0  b697a000-b697b000 r--p 000fe000 08:0c 10434      /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0  b697b000-b697c000 rw-p 000ff000 08:0c 10434      /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0  b697c000-b697d000 rw-p 00000000 00:00 0   b697d000-b6c4e000 r-xp 00000000 08:0c 41489      /usr/lib/atlas-base/atlas/libblas.so.3gf.0  b6c4e000-b6c52000 rw-p 002d1000 08:0c 41489      /usr/lib/atlas-base/atlas/libblas.so.3gf.0  b6c52000-b6c7c000 r-xp 00000000 08:0c 690058     /lib/i386-linux-gnu/libm-2.15.so  b6c7c000-b6c7d000 r--p 00029000 08:0c 690058     /lib/i386-linux-gnu/libm-2.15.so  b6c7d000-b6c7e000 rw-p 0002a000 08:0c 690058     /lib/i386-linux-gnu/libm-2.15.so  b6c7e000-b6c7f000 rw-p 00000000 00:00 0   b6c7f000-b6e23000 r-xp 00000000 08:0c 691765     /lib/i386-linux-gnu/libc-2.15.so  b6e23000-b6e25000 r--p 001a4000 08:0c 691765     /lib/i386-linux-gnu/libc-2.15.so  b6e25000-b6e26000 rw-p 001a6000 08:0c 691765     /lib/i386-linux-gnu/libc-2.15.so  b6e26000-b6e29000 rw-p 00000000 00:00 0   b6e29000-b75f0000 r-xp 00000000 08:0c 41490      /usr/lib/atlas-base/atlas/liblapack.so.3gf.0  b75f0000-b75f4000 rw-p 007c6000 08:0c 41490      /usr/lib/atlas-base/atlas/liblapack.so.3gf.0  b75f4000-b7701000 rw-p 00000000 00:00 0   b7719000-b771c000 rw-p 00000000 00:00 0   b771c000-b771d000 r-xp 00000000 00:00 0          [vdso]  b771d000-b773d000 r-xp 00000000 08:0c 689978     /lib/i386-linux-gnu/ld-2.15.so  b773d000-b773e000 r--p 0001f000 08:0c 689978     /lib/i386-linux-gnu/ld-2.15.so  b773e000-b773f000 rw-p 00020000 08:0c 689978     /lib/i386-linux-gnu/ld-2.15.so  bfc14000-bfc35000 rw-p 00000000 00:00 0          [stack]  Aborted  


Solution:1

You need to install the lapack and libblas.

sudo apt-get install build-essential  sudo apt-get install liblapack*  sudo apt-get install libblas*  

The real trick lies in calling the library functions and link them.

LAPACK

Header

No special header is required, Only usual headers will be there.

#include <stdio.h>  #include <math.h>  

Function prototype and function call

All the functions should append underscore (_) while declaring prototype and calling. It is important. Say I am going to call a function zheev from lapack. Its prototype should be like,

extern void zheev_( char* jobz, char* uplo, int* n, dcomplex* a, int* lda,              double* w, dcomplex* work, int* lwork, double* rwork, int* info );  

Link and run a code in terminal

$ gcc code.c -o code -llapack -lblas  $ ./code  

You need to use -lm flag to use math library. Also you could use -Wall for all warnings.

BLAS

Using blas is quite simple.

Header

Use special header along with others,

#include <stdio.h>  #include <cblas.h>  

Function Call

Just call the function,

cblas_dgemv(CblasRowMajor, CblasNoTrans, 3, 3, 1.0, m, 3, x, 1, 0.0, y, 1);  

Link and run a code in terminal

$ gcc code_blas.c -o code_blas -lblas -lm  $ ./code_blas  

You can have some good examples here


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »