yolegu.xyz
Possibly interesting...

Fortran GUI with gtk-fortran (installation)

gtk-fortran is a Fortran code that makes the gtk libraries written in C accessible to Fortran programs. Using gtk-fortran, one can code both the back-end (the calculation) and the front-end (the GUI) of its application with only Fortran code.

However, the required setup to make gtk-fortran accessible to Windows users is quite lengthy. This post aims at providing a non-ambiguous way to install gtk-fortran and all its dependencies to a Windows user, assuming that no Fortran compiler or IDE is already setup.

All along this post, the following applications will be installed:

  • Visual Studio 2022
  • Intel Fortran compiler
  • gtk-fortran
  • Python 3
  • GTK 4

The Visual Studio solution described in this post, the GTK4 and the gtk-fortran compiled libraries can be downloaded here.

1 Visual Studio 2022 installation

Visual Studio 2022 is the selected IDE as it has a great integration of the Intel Fortran compiler and debugger. Although I used to work with CodeBlocks, the lack of a bulletproof debugger integration made me switch to Visual Studio. Moreover, the online community of Visual Studio seems more open to Windows users than the CodeBlocks community, making it easier to find answers to questions just by googling them. The only drawback of Visual Studio I met is the lack of code autocomplete although the use of Visual Studio Code (which is NOT Visual Studio) solves this problem when the right plugins are installed.

Visual Studio 2022 can be downloaded here.

Once downloaded, just launch the installer. During the installation you will have to tick the box “Desktop Development with C++”. Note that this does not install any Fortran compiler, it is just a preliminary step to install later the Intel fortran compiler.

2 Intel Fortran Compiler installation

The installer can be downloaded here.

My understanding is that the debugger of the Intel Fortran Compiler is also shipped with the installer. Because using gdb from gcc in CodeBlocks is a pain (e.g., the array values are not well/easily accessible) I decided to give a try to this one.

During the installation, make sure you accept the integration with Visual Studio. Normally, the Visual Studio versions installed on your computer should automatically be detected. Make sure that Fortran projects can now be created in Visual Studio. You should also try to compile a first Fortran project in Visual Studio to ensure that everything went as expected.

Visual Studio 2022 new project screen when Intel Fortran Compiler installation went as expected

Figure 2.1: Visual Studio 2022 new project screen when Intel Fortran Compiler installation went as expected

3 GTK4 installation

Now is the time to talk about GUI… but first Python if it is not already installed.

Regarding the Python download and install, just follow the instructions here. Select where you want Python to be installed and remember this location. Once the installation is done, add the Python installation path to your system’s PATH variables. In case of trouble with this last step just take a look here.

To perform GTK 4 installation we will use gvsbuild (repo is here. Click on the green “Code” button at the top of the page and “Download zip”. Unzip the downloaded folder. Then open a command prompt and navigate to the unzipped folder. You should have a “build.py” file in this folder. In the command prompt, then type python .\build.py build gtk4. The GTK code compilation should occur for the next ten minutes at least. Once the compilation is over you should find a new folder C:\gtk-build\gtk\x64\release\lib\ containing the precious libraries that will be called by gtk-fortran once installed! Some important DLL were also created at C:\gtk-build\gtk\x64\release\bin. But we will come back to this point later.

Ok so now we have the Fortran IDE and compiler but also GTK4. Great news, but no practical interest yet as both cannot communicate together. gtk-fortran will act as the ice-breaker.

4 gtk-fortran installation

gtk-fortran repo is here. Download the code by clicking on the green button at the top and unzip the downloaded folder. The important Fortran files, to be compiled in Visual Studio soon, will be in the src folder.

5 Visual Studio solution setup in order to use GTK 4 and gtk-fortran

The first step is to create a Fortran Static Library project in Visual Studio in order to compile the gtk-fortran source files. Once the project is created, add all the files from the gtk-fortran src folder in the Source Files folder from your newly created Fortran project. Then click Build > Build Solution to compile gtk-fortran. If everything went as expected, you should have no warning from Visual Studio.

Now, create a new Fortran Application in Visual Studio. Create this project in a new Solution. Once created, add the previously discussed Static library gtk-fortran project to the Solution. You should now have two projects in the same Solution (one Static Library with gtk-fortran files and one Application with nothing inside). In order to make them communicate, right-click on your Application project in Visual Studio and select Build dependencies>Project dependencies. Make sure that your Application depends on the gtk-fortran Static Library. As both projects are in the same Solution this should be your only option.

Now, one needs to add the GTK4 static library files generated with gvsbuild (remember?) in the Application. To do so, select all lib files from C:\gtk-build\gtk\x64\release\lib and add them to your Source Files folder in your Fortran Application. This in only step 1.

For step 2, you need to give to your Fortran Application access to the dll files from folder C:\gtk-build\gtk\x64\release\bin. To do so, in Visual Studio, right-click on your Application project and select Properties > Configuration Properties > Debugging > Environment. Then specify the dll path as follow PATH=C:\gtk-build\gtk\x64\release\bin;%PATH%. If you change the dll location just update the path.

6 Test with an example provided in the gtk-fortran repository

Some examples of Fortran applications using gtk-fortran are provided in the folder C:\msys64\home\USER NAME\gtk-fortran-gtk4\examples. Select one of these (bazaar.f90 might be a good start) and drop it in the Source Files folder of your Fortran Application project.

My Visual Studio looks like this after this step: gtk-fortran project is the Static Library project while gui-test is the Fortran application project. The GTK4 static library files are in the Source Files > gtk folder.

Visual Studio 2022 snapshot when gtk-fortran works as expected. The *Solution* structure is on the right side of the image.

Figure 6.1: Visual Studio 2022 snapshot when gtk-fortran works as expected. The Solution structure is on the right side of the image.

Then just click on Start in Visual Studio to see the magic happen.

Remark: if you get the message “Visual Studio cannot debug because a debug target has not been specified.”, it is very likely that the “Static library” is defined as the Startup Project. To select your Application as the Startup Project, right-click on your Application project and select “Set as Startup Project”.

This is what it looks like on my computer:

bazaar.90 compilation result

Figure 6.2: bazaar.90 compilation result

7 Resources

Most of the information provided in this post are from the following pages:

  • Visual Studio and Intel fortran installation: link
  • GTK4 and gtk-fortran installation: link
  • Reference to DLL in Visual Studio: link