Fork me on GitHub
Credit and license

Mixed Martial Arts: Interfacing Fortran, C, C++, and Python

Learning goals

The programming languages Fortran, C, C++, and Python each have their strengths and weaknesses and their own fan base. This workshop is for people who would like to be able to combine these languages within one code project:

  • When writing a high-level abstraction layer or interface to a “bare metal” legacy software written for instance in Fortran or C.
  • When writing an efficient back-end to a code mainly written in a high-level language such as Python.
  • When combining modules written in different programming languages.
  • When writing a Python interface to a software in C or C++ or Fortran to leverage the wealth of libraries available in Python.
  • For testing and prototyping compiled code with Python.

The goal of this lesson is not to become expert in pybind11 or Python CFFI but to get you on a good track with simple but working examples which can be adapted to your domain.


To appreciate the material it helps to have some previous exposure to Python and a compiled language (C, C++, or Fortran).

Required software

This lesson is tested on Linux and Mac. It should be possible to get it to run on Windows but tweaks are then necessary.

  • C++ compiler which is C++11 compliant (e.g. g++)
  • Fortran compiler (e.g. gfortran)
  • CMake
  • Make
  • Git
  • pytest and Python CFFI (we recommend to install these using Virtualenv or Anacoda or Miniconda)
  • python-dev (Debian-like systems) or python-devel/python3-devel (RedHat-like systems)


13:00 Motivation Why is one programming language often not enough?
13:10 Overview over different approaches Which tools are out there?
13:15 Hands-on example using pybind11 What tool do you recommend to interface Python and C++?
13:40 Fortran, C and C++ talking to each other How can we couple Fortran with C/C++?
What is name mangling?
How can we avoid worrying about it?
How can we safely pass data types across languages?
14:00 Using the C Foreign Function Interface for Python Is there a non-intrusive way to couple C/C++/Fortran and Python?
Can I test my Fortran code with Python?
14:35 Finish

Bonus material

14:35 Memory allocation for arrays that pass the API Where and how should we allocate memory?
14:45 API design How can we create composable code units?
How can we improve the API to make it easier to understand and use?
15:05 Roadmap for migrating and modularizing legacy code Big untested legacy monolith code in front of you - what now?
15:15 Finish