Skip to content

Tutorial 5

Vincent Magnin edited this page May 8, 2024 · 13 revisions

Using gtk-fortran as a fpm dependency

In this tutorial, you will transform the most simple gtk-fortran example (it just opens an empty GTK window) into a Fortran Package Manager fpm project. The source of the resulting fpm project is available in the gtkzero_fpm repository.

Prerequisites

I suppose you have already installed:

  • gtk-fortran >= 4.2 (or >=3.24.41).
  • The Fortran Package Manager fpm.
  • GTK 4 (or GTK 3) and its development files.
  • The git version control system.

Creating the fpm project

You will first create a "hello world" fpm project named gtkzero_fpm:

$ fpm new gtkzero_fpm
$ cd gtkzero_fpm

The tree of the project created by fpm is:

├── app
│   └── main.f90
├── build
├── fpm.toml
├── README.md
├── src
│   └── handlers.f90
└── test
    └── check.f90

Copying the Fortran code

Now open the examples/gtkzero_gapp.f90 gtk-fortran example in your text editor. That file just contains the main Fortran program and a module named handlers. Copy the main program into the app/main.f90 file and the module into src/handlers.f90 (although it is a good practice to use the same name for the file and the module, don't care about that now).

Configuring the fpm project

The fpm.toml manifest must contain a dependencies section with the needed branch of gtk-fortran. Open the manifest in your editor and add those two lines:

[dependencies]
gtk-fortran = { git = "https://github.com/vmagnin/gtk-fortran.git", branch = "gtk4" }

(or branch = "gtk3")

Building and running the project

Now you can build and run the project very simply thanks to fpm:

$ fpm run

After having automatically cloned gtk-fortran in the build/dependencies/ directory of the project, fpm will build everything and run the executable. You should see on screen an empty GTK window with an "hello world" title:

The gtkzero_fpm gtk-fortran example

That's all folks! You have successfully created your first gtk-fortran fpm project.


Let's just finish with two useful remarks:

A local fpm dependency

If you have several projects using gtk-fortran, it would be a better solution to clone the gtk-fortran repository alongside your projects and replace in their fpm.toml manifests the git dependency by the local path to gtk-fortran:

[dependencies]
gtk-fortran = { path = "../gtk-fortran" }

How to use PLplot?

If your fpm project also uses the PLplot features of gtk-fortran, you need to add in the fpm.toml manifest:

[build]
link = ["plplot", "plplotfortran"]
external-modules = ["plplot"]

and you will build and run it with:

$  fpm run --flag '$(pkg-config --cflags --libs plplot plplot-fortran)'
Clone this wiki locally