Before jumping right on into the actual somewhat esoteric incantations
necessary to actually interface C and FORTRAN 77, we need to go over some of the
preliminaries. Firstly, let us go over what this guide is, and what it is not.
It is not an entirely generic guide applicable on all platforms and
tool chains. It is however broadly applicable, and the general ideas
and concepts can be applied to architectures and tool chains not
covered—just adjust any platform-specific pieces of code (for instance,
when using gfortran the unnamed COMMON block is referred by the
symbol __BLNK__, whereas when using g77 it is referred to by the
symbol _BLNK__). It is not exhaustive—there may be
omissions in what this guide covers, which the author hopes to rectify as he
continues working on this guide. It is a “living”
document, in that the guide is always being worked on.
Sections on this page:
The environment where the examples were tested include several Linux systems (CentOS 6.8, Debian 9.6, Debian 11.11, Debian 13.5) using GCC (4.4.7, 6.3.0, 10.2.1, 14.2.0) and GFortran (4.4.7, 6.3.0, 10.2.1, 14.2.0), both natively on x86-64 and under the Windows Subsystem for Linux 2 running on Windows 10 on x86-64. As well as the examples were run on OpenBSD 6.7 on native x86-64 using Clang 8.0.1 and Flang 8.0.1.
OpenBSD 5.8 (using gcc 3.3.6 and G77 3.3.6) was also tested on an emulated (using OpenSIMH) VAXserver 3900.
Debian 9.3 (using gcc 6.3.0-18 and gfortran 6.3.0-18) was also tested on an emulated (using SDL Hercules 4.x “Hyperion”) z/Architecture mainframe system.
The techniques shown should apply equally well to all platforms on which the tested C and FORTRAN compilers are available. Whether these techniques will work with other pairs of compilers is unknown and untested. Furthermore, whether these techniques will continue to work with future versions of the tested tooling cannot be guaranteed.
To make building the examples easier, a full Makefile is provided which will compile and link the examples (on Linux and OpenBSD 6.7). To make life easier, GNU Make (versions 3.81, 4.1, 4.3, 4.4.1) was used even on OpenBSD (as such on OpenBSD instead of invoking the Makefile with make, instead invoke it with gmake).
The Makefile used to compile and link the programs is based on the following skeleton/example:
# Compilers, Librarian, & Flags
# Tailor this to your own environment; do note of that the defaults are set for
# gcc and gfortran, on OpenBSD 6.7 they should be replaced by clang and flang.
# For gcc and g77 on OpenBSD 5.8 not only will the compilers need to be adjust,
# but so will the flags--g77 does not support the -fdec or -cpp flags.
AR := ar
CC := gcc
FC := gfortran
# Flags and Libraries
#
ARFLAGS := rcs
CFLAGS := -std=c99 -O3 -c
FFLAGS := -std=legacy -fdec -funsigned -O3 -cpp -c
LDFLAGS := -O3
LDLIBS := -lm
# Shell Commands
RM := rm -f
CAT := cat
# Netlib Library Sources
BLAS_SRCS := $(wildcard netlib/blas/src/*.f)
SLATEC_SRCS := $(wildcard netlib/slatec/src/*.f)
LINPACK_SRCS := $(wildcard netlib/linpack/src/*.f)
EISPACK_SRCS := $(wildcard netlib/eispack/src/*.f)
QUADPACK_SRCS := $(wildcard netlib/quadpack/src/*.f)
.PHONY: all clean test
all: example
clean:
$(RM) *.a
$(RM) *.o
$(RM) example
$(RM) blas.f
$(RM) slatec.f
$(RM) linpack.f
$(RM) eispack.f
$(RM) quadpack.f
# The "test" target runs valgrind's leak check on the executables
test: example
valgrind -s --leak-check=full --show-leak-kinds=all \
--track-origins=yes ./example
# Link target "example"
example: example_f.o example_c.o | libblas.a libeispack.a liblinpack.a \
libslatec.a libsqlite3.a libquadpack.a
$(FC) $(LDFLAGS) $^ $(LDLIBS) -L. -lquadpack --leispack -llinpack \
-lslatec -lblas -o $@
# Build the BLAS static library
blas.f: $(BLAS_SRCS)
$(CAT) $^ >> $@
libblas.o: blas.f
$(FC) $(FFLAGS) -fPIC $(CPPFLAGS) $^ -o $@
libblas.a: libblas.o
$(AR) $(ARFLAGS) $@ $^
# Build the SLATEC static library
slatec.f: $(SLATEC_SRCS)
$(CAT) $^ >> $@
libslatec.o: slatec.f
$(FC) $(FFLAGS) -fPIC $(CPPFLAGS) $^ -o $@
libslatec.a: libslatec.o
$(AR) $(ARFLAGS) $@ $^
# Build the LINPACK static library
linpack.f: $(LINPACK_SRCS)
$(CAT) $^ >> $@
liblinpack.o: linpack.f
$(FC) $(FFLAGS) -fPIC $(CPPFLAGS) $^ -o $@
liblinpack.a: liblinpack.o
$(AR) $(ARFLAGS) $@ $^
# Build the EISPACK static library
eispack.f: $(EISPACK_SRCS)
$(CAT) $^ >> $@
libeispack.o: eispack.f
$(FC) $(FFLAGS) -fPIC $(CPPFLAGS) $^ -o $@
libeispack.a: libeispack.o
$(AR) $(ARFLAGS) $@ $^
# Build the QUADPACK static library
quadpack.f: $(QUADPACK_SRCS)
$(CAT) $^ >> $@
libquadpack.o: quadpack.f
$(FC) $(FFLAGS) -fPIC $(CPPFLAGS) $^ -o $@
libquadpack.a: libquadpack.o
$(AR) $(ARFLAGS) $@ $^
# Build the SQLite 3 static library
libsqlite3.o: sqlite3.c
$(CC) $(CFLAGS) -fPIC -DSQLITE_ALLOW_COVERING_INDEX_SCAN=1 \
-DSQLITE_DEFAULT_AUTOMATIC_INDEX=1 -DSQLITE_DEFAULT_AUTOVACUUM=2 \
-DSQLITE_DEFAULT_FOREIGN_KEYS=1 -DSQLITE_DEFAULT_MEMSTATUS=1 \
-DSQLITE_DEFAULT_SYNCHRONOUS=3 -DSQLITE_DEFAULT_WORKER_THREADS=5 \
-DSQLITE_DIRECT_OVERFLOW_READ -DSQLITE_DQS=0 -DSQLITE_ENABLE_API_ARMOR \
-DSQLITE_ENABLE_BYTECODE_VTAB -DSQLITE_ENABLE_CARRAY \
-DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_DBPAGE_VTAB \
-DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
-DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS3_TOKENIZER \
-DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_GEOPOLY \
-DSQLITE_ENABLE_HIDDEN_COLUMNS -DSQLITE_ENABLE_MATH_FUNCTIONS \
-DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_NORMALIZE \
-DSQLITE_ENABLE_NULL_TRIM -DSQLITE_ENABLE_OFFSET_SQL_FUNC \
-DSQLITE_ENABLE_ORDERED_SET_AGGREGATES -DSQLITE_ENABLE_PERCENTILE \
-DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_RTREE \
-DSQLITE_ENABLE_SNAPSHOT -DSQLITE_ENABLE_SORTER_REFERENCES \
-DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_STMTVTAB \
-DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION -DSQLITE_ENABLE_UNLOCK_NOTIFY \
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_MAX_WORKER_THREADS=50 \
-DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_LOAD_EXTENSION \
-DSQLITE_SECURE_DELETE -DSQLITE_SOUNDEX -DSQLITE_STRICT_SUBTYPE=1 \
-DSQLITE_TEMP_STORE=2 -DSQLITE_THREADSAFE=2 $^ -o $@
libsqlite3.a: libsqlite3.o
$(AR) $(ARFLAGS) $@ $^
# Compile the C and FORTRAN 77 objects
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) $^ -o $@
%.o: %.f
$(FC) $(FFLAGS) $(CPPFLAGS) $^ -o $@
As this is a “skeletonized” version of the
Makefile, included in the package of examples, only the fictitious
example target is included.
One should note in the definition of FFLAGS, that the
-cpp flag has been set, which calls the C preprocessor on the
FORTRAN sourcess being compiled. This allows for conditional compilation by
using the standard C preprocessor # directives (such as
#ifdef. If the FORTRAN sources are saved with the extension of
.F (i.e. a capital ‘F’), then the C preprocessor will
be invoked automatically.
To obviate the tedium of copy-pasting the code examples from this guide, the examples have been packaged in a convenient archive, along with a Makefile to build the code.
Fave icon Database CMYK © 2008 – Barry Mieny
Syntax highlighting using highlight.js by © 2006 – Ivan Sagalaev