Compilation

Fortran source code is compiled by executing the desired compiler with given input and output files. The following basic example can be built with all modern compilers:

! hello.f90
program main
    print '(a)' 'Hello, World!'
end program main

Save the source code to hello.f90. To compile the example with GNU Fortran, run:

$ gfortran7 -Wl,-rpath=/usr/local/lib/gcc7/ -o hello hello.f90

On FreeBSD, GNU Fortran 7 requires the runtime library search path to be set to /usr/local/lib/gcc7/. The path differs for other versions of GCC. Compiled binaries depend on the shared runtime library libgfortran, unless the parameter -static-libgfortran is used.

To compile the example with Flang, run:

$ flang -o hello hello.f90

Both compilers create a binary hello, which can be executed:

$ ./hello
Hello, World!