Fortran Graphical Interface Development on Debian: Key Approaches and Tools
Fortran, traditionally used for scientific computing, can be extended to develop graphical user interfaces (GUIs) by leveraging bindings to popular C/C++ GUI libraries (e.g., GTK) or specialized Fortran libraries. Below are the primary methods for GUI development on Debian, along with setup instructions, examples, and tool recommendations.
gtk-fortran is an open-source project that provides Fortran bindings to the GTK library (GTK3/GTK4), enabling the creation of cross-platform GUIs with native look-and-feel. It is actively maintained and integrates seamlessly with Debian’s package ecosystem.
gtk-fortran can be installed via two methods:
conda-forge
(supports Linux/macOS) or fpm
(Fortran Package Manager):# Using conda-forge (install conda first)
conda install -c conda-forge gtk-4-fortran
# Using fpm (add the community repository first)
fpm install gtk-fortran
git clone https://github.com/vmagnin/gtk-fortran.git
mkdir gtk-fortran/build && cd gtk-fortran/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
make && sudo make install
The following program creates a GTK window using gtk-fortran (GTK4):
program simple_window
use gtk, only: gtk_application_new, gtk_application_run, gtk_application_quit
use iso_c_binding, only: c_ptr, c_null_ptr
implicit none
type(c_ptr) :: app
! Initialize GTK application
app = gtk_application_new("org.example.simple", 0_c_int)
call gtk_application_run(app, 0_c_int, c_null_ptr)
call gtk_application_quit(app)
end program simple_window
Compilation:
gfortran -o simple_window simple_window.f90 `pkg-config --cflags --libs gtk4-fortran`
Execution:
./simple_window
This will display a blank GTK window. The gtk-fortran wiki provides extensive tutorials for adding buttons, labels, and event handlers.
FLTK is a lightweight, cross-platform GUI library written in C++. Fortran bindings (e.g., fltk4fortran
) allow Fortran developers to create simple, fast GUIs.
Install FLTK and Fortran bindings via apt
:
sudo apt install fltk1.3-dev fortfltk
program simple_fltk
use fltk
implicit none
type(fltk_window) :: win
! Create a window
win = fltk_window(300, 200, "FLTK Window")
call win%show()
! Run the FLTK event loop
call fltk_run()
end program simple_fltk
Compilation:
gfortran -o simple_fltk simple_fltk.f90 -lfltk
Execution:
./simple_fltk
FLTK is ideal for lightweight applications but lacks advanced widgets compared to GTK.
While Qt is a powerful GUI framework, Fortran bindings for Qt (e.g., qtfortran
) are less mature and require manual integration with C++ code. This approach is not recommended for beginners due to complexity.
For data visualization without user interaction, Fortran libraries like PGPLOT (basic 2D plots) and F.EN.I.A. (2D/3D scientific plots) are useful. These libraries generate output files (e.g., PNG, SVG) that can be viewed with external tools.
sudo apt install pgplot5 fenaia
program plot_example
external pgopen, pgline, pgend
real :: x(5), y(5)
! Data points
x = [1.0, 2.0, 3.0, 4.0, 5.0]
y = [2.0, 4.0, 6.0, 8.0, 10.0]
! Open PGPLOT device (PNG file)
call pgopen('/XWINDOW') ! Or '/PNG' for file output
call pgenv(0.0, 6.0, 0.0, 12.0, 0, 0) ! Set axis ranges
call pgline(5, x, y) ! Draw line
call pgend() ! Close device
end program plot_example
Compilation:
gfortran -o plot_example plot_example.f -lpgplot -lcpgplot
Execution:
./plot_example
PGPLOT is outdated but still used for legacy projects. F.EN.I.A. supports modern formats (SVG, VTK) and is more flexible.
build-essential
, gfortran
, and libgtk-4-dev
(or libgtk-3-dev
) are installed for GUI development.conda-forge
or fpm
for easy installation of gtk-fortran.By choosing the right toolchain (gtk-fortran for full GUIs, FLTK for lightweight apps, or PGPLOT for plotting), Fortran developers on Debian can create effective graphical interfaces for scientific and engineering applications.