Skip to content

Installation

Vincent Magnin edited this page Jun 3, 2024 · 22 revisions

Let's get started with gtk-fortran: this tutorial will guide you to install it on your system, with specific instructions for Linux, Windows, FreeBSD and macOS. It is based on the gtk4 branch.

Requirements and dependencies

gtk-fortran is packaged by volunteers for only a few Linux distributions, essentially Gentoo and Slackware based distributions (see the sites pkgs.org and repology.org for more information). In the general case, you will therefore have to build it on your system.

You need:

  • a modern Fortran compiler (Fortran 2008 standard), for example GFortran which is mainly used to develop gtk-fortran, or the Intel ifort/ifx compilers. See the Fortran-lang.org compilers page for other compilers.
  • The GTK development files (libgtk-4-dev package in Ubuntu).
  • A build system: either CMake (version>=3.7) and pkg-config (or pkgconf) to build and install the whole library, or the Fortran Package Manager fpm if you use gtk-fortran as a dependency. If you are a Fortran Package Manager fpm user and don't want to use CMake, please read the tutorial How to use fpm to build a gtk-fortran project.

Remarks:

  • If you want to build the PLplot examples, you need the PLplot development files (PLplot>=5.13) and the PLplot Cairo driver. If PLplot was not compiled with the Fortran compiler you use, you can not build those examples.
  • A few programs in the project use the g_get_os_info() function introduced in GLib 2.64, but they are not essential and you can ignore them with $ make -i if your GLib is older.

Downloading gtk-fortran

If you have a github account, just clone the repository:

$ git clone [email protected]:vmagnin/gtk-fortran.git

else you can use git with https:

$ git clone https://github.com/vmagnin/gtk-fortran.git

or you can click in your browser on the Code tab (top left), then click on the Clone or download button on the right, then click on Download ZIP and extract the files in your home directory.

Linux

First, you need to install a Fortran compiler, the GTK development files, CMake and pkg-config. In a Debian based distribution like Ubuntu just type:

$ sudo apt install gfortran libgtk-4-dev cmake pkg-config

If you want to use PLplot in gtk-fortran:

$ sudo apt install libplplot-dev plplot-driver-cairo

You may also need the libgtk-4-media-gstreamer package if you want to play media files.

Note that depending on your distribution, you may also need other packages and the names of the packages may be different.

You can then build and install gtk-fortran by typing:

$ cd gtk-fortran
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

See CMake basics for other useful options and advices.

About PLplot support

If PLplot is not installed on your machine or was not compiled with the same Fortran compiler, you must tell CMake to exclude the PLplot gtk-fortran examples before calling make:

$ cmake -D EXCLUDE_PLPLOT=true ..
$ make
$ sudo make install

On some distributions, it may also happen that PLplot was packaged without the Fortran support. But you can install it from sources following similar steps to those given for FreeBSD farther down on this page.

Testing gtk-fortran

You can test gtk-fortran by running each example in the build/examples and build/plplot directories:

$ cd examples
$ ./gtkhello

or you can use CTest to launch and test all the examples sequentially (see CMake basics).

Compiling your own programs

On Linux and Unix systems the build system generates a pkg-config file and installs it. So building a single source file application should be as simple as:

$ gfortran my_app.f90 $(pkg-config --cflags --libs gtk-4-fortran)

If you use PLplot, don't forget to add its packages in the pkg-config command:

$ gfortran my_plplot_app.f90 $(pkg-config --cflags --libs gtk-4-fortran plplot plplot-fortran)

Solving pkg-config problems

If you have made a default install to /usr/local you may need to run:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

or

$ setenv PKG_CONFIG_PATH /usr/local/lib/pkgconfig

if you use csh or one of its derivatives.

This will depend on your distribution, Ubuntu looks there by default, Pardus and Manjaro don't. Just look the messages of the sudo make install command to know that directory, for example in Ubuntu you will find that line:

-- Installing: /usr/local/lib/pkgconfig/gtk-4-fortran.pc

In Fedora it's /usr/local/lib64/pkgconfig.

Windows

There are several solutions to use gtk-fortran under Windows. In this blog post from yolegu.xyz, you will find explanations about how to build the GTK library in Windows and set Visual Studio 2022 to use gtk-fortran and Intel Fortran.

A second solution is to use WSL: in that user post, you will find some information on how to use gtk-fortran in a WSL2 Ubuntu installation and how to configure MS Visual Studio Code.

