Examples of linking common lisp to FORTRAN 77
This package provides a set of examples of passing variables between common lisp and FORTRAN 77 (f77) subroutines using the CFFI package. The package goal at this point is educational. It gives low level examples that should enable users for quick linking of lisp and f77. It does not provide a general facility for interfacing lisp to f77 that might be used for linking large amounts of f77 code to lisp.
Since argument passing to f77 subroutines is by reference, the examples demonstrate the use of pointers to pass variables by reference to f77 subroutines. The examples do not cover calls to f77 functions.
Other packages, such as
LLA also provide links to
large f77 libraries. However the interface to f77 is embedded in
these packages. It would be nice if the interface could be abstracted
out, so that it could be used by other authors.
Development and execution environment
The package was developed on Cygwin, using
gfortran. It has been tested on 64-bit Linux and
LispWorks. It is possible that the code might not work with other
compilers (such as
The package should be launched in a lisp session started from the package’s home directory.
About the f77 code in this package
The two f77 files are
- simple_example.f is an annotated introduction to linking f77 to cl
- subroutines.f contains subroutines that demonstrate passing of logical, integer, long, real, double, complex, and string variables, and also real and complex arrays.
The library is built with the
makefile by doing
in Cygwin or
On f77 example code
The annotated introductory example is in
the other examples use this example as template.
The bulk of the example code is in
subroutines.f. This file
contains subroutines that specialized on a variable type, or on
Most examples perform a simple copy of the input into the output variable. Many of them also echo the input and output variables into a file `xyz.echo’ where `xyz’ is one of logical, integer, … If the lisp code is not behaving as expected, these files might provide clues as to why.
Some of the examples do not copy, but perform a simple modification (such as negating a logical, or returning a complex conjugate).
About lisp code in this package
The package structure is describe in the
Loading and unloading of the library is done by functions in
library-ops.lisp. However, whenever I modified the library, I had
to restart the lisp session. Unloading the library and loading it
again, did not import library changes. I am not sure of the reason
The examples correctness can be verified by running unit tests:
(in-package #:cffi+f77) (run-tests)
- Examples of array pinning on sbcl and Linux
- Examples for linking to functions
- Automate handling of different name mangling conventions on different compilers (g77 vs gfortran vs ifort and others)
Mangling of subroutine names
(This was written by a non-expert, and thus subject to corrections)
When trying to refer to a f77 subroutine “foo”, you will most likely need to refer to either “foo_” or “foo__”. It really depends on the compiler, as discussed in this Wikipedia article.
The best way to obtain the mangled name is to use the
nm libXYZ.so | grep foo
nm command is available on Linux and Cygwin, and can be
*.o files. Neglect the leading
underscore, if any.