I'm putting a general answer for linking BLAS not only this question, the answer for this question at the end below.
first you need to ensure you have BLAS installed using(+lapack)
$ sudo apt-get install libblas-dev liblapack-dev
then you can link using -lblas after your program files. or you can use a make file.
for example:
g++ test.o dmatrix_denseCM.o mmio.o -o output -lblas
from my side I prefer to use OpenBlas instead, you can use the following in a makefile.
- www.openblas.net, get the tar.gz
copy it in your directory
- extract it : tar -zxvf OpenBLAS-0.2.20.tar.gz
- compile it : cd OpenBLAS-0.2.20
make
When it is done you should have the file libopenblas.a, the openblas library
- BLASLIB = OpenBLAS/libopenblas.a -lpthread
then add it to the file linked together as: $(BLASLIB)
this directory OpenBLAS/libopenblas.a should be in the same working directory.
example code in .cc file:
extern "C"{
void dgemm_( const char &TRANSA, const char &TRANSB, const int &M, const int &N, const int & K, const double & ALPHA, const double *A, const int & LDA, const double *B, const int &LDB, const double &BETA, double *C, const int & LDC);
}
Moreover,When calling LAPACK or BLAS routines from C, be aware that because the Fortran language is case-insensitive, the routine names can be both upper-case or lower-case, with or without the trailing underscore. For example, the following names are equivalent:
LAPACK: dgetrf, DGETRF, dgetrf_, and DGETRF_
BLAS: dgemm, DGEMM, dgemm_, and DGEMM_ Intel® Math Kernel Library 11.3 Update 4 Developer Guide
you can add more information about the FORTRAN compiler you used to compile BLAS, for example:
Compile the Fortran program with the -U option, which tells the compiler to preserve existing uppercase/lowercase distinctions on function/subprogram names.
The Fortran compiler normally appends an underscore (_) to the names of subprograms appearing both at entry point definition and in calls. This convention differs from C procedures or external variables with the same user-assigned name. here
the Name mangling in C++ makes a problem in C. as C doesn't support overloading then we have to use extern "c"{}.