Using Lapack Routines in C Programs

The problem of using external libraries can be divided into two parts, how to call the routines in your program and how to compile this program.

LAPACK routines are written in Fortran 77 and although it is possible to use Fortran routines (at least on the Unix system, I have no idea what about Mac and PC) it takes a little bit more work.

One difference between C and Fortran lies in the way matrices are stored in memory, so that we have to transpose our matrices before we can pass them to a Fortran routine.
Another difference is the way arguments are passed to a subroutine in C and Fortran. In Fortran the argument is passed to the routine and the routine can read and change this value. This mechanism is called call by reference. In C a copy of the value of the argument is passed to the routine and not the argument itself, the subroutine can't change the value of the argument. This mechanism is called call by value. To use call by reference in C, we have to pass pointers to variables instead of the variables themself to the routine.

When you compile a C program the compiler searches for the definition of the functions in your program in a set of predefined libraries. If you want to use additional libraries, like the LAPACK library, you have to tell the compiler explicitly which library to use and sometimes where to find this library. The exact procedure of compiling your program may depend on your system and how LAPACK was installed on it.

Simple C programs using LAPACK

Back to matrix programming pitfalls