A public-domain Fortran 2003 interface to the ncurses library can be used to output formatted text to the console. Only two files are needed to use ncurses with Fortran:

Both files, including some examples, are also contained in the archive ncf.tgz (mirror).

! hello.f90
program main
    use ncurses                                         ! Load interface to C library.
    implicit none
    integer :: e, k

    stdscr = initscr()                                  ! Start curses mode.

    e = curs_set(0)                                     ! Disable the cursor.
    e = start_color()                                   ! Start color.

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

    e = refresh()                                       ! Update the real screen.
    k = getch()                                         ! Wait for a user keystroke.
    e = endwin()                                        ! End curses mode.
end program main
Fortran and ncurses
Fig. 1: Output of the example ncurses programme

Compiling ncurses with LLVM

We create an object file for the auxiliary C routines named macros.o using Clang and then compile the ncurses C library bindings with Flang:

$ clang -c macros.c
$ flang -c ncurses.f90

The ncurses example can be build by linking macros.o and ncurses.o:

$ flang -lncurses -o hello hello.f90 ncurses.o macros.o

The compilation can be further automated by writing a proper Makefile:

CC = clang
FC = flang

CFLAGS     = -c -Wall
LDFLAGS    = -lncurses
OBJECTS    = macros.o ncurses.o
SOURCES    = hello.f90



macros.o: macros.c
	$(CC) $(CFLAGS) macros.c

ncurses.o: ncurses.f90
	$(FC) $(CFLAGS) ncurses.f90

	rm *.o *.mod $(EXECUTABLE)

Please note the hard tabs in Makefile. The sources can be compiled with:

$ make

After compilation, we just execute our Fortran application:

$ ./hello

Compiling ncurses with GCC

You can compile the ncurses example with the GNU Compiler Collection:

$ gcc7 -c macros.c
$ gfortran7 -c ncurses.f90
$ gfortran7 -I. -Wl,-rpath=/usr/local/lib/gcc7/ -L/usr/local/lib/ -lncurses \
-o hello ncurses.o macros.o hello.f90

We can set the environment variable LD_LIBRARY_PATH instead of using a compiler flag for the shared library path:

$ setenv LD_LIBRARY_PATH /usr/local/lib/
$ gfortran7 -I. -Wl,-rpath=/usr/local/lib/gcc7/ -lncurses \
-o hello ncurses.o macros.o hello.f90