gtk-fortran + Code::Blocks + Windows


codeblocks fortran gtk

1 Introduction

The GTK software suite [1] (also called GTK+ [2] [3]) 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 [4].

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: [5]. 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).

I advise against using the 17.12 release of Code::Blocks available here : [6]. Indeed, the integrated gfortran compiler from the gcc 5.1.0 suite contains an extremely annoying bug that prevents writing to a pre-existing data file [7].

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: [8]. 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.

MSYS2 download page.

Figure 3.1: MSYS2 download page.

Install MSYS2 at the root of the hard disk (usually C: or D:). The C: disk will be considered as the installation directory later on.

Run C:\msys64\msys2.exe. A window should open:

MSYS2 command window.

Figure 3.2: MSYS2 command window.

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:

Result of the `pacman -Syuu` command when everything is up to date.

Figure 3.3: Result of the pacman -Syuu command when everything is up to date.

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 [9]:

$ pacman -S mingw-w64-x86_64-toolchain base-devel

Press Enter and then O to proceed with the installation. From now on, the sequence Enter then 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 [10]). 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.

Expected result of the command `pkg-config --libs gtk+-3.0` in MSYS2.

Figure 3.4: Expected result of the command pkg-config --libs gtk+-3.0 in MSYS2.

For the moment, gfortran and 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 😬.

Result (not satisfactory) of the `pkg-config --libs gtk+-3.0` command in the Windows command prompt.

Figure 3.5: Result (not satisfactory) of the pkg-config --libs gtk+-3.0 command in the Windows command prompt.

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

Access to environment variables under Windows 10.

Figure 3.6: Access to environment variables under Windows 10.

In the lower window of the page that has just opened, double-click on the line called Path.

Added path to executables contained in MSYS2 for the command prompt to call them.

Figure 3.7: Added path to executables contained in MSYS2 for the command prompt to call them.

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

This is what I get:

Calls to gfortran and execution of the command `pkg-config --libs gtk+-3.0` after changing the Windows PATH variable.

Figure 3.8: Calls to gfortran and execution of the command pkg-config --libs gtk+-3.0 after changing the Windows PATH variable.

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.

Selection of the gfortran compiler in Code::Blocks.

Figure 4.1: Selection of the gfortran compiler in Code::Blocks.

In the Toolchain executables tab, enter the names of the executables as shown in the following drawing:
Choice of the different executables to call when compiling a fortran code in Code::Blocks. These executables are those previously downloaded with MSYS2.

Figure 4.2: Choice of the different executables to call when compiling a fortran code in Code::Blocks. These executables are those previously downloaded with MSYS2.

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.

Choice of the gfortran compiler for a new project Code::Blocks.

Figure 4.3: Choice of the gfortran compiler for a new project Code::Blocks.

The compilation and execution (shortcut F9) of the code should be done without difficulty and you should see a sumptuous Hello world!.

Result of the compilation and execution of the default project defined by Code::Blocks - here a Hello World!

Figure 4.4: Result of the compilation and execution of the default project defined by Code::Blocks - here a Hello World!

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): [11].

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:

Attempt to execute the code retrieved from the Internet.

Figure 5.1: Attempt to execute the code retrieved from the Internet.

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 Debug and Release childs).

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

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

Project compilation options set by default. We see that the version of GTK called is not the right one.

Figure 5.2: Project compilation options set by default. We see that the version of GTK called is not the right one.

From then on, the program can be compiled and executed. The rendering, in accordance with what is expected, is as follows:

Fortran program which this time is correctly compiled and executed.

Figure 5.3: Fortran program which this time is correctly compiled and executed.

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 [4]).

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 gtk-fortran-gtk3.

To make the sudo command available type in mingw64.exe [12]:

$ 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

Thegtk-fortran-gtk3/build/ folder should now be filled with several different files. We are interested in files located in the build/src folder:

  • the files libgtk-3-fortran.dll.a and 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).
  • the *.mod files in folder module which will be mandatory for the program compilation.

7 Conclusion

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.

Sources

[1] “The GTK Project.” https://www.gtk.org/.

[2] “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.

[3] “Project rename to "GTK".” https://mail.gnome.org/archives/gtk-devel-list/2019-February/msg00000.html.

[4] “Gtk-fortran.” Mar-2019.

[5] “Fortran Code::Blocks,” CBFortran. http://cbfortran.sourceforge.net/downloads/, Feb-2016.

[6] “Code::Blocks.” http://www.codeblocks.org/.

[7] “OPEN bug in gFortran 5.1.0.” https://sourceforge.net/p/mingw-w64/bugs/487/.

[8] “MSYS2 homepage.” http://www.msys2.org/.

[9] “Installation toolchain.” http://www.math.ucla.edu/~wotaoyin/windows_coding.html.

[10] “Binaires GTK+.” http://www.tarnyko.net/dl/gtk.htm.

[11] “Test_say_hello_1,” CBFortran. http://cbfortran.sourceforge.net/gtk-fortran/, Nov-2014.

[12] Ivanq, “Add ‘sudo‘ command to Git Bash. Contribute to imachug/win-sudo development by creating an account on GitHub.” Mar-2019.