The ncurses library is a free implementation of the curses terminal control interface for Unix. It provides functions to handle keyboard/mouse input and formatted output. In Fortran 2003, we can link M_ncurses that includes ISO C binding interfaces to ncurses for Fortran 2003. The library does not feature Unicode support nor forms.


Clone the GitHub repository and compile the source files:

$ git clone
$ cd M_ncurses/src/
$ make

You may want to change the C and Fortran compiler declarations in the Makefile. Optionally, the object files can be linked into a static library libm_ncurses.a:

$ ar rcs libm_ncurses.a M_ncurses.o macros.o nc_errmessage.o nc_printhtml.o nc_printplain.o

Link your Fortran application with libm_ncurses.a -lncurses.


The Fortran 2003 example program just initialises ncurses and outputs coloured text:

! hello.f90
program main
    use, intrinsic :: iso_c_binding, only: c_null_char
    use :: m_ncurses
    implicit none
    integer :: rc, key

    stdscr = initscr()                                  ! Start curses mode.

    rc = curs_set(0)                                    ! Disable the cursor.
    rc = start_color()                                  ! Start colour.

    rc = init_pair(1_c_short, COLOR_GREEN, COLOR_BLACK) ! Initialise colours.
    rc = attron(COLOR_PAIR(1))                          ! Enable attribute.
    rc = addstr('Hello, ncurses!' // c_null_char)       ! Print the string.
    rc = attroff(COLOR_PAIR(1))                         ! Disable attribute.

    rc = refresh()                                      ! Update the real screen.
    key = getch()                                       ! Wait for a user keystroke.
    rc = endwin()                                       ! End curses mode.
end program main

We can then simply compile and link the ncurses example with GNU Fortran:

$ gfortran10 -I./M_ncurses/src/ -o hello hello.f90 ./M_ncurses/src/libm_ncurses.a -lncurses

Point the include search path -I to the directory containing the M_ncurses module files (*.mod). If you prefer Flang, run instead:

$ flang -I/usr/local/flang/include/ -I./M_ncurses/src/ \
  -o hello hello.f90 ./M_ncurses/src/libm_ncurses.a -lncurses

Then, execute the binary:

$ ./hello
Fortran and ncurses
Fig. 1: Output of the example ncurses program