Skip to content

Tutorial 5

Vincent Magnin edited this page May 12, 2022 · 13 revisions

Using gtk-fortran as a fpm dependency

We demonstrate how the Fortran Package Manager fpm can be used to build and run a gtk-fortran program, starting from the gtkzero_gapp.f90 example, which just opens an empty GTK 4 window. The source of the resulting fpm project is available in the following repository:

https://github.com/vmagnin/gtkzero_fpm

Prerequisites

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

Creating the fpm project

An hello world project is first created by:

$ fpm new gtkzero_fpm
$ cd gtkzero_fpm

The gtkzero_gapp.f90 file is just composed of the main Fortran program and a module named handlers. In the fpm version, we put the main program into the app/main.f90 file and the module into src/handlers.f90 (the file must have the name of the module).

The tree of the project is:

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

Configuring the fpm project

The fpm.toml manifest must contain a dependencies section with the needed branch of gtk-fortran:

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

Building and running the project

The project can then be built and run very simply:

$ fpm run

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

That's all folks!

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