Skip to content

Tutorial 5

Vincent Magnin edited this page Jun 25, 2021 · 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 is supposed to be installed in your system (with sudo make install on a Unix-like system), following instructions on the main wiki page.
  • A Fortran Package Manager fpm >=0.3.0 is also supposed to be installed.

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

As our program uses the gtk and g (for GLib) gtk-fortran modules, the fpm.toml manifest must contain:

[build]
external-modules = ["gtk", "g"]

Let's have a look at the flags returned by pkg-config:

$ pkg-config --cflags --libs gtk-4-fortran
-rdynamic -mfpmath=sse -msse -msse2 -pthread -I/usr/local/include/gtk-4-fortran -I/usr/include/gtk-4.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/graphene-1.0 -I/usr/lib/x86_64-linux-gnu/graphene-1.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -Xlinker -R/usr/local/lib -lgtk-4-fortran -lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

The --cflags option returns flags for the compiler, the second option --libs returns options for the linker:

$ pkg-config --libs gtk-4-fortran
-Xlinker -R/usr/local/lib -lgtk-4-fortran -lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

We will keep the first two options -Xlinker -R/usr/local/lib in our command line but will put the names of the libraries to be linked in the build section of the fpm.toml file:

[build]
link = [
  "gtk-4",
  "pangocairo-1.0",
  "pango-1.0",
  "harfbuzz",
  "gdk_pixbuf-2.0",
  "cairo-gobject",
  "cairo",
  "vulkan",
  "graphene-1.0",
  "gio-2.0",
  "gobject-2.0",
  "glib-2.0",
  "gtk-4-fortran" ]
external-modules = ["gtk", "g"]

Building and running the project

The project can now be built and run with the command:

$ fpm run --flag '$(pkg-config --cflags gtk-4-fortran) -Xlinker -R/usr/local/lib'

That's all folks!

Remarks

  • If you first build the project with fpm build --flag '$(pkg-config --cflags gtk-4-fortran) -Xlinker -R/usr/local/lib', you will be obliged to launch fpm run with the same flags, because each fpm build is identified by a hash of the command line arguments.
  • In the [build] section, we have listed all the libraries composing GTK, but for that small example we only really need:
link = [
  "gtk-4",
  "gio-2.0",
  "gobject-2.0",
  "glib-2.0",
  "gtk-4-fortran",
]
Clone this wiki locally