vmagnin edited this page Nov 19, 2018 · 288 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-fortran is licensed under GNU GPL 3.

GTK+ is a free software cross-platform graphical library developed since 1998, written in C, distributed under GNU LGPL 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 (see the "How can I help ?" section at the end of the page).

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.


  • 05-01-2018: gtk-fortran 17.10 released.
  • 01-15-2018: cfwrapper.py has been updated and can now be used to build gtk-fortran under Ubuntu 17.10.
  • 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.

Older news

gtk-fortran structure

gtk-fortran is a three-stage rocket:

  • The cfwrapper.py script parses the GTK+ .h files and generates several .f90 files containing interfaces to the functions of the GTK+ libraries. Normally, you should not use cfwrapper.py except if you really 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 the most useful 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.

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 names):

sudo apt 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 install libplplot-dev plplot-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 install cmake

You can then build the project from the top-level directory:

$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

If the building of some examples causes an error, you can ignore them with the -i option: make -i

Testing gkt-fortran

You can then test gtk-fortran by running the examples in the build/examples directory. If perl is installed on your machine, you can use the test/run_all.pl script to run them all one after the other:

$ cd test
$ ./run_all.pl ../build/examples

If you do not want to use CMake, you can alternatively use the src/test.sh bash script to compile and run 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 glib-auto.f90 cairo-auto.f90 gdk-auto.f90 gdk-pixbuf-auto.f90 gtk.f90 ../examples/julia_pixbuf.f90 `pkg-config --cflags --libs gtk+-2.0`
$ ./a.out

(please replace 2.0 by 3.0 in the gtk3 branch).


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).

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.

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, although now quite old, 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:

Some 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 ?)

How can I help ?

  • You can report issues and seek support in the Issues tab.
  • 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 write new examples or improve existing examples and share them with the community (by using the Issues tab or by a Pull request). 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 to improve our pagerank in search engines.

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 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).

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...

Gtk-fortran was soon referenced in the following book: Markus, Arjen. Modern Fortran in Practice. 1st ed. Cambridge: Cambridge University Press, 2012. http://dx.doi.org/10.1017/CBO9781139084796.


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.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.