vmagnin edited this page Feb 8, 2017 · 260 revisions

The gtk-fortran project aims to offer scientists programming in Fortran a cross-platform library to build Graphical User Interfaces (GUI). Gtk-fortran is a partial GTK+ / Fortran binding 100% written in Fortran, thanks to the ISO_C_BINDING module for interoperability between C and Fortran, which is a part of the Fortran 2003 standard.

GTK+ is a free software cross-platform graphical library developed since 1998, written in C, distributed under GNU GPL 3 license and available for Linux, Unix, Windows and Mac OS X. And gtk-fortran offers an interface to around 10000 GTK+ 2 and GTK+ 3 functions (GTK, GDK, GdkPixbuf, Cairo, Pango, ATK, GLib, GObject, GIO). Although gtk-fortran development is mainly focused on Linux, some users have also built it successfully under BSD, Mac OS X, Windows. You can join our small community to share knowledge, skills and code: just create a github account and you will be able to post messages. Every good will is welcome.

GTK+ being a huge library, only some hundreds of its functions have been tested. That is the reason why gtk-fortran must be considered as in the alpha development stage. See the list of Tested functions and the Status page for more informations.

Note also that gtk-fortran goes beyond programming GUI:

  • GTK+ includes the crossplatform GLib library which offers a lot of generic functions (regular expressions, random numbers, hash, strings, input/output...),
  • and gtk-fortran offers also an interface to PLplot.

julia_pixbuf.f90 example with gtk-fortran on Kubuntu 16.04 with GTK+ 3.18.9julia_pixbuf.f90 example with gtk-fortran on Kubuntu 16.04 with GTK+ 3.18.9

You will find more Screenshots on the wiki and even more in the screenshots directory (especially in the gtk3 branch). You can also watch this short video under MacOS posted by a gtk-fortran user. See also jtappin's RPN Scientific calculator, a good example of what can be done with gtk-fortran.


  • 01-09-2017: gtk-fortran 16.10 released.
  • 07-18-2016: gtk3 changes backported to the master (gtk2) branch. CMake files have been unified.
  • 07-04-2016: gtk3 branch has been updated.
  • 06-25-2016: The code of the heart of gtk-fortran, the cfwrapper.py script, has been refactored and improved in order to ease maintenance. The new version is in the gtk3 branch, with the latest versions of gtk-fortran files.
  • 06-15-2016: gtk-fortran 16.04 released.
  • 06-11-2016: the test.sh script has been updated and can be used as a primitive alternative to compile gtk-fortran and its examples.
  • 03-02-2016: a bug in GCC 5 prevents CMake to build two examples of gtk-fortran. The workaround (see Issue 89) is simply to type make -i instead of make, to ignore errors during the build.
  • 01-22-2013: Arch PKGBUILDs submitted to the AUR for Arch and Arch-derived distros. Packages are gtk-2-fortran-git and gtk-3-fortran-git.
  • 08-19-2012: Reorganize branches.
  • 08-10-2012: Plplot integration with gtk-fortran is now automatically built by the cmake build system if plplot libraries and module files are found on the system, a tutorial on using plplot and gtk-fortran is under construction. Plplot integration

Older news

gtk-fortran structure

gtk-fortran is a three-stage rocket:

  • The Python cfwrapper.py script parses the GTK+ .h files and generates several .f90 files containing interfaces to the functions of the GTK+ libraries. Normally, you do not need to use cfwrapper.py except when you need a new GTK+ function whose interface is not available in the gtk-fortran branches available on this site.
  • The gtk.f90 and the *-auto.f90 files constitute the source of the gtk-fortran library which you can use directly in your own programs.
  • But you can also decide to use the high-level-interface-for-Gtk-Fortran (hl-*.f90 files) which offers a simplified interface to some GTK+ functions.

Installation and building

First, you need a Fortran compiler with the ISO_C_BINDING module, i.e. compliant with the Fortran 2003 standard, for example gfortran (4.6 or higher). You also need the GTK+ development files.

If you want to build the whole project, you need CMake. If you want to build the PLplot examples, you need the PLplot development files.

To download all the project files, you can simply clone the repository using git (see git-basics). Alternatively you can click on Source (top left), then just below Switch branches to "master" if you use GTK+ 2 or "gtk3" for GTK+ 3, then click on the Downloads button on the right, then choose the .tar.gz or .zip archive depending on your system. The only files you absolutely need are gtk.f90 and the -auto.f90 files. The files gtk-sup.f90 and gtk-hl.f90 are needed for list_demo.f90 and the hl-.f90 files (see gtk_hl:-A-high-level-interface-for-Gtk-Fortran).

Git branches

You will find the latest versions of gtk-fortran in the master branch for GTK+ 2 and the gtk3 branch for GTK+ 3. You can also download a tagged release by choosing one in the "Switch branches/tags" selection window:

  • v16.04.gtk2.24.30: compiled with GTK+ 2.24.30, GLib 2.48.0 (Kubuntu 16.04),
  • v16.04.gtk3.18.9: compiled with GTK+ 3.18.9, GLib 2.48.0 (Kubuntu 16.04),
  • v13.10.gtk2.24.22: compiled with GTK+ 2.24.22, GLib 2.38.1 (October 2013),
  • v13.10.gtk3.10.1: compiled with GTK+ 3.10.1, GLib 2.38.1 (October 2013).

If you have an older version of GTK+ 2 or GTK+ 3, you should have no problem because deprecated functions will be removed only in the next major version. But with GLib, deprecated functions are sometimes removed from one minor version to another, which can prevent building gtk-fortran on your machine. In that case, you can test an older branch.


