gtk-fortran + Code::Blocks + Windows
codeblocks fortran gtk
- 1 Introduction
- 2 Installing Code::Blocks
- 3 Installing gfortran and GTK3
- 4 Running a Code::Blocks Fortran project without a GUI
- 5 Running a Code::Blocks Fortran project with a GUI (no gtk-fortran dll)
- 6 Running a Code::Blocks Fortran project with a GUI (using a gtk-fortran dll)
- 7 Conclusion
The GTK software suite  (also called GTK+  ) allows to create graphical user interfaces (GUI) in C/C++ language. Version 3 is the latest version. Since the Fortran language allows interoperability with the C language thanks to the iso_c_binding module, it has become easier to make communicate programs written in Fortran with those written in C/C++. This gave birth to the gtk-fortran library .
In order to realize GUIs in fortran, several elements are necessary:
- a fortran compiler
- the GTK3 package
- the gtk-fortran package
In this tutorial, the gfortran compiler will be used. Moreover, Code::Blocks IDE will be used to build Fortran project. Finally, this tutorial is directed toward Windows users.
2 Installing Code::Blocks
I advise you to install the version of Code::Blocks available at the following address: . Unzip the downloaded directory and copy it wherever you want (
C:\Program Files seems like a good idea). By double-clicking on the
codeblocks.exe executable contained in this directory the software should open. However, the IDE does not embed the gfortran compiler. Therefore, it is too early to write any Fortran code. In order to install gfortran, I recommend you to use MSYS2 (more details in the next section).
3 Installing gfortran and GTK3
3.1 Installing MSYS2
MSYS2 will perform the istallation of gfortran and GTK3 from the command interface. It can be downloaded at: . For a 32-bit system, choose the executable starting with msys-i686 while for a 64-bit system, choose the one starting with msys2-x86_64. I chose the latest because my personnal computer is a 64-bits system.
Install MSYS2 at the root of the hard disk (usually
C: disk will be considered as the installation directory later on.
C:\msys64\msys2.exe. A window should open:
First, check that the latest version of MSYS2 is installed by typing the following command in the window:
$ pacman -Syuu
You do not need to type the symbol
$, it is automatically put at the beginning of each line. Press
Enter to confirm the order. In my case the system is up to date and the following lines are displayed:
If some MSYS2 programs are not outdated, the list of programs to be updated appears. To update everything, write
O (the letter
o capitalized and not a zero). The update may take time but it is necessary. Once it is done, retype the
pacman -Syuu command to check that everything is up-to-date (the result must be similar to the one in the previous image).
3.2 Installing gfortran
To install gfortran, the following command must be typed :
$ pacman -S mingw-w64-x86_64-toolchain base-devel
Enter and then
O to proceed with the installation. From now on, the sequence
O (if asked) will be implicit for the sake of a shorter text. The installation will be long because a large number of tools must be installed.
3.3 Installing GTK3
To install GTK, the following command must be typed:
$ pacman -S mingw-w64-x86_64-gtk3
There are executable GTK programs available on the Internet but they are likely not up to date (for example ). Indeed, only versions recompiled by MSYS2 (i.e. by yourself when you execute the
pacman -S mingw-w64-x86_64-gtk3 command) guarantee you to have the latest version. It is at your own risk to use these outdated executables and then complain on Stack Overflow about a bug fixed for several years.
Now close the window MSYS2 in which you were entering the commands until now and then execute
mingw64.exe located in the directory
C:\msys64\. A new window should open, similar to the previous one (but only in appearance). Then type the following command line:
$ pkg-config --libs gtk+-3.0
This command will be used as a gfortran compilation option to use the GTK library we just installed. In other words, if it doesn’t work, it’s not worth going any further. Normally, you should get a list of libraries belonging to GTK as represented in the figure bellow.
For the moment,
gtk can only be called from MSYS2, which Windows cannot access to launch the programs we have just installed. Indeed, the command
pkg-config --libs gtk+-3.0 does not work in the Windows command prompt with the consequence that Code::Block will not be able to connect GTK to gfortran 😬.
To solve this problem it is necessary to add the
C:\msys64\mingw64\bin directory to the
PATH variable of the operating system. This directory contains various executables we would like Windows to use.
To do this, type
PATH in the Windows search tool. The proposal
Modify system environment variables should be made to you. Click on it. In the new window click on
In the lower window of the page that has just opened, double-click on the line called
In the new window, click on
New and copy the path
C:\msys64\mingw64\bin. Confirm the change made. Close your old command window if it is still open and open a new one.
In order to verify that
gfortran can be called, simply write
gfortran in the command window. If everything is working normally, you should receive a message from
gfortran informing you that it does not have a file to process. This means that Windows can now call
gfortran, which will allow us to compile Fortran code.
To ensure that
GTK3 could be used in the fortran program, type
pkg-config --libs gtk+-3.0 in the command prompt. You should then get the same list of libraries on the screen as when you entered this command in
This is what I get:
4 Running a Code::Blocks Fortran project without a GUI
Now that the various pieces of the puzzle are at our disposal, let’s start by telling CodeBlocks where to find
gfortran when a Fortran code needs to be compiled. To do this, you have to go to
Settings -> Compile.... Choose
GNU Fortran Compiler as compiler in the window that just opened.
Toolchain executablestab, enter the names of the executables as shown in the following drawing:
In order to check that the compiler is well configured, I invite you to create a new fortran project by selecting
File -> New -> Project... then
Fortran application. Enter the name of the project, its location and check on the next page that the compiler used is
GNU Fortran Compiler.
The compilation and execution (shortcut
F9) of the code should be done without difficulty and you should see a sumptuous
5 Running a Code::Blocks Fortran project with a GUI (no gtk-fortran dll)
Now that Code::Blocks works well with a simple fortran code, it is possible to make it more complex and integrate a GTK overlay. Rather than writing everything ourselves, it is better to start from a code that has already been written but that will not be compiled without problems (otherwise it would not be fun 😈). I advise you to download the
test_say_hello_1_zip directory at the following address (downloads are at the bottom of the page): .
Unzip the directory and open the file
test_say_hello_1.cbp with Code::Blocks. For your information, this directory already contains a number of source files from the gtk-fortran library necessary to make communicate the Fortran language with the previously installed GTK package.
This means that the only gtk-fortran source you will use in this exemple are the Fortran files in this project that will be compiled when you will compile the whole project. You will see in the next section how to “install” gtk-fortran on your local computer so that gtk-fortran compiled files will be located outside of the project scope into a so-called dll.
When compiling this code, you should get a large number of errors. This is what I get:
It seems that all the errors obtained are related to GTK. In fact, the compiler does not find some of the gtk-fortran functions and it is therefore necessary to modify the project’s compilation options. To modify only the compilation options of the current project and not all projects, go to
Project -> Build options.... In the left tree structure, select the parent
say_hello (as opposed to the child
say_hello). Changing the compilation options of the parent
say_hello will change the compilation options of all children (here only
say_hello child but by default when opening a new project with Code::Blocks you should have
Linker Settings tab, we notice that the author of the code has already filled in the compilation option
pkg-config --libs gtk+-2.0, meaning that he wants to use
GTK2 to create the graphical interface.
The symbols ` around
pkg-config --libs gtk+-2.0 are obtained with the combination
AltGr+7. These are therefore not quotation marks and are mandatory.
However, we have never installed GTK2 but only GTK3. Therefore, the GTK2 functions that the program calls do not exist on your computer.
pkg-config --libs gtk+-2.0 by
pkg-config --libs gtk+-3.0. We know that the compiler will be able to process it correctly because it was recognized in the Windows command prompt. The initial problem with the code was simply that it was developed with GTK2.
From then on, the program can be compiled and executed. The rendering, in accordance with what is expected, is as follows:
Victory ! 👍
6 Running a Code::Blocks Fortran project with a GUI (using a gtk-fortran dll)
In the previous example, the gtk-fortran files are included in the Code::Blocks project. This is not the best way to do it because the compilation time of these files can be excessively long. If I integrate all the gtk-fortran files into my project, the compilation time exceeds one hour. It’s too much. It is then recommended to use a dll containing all the gtk-fortran files compiled in the form of a library accessible from any Fortran program. The advantage is that the library does not need to be recompiled for each new project.
6.1 Download and compilation of gtk-fortran
First of all, it is necessary to download the source files of gtk-fortran. Indeed, there is no compiled version for Windows and it is up to us to do it (thanks to the author’s indications ).
First of all, the uncompiled gtk-fortran source code must be downloaded and extracted using MSYS2:
$ wget https://github.com/vmagnin/gtk-fortran/archive/gtk3.tar.gz $ tar -xvzf gtk3.tar.gz
The unzipped folder should be located in the directory
C:/msys64/home/Yourname/ and named
To make the
sudo command available type in mingw64.exe :
$ curl -s https://raw.githubusercontent.com/imachug/win-sudo/master/install.sh | sh
Then, type the four following lines:
$ mkdir build && cd build $ cmake -G "MSYS Makefiles" -D EXCLUDE_PLPLOT=Y .. $ make $ sudo make install
gtk-fortran-gtk3/build/ folder should now be filled with several different files. We are interested in files located in the
- the files
libgtk-3-fortran.dll. The first file will be specified in Code::Blocks as the entry to the gtk-fortran functions compiled in the second file (it will become clearer soon when exploring the linker settings in Code::Blocks).
*.modfiles in folder
modulewhich will be mandatory for the program compilation.
In summary, the MSYS2 program allows to perform most of the work of collecting the different subprograms necessary to execute a GUI written in fortran (gfortran, GTK3 and gtk-fortran). It is quite straight-forward to add the compilation options in Code::Blocks (once you know which they are) so that any Fortran code can use the gtk-fortran and GTK3 libraries.
 “The GTK Project.” https://www.gtk.org/.
 “What is the relation between GTK, GTK+ and GTK2?” Stack Overflow. https://stackoverflow.com/questions/5830731/what-is-the-relation-between-gtk-gtk-and-gtk2.
 “Project rename to "GTK".” https://mail.gnome.org/archives/gtk-devel-list/2019-February/msg00000.html.
 “Gtk-fortran.” Mar-2019.
 “Fortran Code::Blocks,” CBFortran. http://cbfortran.sourceforge.net/downloads/, Feb-2016.
 “Code::Blocks.” http://www.codeblocks.org/.
 “OPEN bug in gFortran 5.1.0.” https://sourceforge.net/p/mingw-w64/bugs/487/.
 “MSYS2 homepage.” http://www.msys2.org/.
 “Installation toolchain.” http://www.math.ucla.edu/~wotaoyin/windows_coding.html.
 “Binaires GTK+.” http://www.tarnyko.net/dl/gtk.htm.
 “Test_say_hello_1,” CBFortran. http://cbfortran.sourceforge.net/gtk-fortran/, Nov-2014.
 Ivanq, “Add ‘sudo‘ command to Git Bash. Contribute to imachug/win-sudo development by creating an account on GitHub.” Mar-2019.