Skip to content

Tutorial 5

vmagnin edited this page Apr 18, 2022 · 13 revisions

How to use fpm to build a gtk-fortran project

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 manifest the git dependency by the local path to gtk-fortran:

[dependencies]
gtk-fortran = { path = "../gtk-fortran" }
Clone this wiki locally