You absolutely need a Fortran 2003 compiler and the GTK+ development files (Ubuntu packages name):

sudo apt-get install gfortran libgtk2.0-dev libgtk-3-dev

Depending on your system, you may also need other packages, for example *-dev files for the libs GDK, GdkPixbuf, Cairo, Pango, ATK, GLib, GObject, GIO...

If you want to use PLplot, you need:

sudo apt-get install libplplot-dev plplot12-driver-cairo

The best way to fully build gtk-fortran and install its libraries on your system is to use CMake: follow the instructions of the "INSTALL" file.

sudo apt-get install cmake

But if you do not want to use CMake, you can alternatively use the test.sh bash script to compile all the gtk-fortran files and all examples, using gfortran by default. To use another compiler, use the following syntax: GFC='mycompiler' ./test.sh

And if you know which gtk-fortran files are needed, you can also simply compile and run each example as follow, from the src/ directory:

$ gfortran gtk.f90 ../examples/julia_pixbuf.f90 `pkg-config --cflags --libs gtk+-3.0`
$ ./a.out

Mac OS X

Search in the Issues tab to find message from Mac OS X users. The examples were tested under Mac OS X 10.6.7 (Darwin 10.7.0 on x86_64), with the quartz version of GTK2. gtk2 and gfortran 4.6 were built using macports.


See the MS Windows page. Use CMake via win32_install.bat or alternatively the (unmaintained) test.bat script to compile all the libraries and examples (more details in the installation section).

Using gtk-fortran

Learn GTK+

You can begin by studying the examples available in the examples/ directory, beginning by gtkhello2.f90. And the following book is a good way to learn GTK+:

More links to learn and use GTK+ are given at the end of this page.

Some external docs about gtk-fortran:

Projects using gtk-fortran

Advices to lower compilation time

Concerning the names of the modules you need, you can generally just look at the beginning of the names of the functions you use: gtk_window_new is in the gtk module, cairo_curve_to is in cairo... Note that all enumerators are currently in the gtk module. The list of all the functions available in gtk-fortran, with informations on the required files and the C prototype of the function, is in the src/gtk-fortran-index.csv file.

gtk-fortran modules offering interfaces to more than 9000 functions, you should add to your USE statements the ONLY option, e.g.:

use gtk, only: gtk_init, gtk_window_new, GTK_WINDOW_TOPLEVEL, gtk_window_set_title, &
      & gtk_container_set_border_width, g_signal_connect, gtk_hbox_new, gtk_container_add, &
      & gtk_button_new_with_label, gtk_box_pack_start, gtk_widget_show, gtk_main, FALSE, &
      & CNULL, TRUE

The compilation time will be ten times faster than with a simple "use gtk" statement ! To help you, the usemodules.pl perl script can scan your Fortran source files for GTK+ and related library functions and generate the required USE statements. For more informations type:

perl usemodules.pl --help

Note also that the compilation time with gfortran 4.6 under Windows is ten times longer than under Linux (why ?)

Known issues or limitations

Parsing more than 700 .h files is not an easy task. So gtk-fortran does not implement 100% of GTK+ functions and have some limitations:

  • gstdio.h and giochannel.h were excluded because some functions are declared twice (UNIX and non UNIX systems for example).
  • Around 90 functions with optional arguments (variadic fuctions) are not implemented. But some of these functions are also available with an array argument to solve binding problems (for example gtk_list_store_new / gtk_list_store_newv), but not all. See http://live.gnome.org/GObjectIntrospection/WritingBindingableAPIs
  • Very few GTK+ types are not yet implemented.
  • Macro functions (#define) are not implemented.
  • Structures (struct) are not implemented.
  • Some enums are still missing, like GdkPixdataType.
  • All enums are in the gtk module. It would perhaps be better either to dispatch them in the right modules or to create an enums module.
  • g_signal_connect: should have been be declared as a function returning a c_long (handler_id).
  • Fortran has no unsigned integers, so problems could occur if you pass great values between Fortran and GTK+. See the tests.f90 file.
  • Missing deallocate in gtk_init() ? (g95 message when the program is quited).

See also the Issues page.

History and Roadmap


This project is based on Tobias Burnus' post on comp.lang.fortran newsgroup (2nd May 2007 13:10) where he gave an example of a Fortran 2003 program creating a minimalist GUI (an empty GTK window) using ISO_C_BINDING. Vincent Magnin posted a message on the 27th December 2010 and got into contact with Jerry DeLisle on the 29th. They launched together the project in January 2011. Other contributors then joined: James Tappin, Jens Hunger...


  • Planning a 0.1 release and publish packets for some Linux distributions ?

How can I help ?

  • We need people compiling and running the examples under various OS (particularly Windows and Mac OS), architectures (32/64 bits...) and with various Fortran compilers. You can report bugs and problems in the Issues tab.
  • You can write new examples or improve existing examples and share them with the community. Note that C examples are available in the applications gtk-demo and gtk3-demo (gtk-3-examples and gtk2.0-examples Ubuntu packages).
  • Put a link on your site to https://github.com/jerryd/gtk-fortran/wiki, and include the "gui" and "graphical user interface" keywords. It will improve the pagerank of the project in search engines.

Useful links

Tools used in this project


Gtk-fortran is licensed under GNU General Public License version 3. The wiki documentation is under the GNU Free Documentation License 1.3. The gtk-fortran logo uses the GTK+ logo (by Andreas Nilsson) and so is also under GNU Free Documentation License Version 1.2 or later & Creative Commons Attribution-Share Alike 3.0 Unported license. The font is DejaVu Sans, a font under a free license.