A third solution is to use a UNIX-like environment like MSYS2. These instructions were tested in Windows 10:

  • Download and execute the MSYS2 installer, then update MSYS2 as explained on the site MSYS2
  • Install the following packages via the MSYS2-MSYS shell:
    • build tools: $ pacman -S mingw-w64-ucrt-x86_64-toolchain base-devel (it will install gcc, gdb, gfortran, python, make, pkgconf...)
    • CMake and git: $ pacman -S mingw-w64-ucrt-x86_64-cmake git (it will also install ninja, curl, openssh...)
    • GTK and dependencies: $ pacman -S mingw-w64-ucrt-x86_64-gtk4
    • PLplot (optional): $ pacman -S mingw-w64-ucrt-x86_64-plplot mingw-w64-ucrt-x86_64-wxwidgets3.2-msw
    • Fortran Package Manager fpm (optional): $ pacman -S mingw-w64-ucrt-x86_64-fpm
    • If ever you want to play media files: $ pacman -S mingw-w64-ucrt-x86_64-gtk4-media-gstreamer mingw-w64-ucrt-x86_64-gst-plugins-good
  • Your MSYS2 directory size will be around 5 Gio.
  • Start the MSYS2-UCRT64 shell.
  • Install gtk-fortran either with git:
    • $ mkdir .ssh
    • And copy your key id_rsa in that .ssh directory.
    • $ git clone [email protected]:vmagnin/gtk-fortran.git
    • $ cd gtk-fortran
  • or by downloading and extracting the .tar.gz:
    • $ wget https://github.com/vmagnin/gtk-fortran/archive/gtk4.tar.gz
    • $ tar -xvzf gtk4.tar.gz
    • $ cd gtk-fortran-gtk4
  • Create a build directory:
    • $ mkdir build && cd build
  • Generate make files with:
    • $ cmake -G "MSYS Makefiles" ..
    • The reason is the default on MSYS or MINGW is "NMake Makefiles".
  • $ make -j (in MSYS2, the building can be several times slower than under Linux. make -j for parallel building will accelerate it).
  • Test the examples like described in the Linux section.
  • Install the gtk-?-fortran library into your MSYS2 directory (by default CMake uses C:\Program Files (x86) but the spaces will cause problems with pkg-config):
$ MSYS2_ARG_CONV_EXCL=- cmake .. -G"MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=$MSYSTEM_PREFIX
$ make -j install DESTDIR=/

That solution was found at https://stackoverflow.com/questions/44282788/cmake-msys2-wrong-install-path-for-library

You will also need to export the PKG_CONFIG_PATH, for example:

$ export PKG_CONFIG_PATH=C:/MSYS2/ucrt64/lib/pkgconfig/

If you have GL rendering problems with the new ngl renderer, you can switch back to the old one by setting this environment variable:

$ GSK_RENDERER=gl ./my_gtk_program

See this blog if you want to use gtk-fortran and Code::Blocks in Windows.

macOS

There is a MacPort of the gtk3 branch, which can be installed with:

sudo port install gtk-fortran

You can also use the Homebrew package manager. Follow those steps (see https://github.com/vmagnin/gtk-fortran/issues/154) where you will adapt the ln command to you gcc version:

$ brew install gcc
$ ln -s /usr/local/Cellar/gcc@7/7.5.0/bin/gfortran-7 /usr/local/bin/gfortran
$ brew install gtk4

Download gtk-fortran as explained for Linux and open a terminal in the source directory. Then export the pkg-config path and build gtk-fortran:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
$ mkdir build && cd build
$ cmake -D EXCLUDE_PLPLOT=true ..
$ make
$ sudo make install

You may also need:

$ export LIBRARY_PATH=/usr/local/lib/

Installing PLplot in macOS

You need to download the sources (see the FreeBSD section) then build PLplot so that it has been built with the same version of gfortran. The PLD_xcairo setting needs to be off:

$ mkdir build && cd build
$ cmake -D PLD_xcairo=OFF ..
$ make
$ sudo make install

FreeBSD

Install these packages (replace gcc13 and gfortran13 by the version you want):

# pkg install git cmake pkgconf gcc13 gtk4

After cloning the gtk-fortran directory:

$ cd gtk-fortran
$ mkdir build && cd build
$ cmake -D EXCLUDE_PLPLOT=true -D CMAKE_Fortran_COMPILER:FILEPATH="/usr/local/bin/gfortran13" ..
$ make
$ cd examples
$ ./gtkhello

The environment variable LD_PRELOAD is necessary if you have an error concerning ligcc_s.so.1:

$ export LD_PRELOAD=/usr/local/lib/gcc13/libgcc_s.so.1

Installing PLplot in FreeBSD

Unhappily, the plplot package in FreeBSD is by default not configured for Fortran. But you can install PLplot either from ports and configure it, or from sources. Here we download and build ourselves the PLplot sources:

$ wget "https://downloads.sourceforge.net/project/plplot/plplot/5.15.0 Source/plplot-5.15.0.tar.gz"
$ tar -xf plplot-5.15.0.tar.gz
$ mkdir build_plplot && cd build_plplot
$ export FC=/usr/local/bin/gfortran13
$ cmake -D CMAKE_INSTALL_PREFIX=/home/mylogin/myPLplot ../plplot-5.15.0
$ make
$ sudo make install

If you have problems with the Qt driver and/or don't need it (and want to avoid downloading its numerous packages), add to CMake the option -D DEFAULT_NO_QT_DEVICES=ON. Other PLplot building options are listed here.

After building PLplot, type in your gtk-fortran/build directory:

$ PKG_CONFIG_PATH=/home/mylogin/myPLplot/lib/pkgconfig cmake ..

Uninstalling gtk-fortran

It will generally be as easy as typing from the build directory:

$ sudo make uninstall

Another solution is to remove by hand the files listed in build/install_manifest.txt

See https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake

Clone this wiki locally