5

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
Bruno Pereira
  • 73,643
kernel_panic
  • 11,732
  • 1
    Have you installed the LAPACK and LBLAS libraries? If so, are you using a Makefile? If so, would you post the Makefile or if not, post the command you are using to compile and link the program. Posting the errors would also be helpful. – user3121023 Jan 20 '14 at 11:46
  • @kernel_panic I assume you installed properly. now let us know how did you used the function and linked it? can you give the code complete code so we can see? You may use http://pastebin.com/ and share the url. – sourav c. Jan 21 '14 at 16:47
  • ubu@sanjay-pc:~/Desktop$ gcc code.c -o code /tmp/cclgw8k2.o: In function main': code.c:(.text+0xb7): undefined reference tozheev_' code.c:(.text+0x167): undefined reference to `zheev_' collect2: ld returned 1 exit status ubu@sanjay-pc:~/Desktop$ – kernel_panic Jan 23 '14 at 11:20

1 Answers1

12

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

sourav c.
  • 44,715
  • I could give you a complete c code using lapack function if you need. – sourav c. Jan 20 '14 at 21:17
  • I am getting jargonous output, where am i going wrong? PS. i have pasted the output as an edit. – kernel_panic Jan 21 '14 at 14:55
  • +1 Very good and complete answer. I might add that you can also call the BLAS FORTRAN functions directly similarly to how you are calling LAPACK functions so that you don't have to include any headers. – OSE Jan 21 '14 at 15:04
  • @OSE you are right. I just tried to show all the ways available. – sourav c. Jan 21 '14 at 16:38