MathGL + GTK + Fortran

MathGL library can be used for visualisation of calculation results directly in GTK window (how to setup GTK on Windows read Gtk-Fortran). Read about this library on MathGL home page. Developer of MathGL maintains bindings for several languages and Fortran is in the list. I prepared 3 examples which, I hope, may be useful for other Fortran developers too.

First example is the simplest. It just shows how to draw a curve and save it directly to a file using MathGL. Second example demonstrates how to use GTK GUI library (gtk-fortran binding) together with MathGL. A curve is drawn directly in GTK window and lets user to save the image to a file (jpg, png, svg, eps). The third example is more complex. It shows how to add a mouse interaction with MathGL drawing using GTK events. MathGL + GTK + Fortran MathGL + GTK + Fortran screenshot

The source code includes Code::Blocks project files and makefiles (makefiles are specific to Windows). The examples were tested:

  • On Windows 7 with GCC Gfortran 4.7 (32-bit), GTK+3.6.4 (32-bit) and MathGL compiled from sources using the same GCC.
  • On Linux (Ubuntu 14.04)

You can use Code::Blocks IDE with FortranProject plugin for compiling and changing the code. First time you open \*.cbp project file inside C::B, you will be asked to define a global variable "mathgl". Set this variable to the directory with "libmgl.dll" (for Windows) or (for Linux) inside. You should compile MathGL library (“libmgl” file) by yourself or download pre-compiled binary.

The distributed makefiles can be used for compiling the examples too. However you should set MATHGL_DIR variable inside Makefile to the directory with "libmgl.dll" first. The makefiles will work on Windows only.

Short step-by-step how to start

  1. Windows: download GTK+3 (32-bit) all-in-one bundle, extract archive, set PATH environment variable to <gtk+3>\bin directory (you need log-out - log-in to activate new PATH). Linux: I guess, you have GTK+3 already. If not, install it.
  2. Download and compile MathGL. It uses ‘cmake’. Windows users may try to use precompiled binaries (see in Download below).
  3. Download the examples. Extract the archive. Open \*.cbp file in Code::Blocks and set ‘mathgl’ global variable to the directory with libmgl.dll (
  4. Compile the example project.


mathgl_fortran.7z - an archive with the examples.

mathgl_lib.7z - precompiled MathGL library for Windows using GCC 4.7 (32-bit). The archive includes the required libpng and jpeg libraries too.