The Basic Linear Algebra Subprograms (BLAS) is a common library including high-quality routines for performing basic operations on vectors and matrices. The first FORTRAN version was released in 1979. As the routines are efficient and portable, they are often used in linear algebra software, like LAPACK. Most implementations are written in C, C++, or FORTRAN 77.

High-performance implementation of the BLAS API, for C and FORTRAN 77 (BSD licence).
The official reference implementation by Netlib (C and FORTRAN 77). Also known as refblas (public domain).
The Intel Math Kernel Library includes BLAS for Intel processors. Professional and community editions for C, C++, and Fortran are distributed (proprietary).

Please see Wikipedia for a more comprehensive overview of BLAS libraries.


The functionality of BLAS is divided into three sets of routines, called levels. Below, A, B, C, and T are matrices, x and y are vectors, and α and β are scalars.

Level 1: Scalar-Vector and Vector-Vector Operations

The level provides low-level operations, like dot product of vectors and vector additions, such as

yαx + y.

Level 2: Matrix-Vector Operations

The level contains basic matrix-vector operations, such as

yαAx + βy.

It also includes a solver for x in the triangular equation

Tx = y,

with triangular matrix T.

Level 3: Matrix-Matrix Operations

The level includes matrix-matrix operations, such as

CαAB + βC,

and routines for solving


with triangular matrix T.


On FreeBSD, the LAPACK implementation of BLAS is available as a port:

# pkg install math/blas

But BLAS can be build also from source. Download version 3.8.0 from Netlib, unpack the archive, and compile the Fortran code manually. In order to create a shared library, run:

$ gfortran10 -O2 -shared -fPIC -o *.f

Or, if the static library libblas.a is prefered, run:

$ gfortran10 -O2 -c *.f
$ ar cr libblas.a *.o


The following example program scales a vector by a constant using BLAS. The BLAS routine sscal() that scales a vector by a constant has to be imported first using the FORTRAN 77 external statement.

BLAS routines have to be imported individually using the external statement, if implicit none (type, external) is set. Otherwise, they are visible by default.

! example.f90
program main
    implicit none (type, external)
    external :: sscal

    integer, parameter :: N = 3

    real :: x(N) = [ 5., 6., 7. ]
    real :: a    = 5.

    print '(a, f0.1)',            'a = ', a
    print '(a, 3(f0.1, " "), a)', 'X = [ ', x, ']'

    call sscal(N, a, x, 1)

    print '(/, a)',               'X = a * X'
    print '(a, 3(f0.1, " "), a)', 'X = [ ', x, ']'
end program main

Compile and link the example with:

$ gfortran10 -L/usr/local/lib/ -o example example.f90 -lblas

If you prefer to link against the static library libblas.a, replace -lblas with the actual path. The program outputs:

$ ./example
a = 5.0
X = [ 5.0 6.0 7.0 ]

X = a * X
X = [ 25.0 30.0 35.0 ]

The compilation can be simplified by writing an appropriate Makefile:


FC      = gfortran10
FFLAGS  = -std=f2018
LDLIBS  = -lblas
TARGET  = example

.PHONY: all clean

	$(FC) $(FFLAGS) $(LDFLAGS) -o $(TARGET) example.f90 $(LDLIBS)

	rm $(TARGET)

We then just have to run make to build the executable.