GFORTRAN(1)

๐Ÿ“ฆ
version 9.5.0 ยท 2 versions
, in ubuntu noble

๐Ÿ’– GFORTRAN(1) ยท GNU Manual โœจ

โœจ NAME โœจ

gfortran - the GNU Fortran compiler ๐Ÿงš

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

gfortran [-c|-S|-E]
         [-g] [-pg] [-Olevel]
         [-Wwarn...] [-pedantic]
         [-Idir...] [-Ldir...]
         [-Dmacro[=defn]...] [-Umacro]
         [-foption...]
         [-mmachine-option...]
         [-o outfile] infile...

Okay so this is only the most useful stuff, bestie. The rest is further down ๐Ÿ‘€

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Okay so gfortran supports every option that gcc supports, no cap. This page only covers the options that are specific to GNU Fortran, so you're not drowning in extra stuff ๐Ÿซถ

Every GCC and GNU Fortran option works with both gfortran and gcc, and also with any other drivers that were built at the same time (like g++). Adding GNU Fortran to the GCC distribution means all the relevant drivers accept GNU Fortran options. It's giving teamwork ๐ŸŒˆ

Some options come in positive and negative forms. The negative form of -ffoo is -fno-foo. This manual only documents one of the two, whichever one isn't the default. So don't be confused when you only see one version ๐Ÿ™ˆ

๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

Here's the cheat sheet of every option specific to GNU Fortran, sorted into groups by type. The explanations come after the groups โœจ

๐Ÿฆ‹ Fortran Language Options

-fall-intrinsics -fbackslash -fcray-pointer -fd-lines-as-code
-fd-lines-as-comments -fdec -fdec-structure -fdec-intrinsic-ints
-fdec-static -fdec-math -fdec-include -fdefault-double-8
-fdefault-integer-8 -fdefault-real-8 -fdefault-real-10
-fdefault-real-16 -fdollar-ok -ffixed-line-length-n
-ffixed-line-length-none -fpad-source -ffree-form
-ffree-line-length-n -ffree-line-length-none -fimplicit-none
-finteger-4-integer-8 -fmax-identifier-length -fmodule-private
-ffixed-form -fno-range-check -fopenacc -fopenmp -freal-4-real-10
-freal-4-real-16 -freal-4-real-8 -freal-8-real-10 -freal-8-real-16
-freal-8-real-4 -std=std -ftest-forall-temp

๐ŸŒˆ Preprocessing Options

-A-question[=answer] -Aquestion=answer -C -CC -Dmacro[=defn] -H -P
-Umacro -cpp -dD -dI -dM -dN -dU -fworking-directory -imultilib dir
-iprefix file -iquote -isysroot dir -isystem dir -nocpp -nostdinc
-undef

๐ŸŒธ Error and Warning Options

-Waliasing -Wall -Wampersand -Wargument-mismatch -Warray-bounds
-Wc-binding-type -Wcharacter-truncation -Wconversion -Wdo-subscript
-Wfunction-elimination -Wimplicit-interface -Wimplicit-procedure
-Wintrinsic-shadow -Wuse-without-only -Wintrinsics-std
-Wline-truncation -Wno-align-commons -Wno-tabs -Wreal-q-constant
-Wsurprising -Wunderflow -Wunused-parameter -Wrealloc-lhs
-Wrealloc-lhs-all -Wfrontend-loop-interchange -Wtarget-lifetime
-fmax-errors=n -fsyntax-only -pedantic -pedantic-errors

๐Ÿ”ฅ Debugging Options

-fbacktrace -fdump-fortran-optimized -fdump-fortran-original
-fdump-fortran-global -fdump-parse-tree -ffpe-trap=list
-ffpe-summary=list

๐Ÿงš Directory Options

-Idir  -Jdir -fintrinsic-modules-path dir
-static-libgfortran

๐Ÿ“ Runtime Options

-fconvert=conversion -fmax-subrecord-length=length
-frecord-marker=length -fsign-zero

๐Ÿซง Interoperability Options

-fc-prototypes -fc-prototypes-external

๐Ÿ’ป Code Generation Options

-faggressive-function-elimination -fblas-matmul-limit=n
-fbounds-check -ftail-call-workaround -ftail-call-workaround=n
-fcheck-array-temporaries
-fcheck=<all|array-temps|bounds|do|mem|pointer|recursion>
-fcoarray=<none|single|lib> -fexternal-blas -ff2c
-ffrontend-loop-interchange -ffrontend-optimize -finit-character=n
-finit-integer=n -finit-local-zero -finit-derived
-finit-logical=<true|false> -finit-real=<zero|inf|-inf|nan|snan>
-finline-matmul-limit=n -fmax-array-constructor=n
-fmax-stack-var-size=n -fno-align-commons -fno-automatic
-fno-protect-parens -fno-underscoring -fsecond-underscore
-fpack-derived -frealloc-lhs -frecursive -frepack-arrays
-fshort-enums -fstack-arrays

๐ŸŒธ Options controlling Fortran dialect

These options control the details of the Fortran dialect the compiler accepts. Here they are, one by one ๐Ÿ’…

  • -ffree-form, -ffixed-form โ€” These pick the layout of the source file. Free form came in with Fortran 90, while fixed form was the old-school way older Fortran programs were written. If you don't specify either one, the compiler figures out the source form from the file extension ๐Ÿ“
  • -fall-intrinsics โ€” This makes every intrinsic procedure work, including GNU's own extensions. It's super handy alongside -std=f95 if you want standard compliance but still want the full set of gfortran intrinsics. The catch is that -Wintrinsics-std gets ignored, and no user-defined procedure that shares a name with an intrinsic will get called, unless it's explicitly declared EXTERNAL ๐Ÿ™ƒ
  • -fd-lines-as-code, -fd-lines-as-comments โ€” These give special treatment to lines starting with d or D in fixed form sources. With -fd-lines-as-code, they're treated as if the first column had a blank in it. With -fd-lines-as-comments, they're treated as comment lines ๐Ÿ’ฌ
  • -fdec โ€” This turns on DEC compatibility mode, which enables extensions and other features that copy the default behavior of older compilers (like DEC's). Fair warning:
These features are non-standard, so avoid them at all costs. For the nitty-gritty on how GNU Fortran implements these extensions, check the full documentation.

Turning this on also flips on -fdollar-ok, -fcray-pointer, -fdec-structure, -fdec-intrinsic-ints, -fdec-static, and -fdec-math. And if -fd-lines-as-code or -fd-lines-as-comments aren't set, then -fdec also sets -fd-lines-as-comments ๐Ÿซ 

  • -fdec-structure โ€” This enables DEC "STRUCTURE" and "RECORD", plus "UNION", "MAP", and the dot ('.') as a member separator (on top of '%'). It's here for compatibility only. Fortran 90 derived types are the better choice wherever you can use them ๐ŸŒท
  • -fdec-intrinsic-ints โ€” This enables the B/I/J/K kind variants of existing integer functions, like BIAND, IIAND, JIAND, and so on. For the full list of intrinsics, check the full documentation ๐Ÿ”ข
  • -fdec-math โ€” This enables legacy math intrinsics like COTAN and the degree-valued trig functions (like TAND, ATAND, and so on) so older code keeps working ๐Ÿ“
  • -fdec-static โ€” This enables DEC-style STATIC and AUTOMATIC attributes, which let you explicitly set the storage of variables and other objects ๐Ÿงฑ
  • -fdec-include โ€” This lets INCLUDE be parsed as a statement, as well as the regular INCLUDE line. When it's parsed as a statement, INCLUDE doesn't have to sit on a single line and can use line continuations โœ‚๏ธ
  • -fdollar-ok โ€” This lets $ show up as a valid non-first character in a symbol name. Symbols that start with $ still get rejected, because it's unclear which implicit typing rules should apply, since different vendors do it differently. Using $ in IMPLICIT statements is also rejected ๐Ÿšซ
  • -fbackslash โ€” This changes how backslashes work in string literals. Instead of being a single backslash character, they become "C-style" escape characters. These get expanded: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\\", and "\0" become the ASCII alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL characters, in that order. On top of that, "\x"nn, "\u"nnnn, and "\U"nnnnnnnn (where each n is a hexadecimal digit) get translated into the Unicode characters for those code points. Any other combination of a character preceded by \ stays unexpanded ๐ŸŒˆ
  • -fmodule-private โ€” This sets the default accessibility of module entities to PRIVATE. Use-associated entities won't be accessible unless they're explicitly declared PUBLIC ๐Ÿ”’
  • -ffixed-line-length-n โ€” This sets the column after which characters get ignored in typical fixed-form lines in the source file. Unless you use -fno-pad-source, it also decides how far spaces are assumed (as if the line were padded out to that length) after the ends of short fixed-form lines. Popular values for n are 72 (the standard and the default), 80 (card image), and 132 (matching the "extended-source" options in some popular compilers). n can also be none, which means the entire line is meaningful and continued character constants never get implicit spaces tacked on to fill out the line. -ffixed-line-length-0 does the exact same thing as -ffixed-line-length-none ๐Ÿ“
  • -fno-pad-source โ€” Normally, fixed-form lines have spaces assumed (as if padded to that length) after the ends of short fixed-form lines. That doesn't happen if you use -ffixed-line-length-0, -ffixed-line-length-none, or -fno-pad-source. With any of those, continued character constants never get implicit spaces appended to fill out the line ๐Ÿ™…โ€โ™€๏ธ
  • -ffree-line-length-n โ€” This sets the column after which characters get ignored in typical free-form lines in the source file. The default is 132. n can be none, meaning the entire line is meaningful. -ffree-line-length-0 means the same thing as -ffree-line-length-none ๐Ÿ“
  • -fmax-identifier-length=n โ€” This sets the maximum allowed identifier length. Typical values are 31 (Fortran 95) and 63 (Fortran 2003 and Fortran 2008) ๐Ÿท๏ธ
  • -fimplicit-none โ€” This says no implicit typing is allowed, unless an explicit IMPLICIT statement overrides it. It's the same as adding implicit none to the start of every procedure ๐Ÿค
  • -fcray-pointer โ€” This enables the Cray pointer extension, which gives you C-like pointer functionality ๐Ÿ‘‰
  • -fopenacc โ€” This enables the OpenACC extensions. That includes OpenACC !$acc directives in free form, c$acc, *$acc, and !$acc directives in fixed form, !$ conditional compilation sentinels in free form, and c$, *$, and !$ sentinels in fixed form. When linking, it also makes sure the OpenACC runtime library gets linked in.
This is an experimental feature, so it's incomplete and subject to change in future versions of GCC. For more info, check https://gcc.gnu.org/wiki/OpenACC ๐Ÿงช
  • -fopenmp โ€” This enables the OpenMP extensions. That includes OpenMP !$omp directives in free form, c$omp, *$omp, and !$omp directives in fixed form, !$ conditional compilation sentinels in free form, and c$, *$, and !$ sentinels in fixed form. When linking, it also makes sure the OpenMP runtime library gets linked in. And heads up, -fopenmp implies -frecursive ๐Ÿงต
  • -fno-range-check โ€” okay so this one turns OFF the range checks when gfortran is folding constant expressions at compile time ๐Ÿ™ˆ. Normally it'll yell at you for a = 1. / 0, but with this flag no error shows up and a just gets +Infinity โœจ. If a value lands outside the range of ["-HUGE()":HUGE()], it gets swapped for -Inf or +Inf, whichever fits. The DATA i/Z'FFFFFFFF'/ example is iconic: it'd normally overflow on most systems, but with -fno-range-check it wraps around and i ends up as -1 ๐Ÿ”ฅ
  • -fdefault-integer-8 โ€” sets the default integer and logical types to an 8 byte wide type ๐Ÿ’–. It also changes the kind of integer constants like 42. Unlike -finteger-4-integer-8, it does not promote variables that already have an explicit kind declaration, so those stay as they are ๐Ÿซถ
  • -fdefault-real-8 โ€” sets the default real type to an 8 byte wide type ๐ŸŒธ. It also changes the kind of non-double real constants like 1.0. This one bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if that's possible. If you also pass -fdefault-double-8, then DOUBLE PRECISION and double real constants won't get promoted. Unlike -freal-4-real-8, -fdefault-real-8 doesn't touch variables with explicit kind declarations ๐Ÿ’…
  • -fdefault-real-10 โ€” same energy as -fdefault-real-8, but the default real type becomes a 10 byte wide type ๐Ÿฆ‹. It changes non-double real constants like 1.0, and it bumps DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible. Passing -fdefault-double-8 along with it keeps DOUBLE PRECISION and double real constants from being promoted. And just like before, it won't mess with variables that have explicit kind declarations, unlike -freal-4-real-10 ๐Ÿงš
  • -fdefault-real-16 โ€” sets the default real type to a 16 byte wide type ๐Ÿ‘‘. It changes the kind of non-double real constants like 1.0, and bumps DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible. With -fdefault-double-8 added, DOUBLE PRECISION and double real constants stay put. Explicitly kinded variables are left alone, unlike with -freal-4-real-16 โœจ
  • -fdefault-double-8 โ€” sets DOUBLE PRECISION and double real constants like 1.d0 to an 8 byte wide type ๐Ÿ“. If that's already the default, it does nothing. This option stops -fdefault-real-8, -fdefault-real-10, and -fdefault-real-16 from promoting DOUBLE PRECISION and double real constants like 1.d0 to 16 bytes, so it's the one to reach for when you want those to stay small ๐Ÿ’œ
  • -finteger-4-integer-8 โ€” promotes all INTEGER(KIND=4) entities to INTEGER(KIND=8) entities ๐Ÿ˜ญ. If KIND=8 isn't available, you'll get an error. Be careful with this one, bestie, because it may not suit your codes โš ๏ธ. The areas to watch are calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O. It's suggested that you inspect the intermediate representation of the translated Fortran code, which you can produce with -fdump-tree-original ๐Ÿ”
  • -freal-4-real-8, -freal-4-real-10, -freal-4-real-16, -freal-8-real-4, -freal-8-real-10, -freal-8-real-16 โ€” these all promote every REAL(KIND=M) entity to REAL(KIND=N) ๐ŸŒˆ. If REAL(KIND=N) isn't available, you'll get an error. Every other real kind is unaffected. Same warning as before, babe: these may not suit your codes ๐Ÿ’€. Watch out for calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O. Again, inspecting the intermediate representation from -fdump-tree-original is suggested ๐Ÿซถ
  • -std=std โ€” tell gfortran which standard your program is supposed to follow ๐Ÿ“œ. You can pick f95, f2003, f2008, f2018, gnu, or legacy. The default is gnu, which is a superset of the latest Fortran standard that includes every GNU Fortran extension, though you'll get warnings for obsolete extensions that aren't recommended in new code ๐Ÿ™„. The legacy value is basically the same but without those obsolete-extension warnings, which is useful for old non-standard programs ๐Ÿง“. The f95, f2003, f2008, and f2018 values enforce strict conformance to Fortran 95, Fortran 2003, Fortran 2008, and Fortran 2018 respectively. Anything beyond that standard gets an error, and Fortran 77 features that are allowed but obsolescent in later standards get warnings ๐Ÿšซ. The deprecated -std=f2008ts is an alias for -std=f2018. It's only there for backwards compatibility with earlier gfortran versions, so please don't use it anymore ๐Ÿฅ€
  • -ftest-forall-temp โ€” boosts test coverage by forcing most forall assignments to use a temporary ๐Ÿงช

โœจ Enable and customize preprocessing โœจ

Okay, these are all the preprocessor options ๐ŸŽ€. Check out the section Preprocessing and conditional compilation for the deeper dive on preprocessing in gfortran ๐Ÿ“–

  • -cpp, -nocpp โ€” -cpp turns preprocessing on ๐ŸŒŸ. The preprocessor gets called automatically if your file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03, or .F08. Use -cpp to manually enable preprocessing for any kind of Fortran file ๐Ÿ’–.

To turn preprocessing OFF for files with any of those extensions, use the negative form, -nocpp ๐Ÿ™…โ€โ™€๏ธ.

The preprocessor runs in traditional mode ๐Ÿ•ฐ๏ธ. Any file-format restrictions, especially the line length limits, still apply to the preprocessed output, so it might be a good idea to use -ffree-line-length-none or -ffixed-line-length-none ๐Ÿ“

  • -dM โ€” instead of the normal output, you get a list of #define directives for every macro defined while the preprocessor runs, predefined ones included ๐Ÿงš. It's a super handy way to see what's predefined in your version of the preprocessor ๐Ÿ‘€. If you don't have a file called foo.f90, this command will show all the predefined macros:
touch foo.f90; gfortran -cpp -E -dM foo.f90
  • -dD โ€” like -dM, but with two differences ๐Ÿ’…. It leaves out the predefined macros, and it outputs both the #define directives AND the result of preprocessing. Both kinds of output go to the standard output file ๐Ÿ“ค
  • -dN โ€” like -dD, but it only emits the macro names and not their expansions ๐Ÿฆ‹
  • -dU โ€” like -dD except only macros that get expanded, or whose definedness gets tested in preprocessor directives, are output ๐ŸŽฏ. The output is delayed until the macro is used or tested. #undef directives are also output for macros that were tested but were undefined at that time ๐Ÿ™ˆ
  • -dI โ€” outputs #include directives alongside the result of preprocessing ๐ŸŒธ
  • -fworking-directory โ€” turns on generating linemarkers in the preprocessor output so the compiler knows the current working directory at preprocessing time ๐Ÿ—บ๏ธ. With this on, the preprocessor emits a second linemarker after the initial one, containing the current working directory followed by two slashes. If the directory is present in the preprocessed input, GCC uses it as the current working directory in some debugging information formats ๐Ÿ›. This is implicitly enabled when debugging information is on, but you can block that with the negated form, -fno-working-directory ๐Ÿšซ. Heads up: if the -P flag is in the command line, this option does nothing at all, because no #line directives get emitted whatsoever ๐Ÿ˜ญ
  • -idirafter dir โ€” searches dir for include files, but only AFTER all the directories given with -I and the standard system directories have been exhausted ๐Ÿ. dir gets treated as a system include directory. If dir begins with =, then the = gets replaced by the sysroot prefix (see --sysroot and -isysroot) ๐ŸŒฟ
  • -imultilib dir โ€” uses dir as a subdirectory of the directory containing target-specific C++ headers ๐Ÿ“š
  • -iprefix prefix โ€” sets prefix as the prefix for subsequent -iwithprefix options ๐Ÿท๏ธ. If the prefix is a directory, include the final / ๐Ÿ’…
  • -isysroot dir โ€” this one's like --sysroot, but it only applies to header files ๐Ÿ“‚. Check the --sysroot option for more deets ๐Ÿ‘‘
  • -iquote dir โ€” searches dir ONLY for header files requested with #include "file" ๐Ÿ’Œ. They're not searched for #include <file>. This happens before all the -I directories and before the standard system directories ๐Ÿฅ‡. If dir begins with =, then the = gets replaced by the sysroot prefix (see --sysroot and -isysroot) ๐ŸŒฟ
  • -isystem dir โ€” searches dir for header files, AFTER all the -I directories but BEFORE the standard system directories ๐Ÿ›๏ธ. It gets marked as a system directory, so it gets the same special treatment as the standard system directories โœจ. If dir begins with =, then the = gets replaced by the sysroot prefix (see --sysroot and -isysroot) ๐ŸŒฟ
  • -nostdinc โ€” don't search the standard system directories for header files ๐Ÿ™…โ€โ™€๏ธ. Only the directories you specified with -I options (plus the directory of the current file, if appropriate) get searched ๐Ÿ”Ž
  • -undef โ€” don't predefine any system-specific or GCC-specific macros ๐Ÿงผ. The standard predefined macros stay defined ๐Ÿ’œ
  • -Apredicate=answer โ€” makes an assertion with the predicate predicate and the answer answer ๐Ÿง . This form is preferred over the older -A predicate(answer) form, which is still supported, because it doesn't use shell special characters ๐Ÿš
  • -A-predicate=answer โ€” cancels an assertion with the predicate predicate and the answer answer โŒ
  • -C โ€” don't discard comments ๐Ÿ’ฌ. All comments get passed through to the output file, EXCEPT for comments inside processed directives, which get deleted along with the directive ๐Ÿ—‘๏ธ

Okay so the -C option has some side effects, bestie ๐Ÿšจ It makes the preprocessor treat comments as real tokens in their own right. A comment sitting at the start of what would be a directive line turns that line into an ordinary source line, because the first token on it is no longer a #. Chaotic, I know ๐Ÿ’€

Warning: this currently handles C-style comments only ๐Ÿ˜ญ The preprocessor does not yet recognize Fortran-style comments.
  • -CC โ€” don't discard comments, not even during macro expansion ๐Ÿ’– It's like -C, except comments inside macros also get passed through to the output file wherever that macro gets expanded.

On top of the -C side effects, -CC converts all C++-style comments inside a macro into C-style comments ๐Ÿ™ˆ Why? So a later use of that macro can't accidentally comment out the rest of the source line. It's generally used to support lint comments ๐Ÿซถ

Warning: this currently handles C- and C++-style comments only. The preprocessor does not yet recognize Fortran-style comments.
  • -Dname โ€” predefines name as a macro, with the definition 1 ๐ŸŒธ
  • -Dname=definition โ€” the contents of definition get tokenized and processed as if they showed up during translation phase three in a #define directive ๐Ÿงš The big catch: embedded newline characters will truncate the definition.

If you're invoking the preprocessor from a shell (or something shell-like), you might need the shell's quoting syntax to protect characters like spaces that have a meaning in shell syntax ๐Ÿš

Want a function-like macro on the command line? Write its argument list with surrounding parentheses before the equals sign (if any). Parentheses mean stuff to most shells, so you'll need to quote the option. With sh and csh, "-D'name(args...)=definition'" works ๐Ÿ’…

-D and -U options are processed in the order you give them on the command line. All -imacros file and -include file options get processed after all the -D and -U options โœจ

  • -H โ€” prints the name of each header file used, along with all the other normal activities ๐Ÿ“š Each name is indented to show how deep it sits in the #include stack.
  • -P โ€” inhibits generation of linemarkers in the preprocessor output ๐Ÿฆ‹ This might be useful when you're running the preprocessor on something that isn't C code and will be sent to a program that might get confused by the linemarkers.
  • -Uname โ€” cancels any previous definition of name, whether it was built in or provided with a -D option ๐Ÿงน

โœจ Options to request or suppress errors and warnings โœจ

Errors are diagnostic messages reporting that GNU Fortran cannot compile the relevant piece of source code ๐Ÿ™… The compiler keeps going to try to report further errors so you can debug, but it won't produce any compiled output.

Warnings are diagnostic messages about constructions that aren't inherently erroneous but are risky or suggest there's likely a bug in the program ๐Ÿ‘€ Unless -Werror is specified, they don't prevent your program from compiling.

You can request lots of specific warnings with options beginning -W, for example -Wimplicit to get warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn warnings off, for example -Wno-implicit. This manual only lists one of the two forms, whichever is not the default ๐Ÿง 

These options control the amount and kinds of errors and warnings GNU Fortran produces:

  • -fmax-errors=n โ€” caps the number of error messages at n, at which point GNU Fortran bails out instead of trying to keep going ๐Ÿ›‘ If n is 0, there is no limit on the number of error messages produced.
  • -fsyntax-only โ€” checks the code for syntax errors but doesn't actually compile it ๐Ÿง This will generate module files for each module present in the code, but no other output file.
  • -Wpedantic, -pedantic โ€” issues warnings for uses of extensions to Fortran ๐Ÿ“ -pedantic also applies to C-language constructs where they occur in GNU Fortran source files, such as use of \e in a character constant within a directive like "#include".

Valid Fortran programs should compile properly with or without this option. Without it, though, certain GNU extensions and traditional Fortran features are supported too. With this option, many of them get rejected ๐Ÿ™ƒ

Some users try to use -pedantic to check programs for conformance. They soon find it doesn't do quite what they wantโ€”it finds some nonstandard practices, but not all. However, improvements to GNU Fortran in this area are welcome ๐Ÿ™

This should be used in conjunction with -std=f95, -std=f2003, -std=f2008 or -std=f2018.

  • -pedantic-errors โ€” like -pedantic, except that errors are produced rather than warnings ๐Ÿšซ
  • -Wall โ€” enables commonly used warning options pertaining to usage we recommend avoiding and that we believe are easy to avoid ๐ŸŒˆ This currently includes -Waliasing, -Wampersand, -Wconversion, -Wsurprising, -Wc-binding-type, -Wintrinsics-std, -Wtabs, -Wintrinsic-shadow, -Wline-truncation, -Wtarget-lifetime, -Winteger-division, -Wreal-q-constant, -Wunused and -Wundefined-do-loop.
  • -Waliasing โ€” warns about possible aliasing of dummy arguments ๐Ÿ‘ฏ Specifically, it warns if the same actual argument is associated with a dummy argument with INTENT(IN) and a dummy argument with INTENT(OUT) in a call with an explicit interface.

The following example will trigger the warning:

interface
  subroutine bar(a,b)
    integer, intent(in) :: a
    integer, intent(out) :: b
  end subroutine
end interface
integer :: a

call bar(a,a)
  • -Wampersand โ€” warns about a missing ampersand in continued character constants ๐Ÿ’… The warning is given with -Wampersand, -pedantic, -std=f95, -std=f2003, -std=f2008 and -std=f2018.

Note: With no ampersand given in a continued character constant, GNU Fortran assumes continuation at the first non-comment, non-whitespace character after the ampersand that initiated the continuation.

  • -Wargument-mismatch โ€” warns about type, rank, and other mismatches between formal parameters and actual arguments to functions and subroutines ๐Ÿงฉ These warnings are recommended and thus enabled by default.
  • -Warray-temporaries โ€” warns about array temporaries generated by the compiler ๐Ÿ—‘๏ธ The information from this warning is sometimes useful in optimization, so you can avoid such temporaries.
  • -Wc-binding-type โ€” warns if a variable might not be C interoperable ๐Ÿ”— In particular, it warns if the variable has been declared using an intrinsic type with default kind instead of using a kind parameter defined for C interoperability in the intrinsic ISO_C_Binding module. This option is implied by -Wall.
  • -Wcharacter-truncation โ€” warns when a character assignment will truncate the assigned string โœ‚๏ธ
  • -Wline-truncation โ€” warns when a source code line will be truncated ๐Ÿ“ This option is implied by -Wall. For free-form source code, the default is -Werror=line-truncation, so truncations are reported as errors.
  • -Wconversion โ€” warns about implicit conversions that are likely to change the value of the expression after conversion ๐Ÿ”„ Implied by -Wall.
  • -Wconversion-extra โ€” warns about implicit conversions between different types and kinds ๐Ÿงช This option does not imply -Wconversion.
  • -Wextra โ€” enables some warning options for usages of language features which may be problematic ๐Ÿฌ This currently includes -Wcompare-reals, -Wunused-parameter and -Wdo-subscript.
  • -Wfrontend-loop-interchange โ€” enables warnings for loop interchanges performed by the -ffrontend-loop-interchange option ๐Ÿ”
  • -Wimplicit-interface โ€” warns if a procedure is called without an explicit interface ๐Ÿ•ต๏ธ Note this only checks that an explicit interface is present. It does not check that the declared interfaces are consistent across program units.
  • -Wimplicit-procedure โ€” warns if a procedure is called that has neither an explicit interface nor has been declared as "EXTERNAL" ๐Ÿซ 
  • -Winteger-division โ€” warns if a constant integer division truncates its result ๐Ÿ• As an example, 3/5 evaluates to 0.
  • -Wintrinsics-std โ€” warns if gfortran finds a procedure named like an intrinsic that isn't available in the currently selected standard (with -std) and treats it as an "EXTERNAL" procedure because of this ๐Ÿง -fall-intrinsics can be used to never trigger this behavior and always link to the intrinsic regardless of the selected standard.
  • -Wreal-q-constant โ€” produces a warning if a real-literal-constant contains a "q" exponent-letter ๐Ÿ”ข
  • -Wsurprising โ€” produces a warning when "suspicious" code constructs are encountered ๐Ÿซข While technically legal, these usually indicate that an error has been made. This currently produces a warning under the following circumstances:
    • An INTEGER SELECT construct has a CASE that can never be matched as its lower value is greater than its upper value.
    • A LOGICAL SELECT construct has three CASE statements.
    • A TRANSFER specifies a source that is shorter than the destination.
    • The type of a function result is declared more than once with the same type. If -pedantic or standard-conforming mode is enabled, this is an error.
    • A "CHARACTER" variable is declared with negative length.
  • -Wtabs โ€” tabs are accepted as whitespace by default, but tabs are not members of the Fortran Character Set โš ๏ธ For continuation lines, a tab followed by a digit between 1 and 9 is supported. -Wtabs will cause a warning to be issued if a tab is encountered. Note, -Wtabs is active for -pedantic, -std=f95, -std=f2003, -std=f2008, -std=f2018 and -Wall.
  • -Wundefined-do-loop โ€” warns if a DO loop with step either 1 or -1 yields an underflow or an overflow during iteration of an induction variable of the loop ๐Ÿ” This option is implied by -Wall.
  • -Wunderflow โ€” produces a warning when numerical constant expressions are encountered which yield an UNDERFLOW during compilation ๐ŸŒŠ Enabled by default.
  • -Wintrinsic-shadow: okay so this one warns if your own procedure or module procedure has the same name as an intrinsic ๐Ÿ‘€. Calls might not resolve to the one you actually meant, so you may need an explicit interface or an EXTERNAL or INTRINSIC declaration to get things back on track ๐ŸŽ€. This is implied by -Wall ๐Ÿ’….
  • -Wuse-without-only: warns if a USE statement has no ONLY qualifier, which means it quietly imports every public entity from the module ๐Ÿซฃ.
  • -Wunused-dummy-argument: warns about unused dummy arguments ๐Ÿงน. This is implied by -Wall.
  • -Wunused-parameter: this one is a plot twist ๐ŸŒ€. Unlike gcc, gfortran doesn't use it for unused dummy arguments (that's -Wunused-dummy-argument). Instead it warns about unused PARAMETER values. It's implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons: by default gfortran warns whenever variables inside a COMMON block get padded for proper alignment ๐Ÿ“. You can silence it with -Wno-align-commons, and see also -falign-commons.
  • -Wfunction-elimination: warns if calls to impure functions get eliminated by the optimizations turned on by -ffrontend-optimize ๐Ÿซ . This is implied by -Wextra.
  • -Wrealloc-lhs: warns when the compiler might insert allocation or reallocation code for an allocatable array of intrinsic type in an intrinsic assignment ๐Ÿ”. In hot loops, the Fortran 2003 reallocation feature can hurt performance ๐Ÿฅฒ. If the array is already allocated with the correct shape, consider a whole-array array-spec like (:,:,:) on the left-hand side so the reallocation check gets skipped โœจ. Heads up, the warning can show up even when the compiler would optimize the check away anyway, like when the right-hand side has the same variable multiplied by a scalar. See also -frealloc-lhs.
  • -Wrealloc-lhs-all: warns whenever the compiler inserts allocation or reallocation code for any allocatable variable, and that includes scalars and derived types ๐Ÿงฉ.
  • -Wcompare-reals: warns when you compare real or complex values for equality or inequality ๐Ÿ™ˆ. This is implied by -Wextra.
  • -Wtarget-lifetime: warns if the pointer in a pointer assignment might outlive its target โณ. This is implied by -Wall.
  • -Wzerotrip: warns if a DO loop is known at compile time to run zero times ๐Ÿ”„. This is implied by -Wall.
  • -Wdo-subscript: this one catches sneaky out-of-bounds vibes ๐Ÿ‘€. It warns if an array subscript inside a DO loop could go out of bounds, even when the compiler can't prove the statement actually runs. Something like this:
real a(3)
do i=1,4
  if (condition(i)) then
    a(i) = 1.2
  end if
end do

This is implied by -Wextra.

  • -Werror: turns every warning into an error ๐Ÿšซ.

Some of these have no effect when compiling programs written in Fortran ๐Ÿคทโ€โ™€๏ธ.

๐Ÿ› Options for debugging your program or GNU Fortran ๐Ÿ”

GNU Fortran has a bunch of special options for debugging either your program or the GNU Fortran compiler itself ๐Ÿงš. Hot tip: most of these are mainly for compiler nerds ๐Ÿค“.

  • -fdump-fortran-original: outputs the internal parse tree right after the source program gets translated into internal representation ๐ŸŒณ. It's mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and it may also throw internal compiler errors for features that were only added recently ๐Ÿ˜ญ.
  • -fdump-fortran-optimized: outputs the parse tree after front-end optimization ๐Ÿช„. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and it may also cause internal compiler errors for brand-new features ๐Ÿ’€.
  • -fdump-parse-tree: outputs the internal parse tree right after the source program gets translated into internal representation ๐ŸŒฒ. It's mostly for debugging the compiler itself, the output can change between releases, and it may also cause internal compiler errors for recently added features.
This option is deprecated! Use -fdump-fortran-original instead ๐Ÿšจ.
  • -fdump-fortran-global: outputs a list of the global identifiers after translating into middle-end representation ๐Ÿ“œ. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and it may also trigger internal compiler errors for features that were only recently added ๐Ÿซจ.
  • -ffpe-trap=list: lets you turn on floating-point exception traps ๐Ÿ’ฅ. On most systems, when a floating-point exception happens and its trap is enabled, a SIGFPE signal gets sent and the program is aborted, leaving behind a core file that's super useful for debugging ๐Ÿ”ฅ. The list is a comma-separated list (it can be empty) of these exceptions:
    • invalid: an invalid floating-point operation, like SQRT(-1.0) ๐Ÿ™…โ€โ™€๏ธ
    • zero: division by zero โž—
    • overflow: overflow in a floating-point operation ๐Ÿ“ˆ
    • underflow: underflow in a floating-point operation ๐Ÿ“‰
    • inexact: loss of precision during an operation ๐ŸŒซ๏ธ
    • denormal: an operation performed on a denormal value ๐Ÿค

The first five exceptions match the five IEEE 754 exceptions. The last one, denormal, isn't part of IEEE 754, but it's available on some common architectures like x86 ๐Ÿ–ฅ๏ธ.

The first three exceptions (invalid, zero, and overflow) often point to serious errors. Unless your program has a plan for handling them, turning on traps for those three is probably a good idea ๐Ÿซถ.

If you use the option more than once on the command line, the lists get joined together. So '"ffpe-trap="list1 "ffpe-trap="list2' is the same as "ffpe-trap="list1,list2 ๐Ÿงฉ.

Once you enable an exception, you can't disable it. There's no negative form, so choose wisely ๐Ÿ”’.

Lots of floating-point operations lose precision because of rounding, so ffpe-trap=inexact is probably not very interesting in practice ๐Ÿฅฑ.

By default, no exception traps are enabled ๐Ÿ•Š๏ธ.

  • -ffpe-summary=list: lets you pick which floating-point exceptions get their flag status printed to ERROR_UNIT whenever STOP or ERROR STOP gets invoked ๐Ÿ“‹. The list can be none, all, or a comma-separated list of invalid, zero, overflow, underflow, inexact, and denormal. (Check -ffpe-trap for what each of those exceptions means ๐Ÿ‘€.)

If you use the option more than once on the command line, only the last one counts ๐Ÿซต.

By default, a summary for all exceptions except inexact gets shown ๐Ÿ“.

  • -fno-backtrace: when a serious runtime error pops up or a deadly signal gets emitted (segmentation fault, illegal instruction, bus error, floating-point exception, and any other POSIX signal whose action is core), the Fortran runtime library tries to print a backtrace of the error ๐Ÿชต. -fno-backtrace turns that backtrace generation off ๐Ÿ™…โ€โ™€๏ธ. This option only matters when compiling the Fortran main program.

These options control how GNU Fortran looks for files pulled in by the INCLUDE directive, and where it looks for previously compiled modules ๐Ÿงญ.

They also affect the search paths cpp uses when it preprocesses Fortran source ๐ŸŒธ.

  • -Idir: affects how the INCLUDE directive gets interpreted (and the #include directive of the cpp preprocessor too) ๐Ÿ—บ๏ธ.

Also, the general behavior of -I and INCLUDE is pretty much the same as -I with #include in the cpp preprocessor, when it comes to looking for header.gcc files and other stuff like that ๐Ÿ’….

This path is also used to search for .mod files when a USE statement needs previously compiled modules ๐Ÿ“ฆ.

  • -Jdir: specifies where to put .mod files for compiled modules ๐Ÿ—ƒ๏ธ. It also gets added to the list of directories that a USE statement searches.

The default is the current directory ๐Ÿ“.

  • -fintrinsic-modules-path dir: specifies where the pre-compiled intrinsic modules live, if they aren't in the default location the compiler expects ๐Ÿ .

๐Ÿ”— Influencing the linking step โ›“๏ธ

These options kick in when the compiler links object files into an executable output file ๐Ÿงฉ. They're meaningless if the compiler isn't doing a link step ๐Ÿคทโ€โ™€๏ธ.

  • -static-libgfortran: on systems that provide libgfortran as both a shared and a static library, this forces the static version ๐Ÿชจ. If no shared version of libgfortran was built when the compiler was configured, this option does nothing ๐Ÿ˜ถ.

๐Ÿƒโ€โ™€๏ธ Influencing runtime behavior ๐ŸŒˆ

These options change how programs compiled with GNU Fortran behave at runtime ๐ŸŽฌ.

  • -fconvert=conversion: sets how data is represented in unformatted files ๐Ÿงƒ. Valid values for conversion are native (the default), swap (swaps between big- and little-endian), big-endian (uses big-endian representation for unformatted files), and little-endian (uses little-endian representation for unformatted files) ๐Ÿ”€.

This option only has an effect when it's used in the main program ๐Ÿ‘‘. The CONVERT specifier and the GFORTRAN_CONVERT_UNIT environment variable both override the default set by -fconvert ๐Ÿซก.

  • -frecord-marker=length: sets the length of record markers for unformatted files ๐Ÿ“. Valid values for length are 4 and 8, and the default is 4 ๐Ÿ’–. This is different from earlier gfortran versions, which used a default record marker length of 8 on most systems ๐Ÿ•ฐ๏ธ. If you want to read or write files that work with earlier versions of gfortran, use -frecord-marker=8 ๐Ÿ”.
  • -fmax-subrecord-length=length: sets the maximum length for a subrecord ๐Ÿ“. The maximum permitted value for length is 2147483639, which is also the default ๐Ÿ’ฏ. It's only really useful for the gfortran testsuite ๐Ÿงช.
  • -fsign-zero โ€” okay bestie, when this is on ๐Ÿ’–, floating point zeros that have the sign bit set get written out as negative numbers in formatted output, and the SIGN intrinsic treats them as negative too ๐Ÿฆ‹. -fno-sign-zero is the chill version ๐ŸŒธ. It doesn't print the negative sign of zero values (or values rounded to zero for I/O), and it treats zero as positive in the SIGN intrinsic, which keeps things compatible with Fortran 77 ๐ŸŽ€. The default is -fsign-zero โœจ.

โœจ Options for code generation conventions โœจ

okay so these are machine-independent options that control the interface conventions used in code generation ๐Ÿงš. They're giving main character energy in the build process ๐Ÿ’….

Most of them come in positive and negative forms. The negative form of -ffoo is -fno-foo. Below, only the non-default form is listed, so you figure out the other one by either removing no- or adding it ๐ŸŒˆ.

  • -fno-automatic โ€” treat each program unit (except those marked RECURSIVE) as if the SAVE statement were specified for every local variable and array referenced in it. It doesn't affect common blocks. (Some Fortran compilers call this option -static or -save.) The default, -fautomatic, uses the stack for local variables smaller than the value given by -fmax-stack-var-size. If you want no static memory at all, use -frecursive ๐Ÿ’œ.
    • Local variables or arrays with an explicit SAVE attribute are silently ignored unless you add the -pedantic option ๐Ÿ™ˆ.
  • -ff2c โ€” generate code that's compatible with code generated by g77 and f2c ๐Ÿซถ. The g77 calling conventions (originally implemented in f2c) make functions that return default REAL actually return the C type double. Functions that return COMPLEX send their value back through an extra argument in the calling sequence, which points to where the return value gets stored. Under the default GNU calling conventions, those functions just return their results like GNU C would. Default REAL functions return the C type float, and COMPLEX functions return the GNU C type complex โœจ. This option also implies -fsecond-underscore, unless you explicitly ask for -fno-second-underscore ๐Ÿ”ฅ.
    • This does not affect the generation of code that interfaces with the libgfortran library.
Caution: It is not a good idea to mix Fortran code compiled with -ff2c with code compiled with the default -fno-f2c calling conventions ๐Ÿ’€. Calling COMPLEX or default REAL functions between program parts compiled with different calling conventions will break at execution time ๐Ÿ˜ญ.
Caution: This will break code that passes intrinsic functions of type default REAL or COMPLEX as actual arguments, because the library implementations use the -fno-f2c calling conventions ๐Ÿ™ˆ.
  • -fno-underscoring โ€” do not transform the names of entities in the Fortran source file by appending underscores to them ๐ŸŽ€.
    • With -funderscoring in effect, GNU Fortran appends one underscore to external names with no underscores. This is done to stay compatible with code produced by many UNIX Fortran compilers ๐Ÿฆ‹.
    • Using -fno-underscoring is not recommended unless you're experimenting with things like integrating GNU Fortran into existing system environments (vis-ร -vis existing libraries, tools, and so on) ๐Ÿง.

For example, with -funderscoring, and assuming that j() and max_count() are external functions while my_var and lvar are local variables, a statement like this:

I = J() + MAX_COUNT (MY_VAR, LVAR)

is implemented as something akin to:

i = j_() + max_count__(&my_var__, &lvar);

With -fno-underscoring, the same statement is implemented as:

i = j() + max_count(&my_var, &lvar);
  • Using -fno-underscoring lets you specify user-defined names directly while debugging and when interfacing GNU Fortran code with other languages ๐Ÿ”.
  • Just because the names match doesn't mean the interface GNU Fortran implements for an external name matches the interface another language implements for that same name ๐Ÿซ . Getting code from GNU Fortran to link with code from another compiler using this or any other method can be only a small part of the solution. Getting the code from both compilers to agree on things other than naming can take serious effort, and unlike naming disagreements, linkers normally can't detect disagreements in those other areas ๐Ÿ’€.
  • With -fno-underscoring, the lack of appended underscores introduces a very real possibility that a user-defined external name will conflict with a name in a system library. That could make finding unresolved-reference bugs quite difficult in some cases, since they might occur at program run time and only show up as buggy behavior then ๐Ÿ˜ญ.
  • In future versions of GNU Fortran, the hope is to improve naming and linking so that debugging always involves using the names as they appear in the source. The names as seen by the linker may still be mangled to prevent accidental linking between procedures with incompatible interfaces โœจ.
  • -fsecond-underscore โ€” by default, GNU Fortran appends an underscore to external names. With this option, GNU Fortran appends two underscores to names that contain underscores, and one underscore to external names with no underscores. It also appends two underscores to internal names with underscores, to avoid naming collisions with external names ๐Ÿ’œ.
    • This option has no effect if -fno-underscoring is in effect. It is implied by the -ff2c option ๐ŸŒธ.
    • With this option, an external name such as MAX_COUNT is implemented as a reference to the link-time external symbol max_count__, instead of max_count_. This is required for compatibility with g77 and f2c, and it's implied by using the -ff2c option ๐Ÿซถ.
  • -fcoarray=<keyword> โ€” pick how coarray support works ๐ŸŒˆ.
    • none โ€” disable coarray support. Using coarray declarations and image-control statements will produce a compile-time error. (Default) ๐Ÿšซ
    • single โ€” single-image mode, i.e. num_images() is always one ๐Ÿงš.
    • lib โ€” library-based coarray parallelization. A suitable GNU Fortran coarray library needs to be linked ๐Ÿ”ฅ.
  • -fcheck=<keyword> โ€” enable the generation of run-time checks ๐Ÿ›ก๏ธ. The argument should be a comma-delimited list of the keywords below. Prefixing a check with no- disables it if it was activated by a previous specification ๐Ÿ’….
    • all โ€” enable all run-time tests of -fcheck โœจ.
    • array-temps โ€” warns at run time when, to pass an actual argument, a temporary array had to be generated. The info from this warning is sometimes useful for optimization, so you can avoid those temporaries ๐Ÿฆ‹.
      • Note: The warning is only printed once per location.
    • bounds โ€” enable generation of run-time checks for array subscripts and against the declared minimum and maximum values. It also checks array indices for assumed and deferred shape arrays against the actual allocated bounds, and it makes sure all string lengths are equal for character array constructors without an explicit typespec ๐ŸŽ€.
      • Some checks require that -fcheck=bounds is set for the compilation of the main program.
      • Note: In the future this may also include other forms of checking, e.g., checking substring references.
    • do โ€” enable generation of run-time checks for invalid modification of loop iteration variables ๐Ÿ”.
    • mem โ€” enable generation of run-time checks for memory allocation ๐Ÿง .
      • Note: This option does not affect explicit allocations using the ALLOCATE statement, which will always be checked.
    • pointer โ€” enable generation of run-time checks for pointers and allocatables ๐Ÿ‘‘.
    • recursion โ€” enable generation of run-time checks for recursively called subroutines and functions that aren't marked as recursive. See also -frecursive ๐ŸŒ€.
      • Note: This check does not work for OpenMP programs, and it's disabled if used together with -frecursive and -fopenmp.

Example: Assuming you have a file foo.f90, the command:

gfortran -fcheck=all,no-array-temps foo.f90

will compile the file with all checks enabled as specified above, except warnings for generated array temporaries ๐Ÿ’–.

  • -fbounds-check โ€” deprecated alias for -fcheck=bounds ๐Ÿฅ€.
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” okay, this one's a lot ๐Ÿ˜ญ. Some C interfaces to Fortran code violate the gfortran ABI by omitting the hidden character length arguments as described in. This can lead to crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿ’€.
    • To provide a workaround for existing binary packages, this option disables tail call optimization for gfortran procedures with character arguments. With -ftail-call-workaround=2, tail call optimization is disabled in all gfortran procedures with character arguments. With -ftail-call-workaround=1 or the equivalent plain -ftail-call-workaround, it's disabled only in gfortran procedures with character arguments that call implicitly prototyped procedures ๐Ÿงฉ.
    • Using this option can lead to problems, including crashes due to insufficient stack space ๐Ÿฅบ.
    • It is very strongly recommended to fix the code in question. The -fc-prototypes-external option can be used to generate prototypes that conform to gfortran's ABI, for inclusion in the source code ๐Ÿ› ๏ธ.
    • Support for this option will likely be withdrawn in a future release of gfortran ๐Ÿ‘‹.
    • The negative form, -fno-tail-call-workaround or the equivalent -ftail-call-workaround=0, can be used to disable this option ๐Ÿ™….
    • The default is currently -ftail-call-workaround. This will change in future releases ๐Ÿ”ฎ.
Warning: Relying on this option is risky bestie. Fix the source instead of leaning on the workaround ๐Ÿšจ.
  • -fcheck-array-temporaries โ€” deprecated alias for -fcheck=array-temps ๐Ÿฅ€.
  • -fmax-array-constructor=n โ€” this option can be used to increase the upper limit permitted in array constructors ๐Ÿ“ˆ. The code below requires this option to expand the array at compile time ๐Ÿงฎ.

Here's what that example looks like in action ๐Ÿงš

program test
implicit none
integer j
integer, parameter :: n = 100000
integer, parameter :: i(n) = (/ (2*j, j = 1, n) /)
print '(10(I0,1X))', i
end program test
โš ๏ธ Caution, bestie: This option can lead to long compile times and excessively large object files ๐Ÿ˜ญ

The default value for n is 65535 ๐Ÿ’œ

-fmax-stack-var-size=n โœจ

This one sets the size in bytes of the biggest array that's allowed to live on the stack. If an array goes over that size, it gets static memory instead (except inside procedures marked RECURSIVE). If you need recursive procedures that don't have the RECURSIVE attribute, or you're writing parallel programs, use -frecursive to allow it. And if you want the stack to never be used at all, -fno-automatic is your girl ๐Ÿ™ˆ

This option currently only touches local arrays with constant bounds, and it might not apply to every character variable. Future versions of GNU Fortran may improve this, so keep an eye out ๐Ÿ‘€

The default value for n is 32768 ๐ŸŒธ

-fstack-arrays ๐Ÿฆ‹

This option tells the Fortran compiler to put all arrays of unknown size and array temporaries onto stack memory. Lowkey a big deal: if your program uses very large local arrays, you might have to raise your runtime stack memory limits on some operating systems ๐Ÿ˜ญ

This flag is turned on by default at the -Ofast optimization level, unless -fmax-stack-var-size is specified ๐Ÿ’…

-fpack-derived ๐Ÿ’–

This tells GNU Fortran to pack derived type members as closely together as possible. Code compiled with this option is likely to be incompatible with code compiled without it, and it may run slower ๐Ÿ’€

-frepack-arrays ๐Ÿซถ

Sometimes GNU Fortran passes assumed shape array sections through a descriptor that describes a noncontiguous area of memory. This option adds code to the function prologue that repacks the data into a contiguous block at runtime โœจ

This should make accesses to the array faster. However, it can add significant overhead to the function call, especially when the passed data is noncontiguous ๐Ÿซ 

-fshort-enums ๐ŸŽ€

This one's for interoperability with C code that was compiled with the -fshort-enums option. It makes GNU Fortran choose the smallest INTEGER kind that a given enumerator set fits in, and then gives all of its enumerators that kind ๐Ÿงš

-fexternal-blas ๐Ÿ”ฅ

This makes gfortran generate calls to BLAS functions for some matrix operations, like MATMUL, instead of using its own algorithms. That only happens if the matrices involved are larger than a given limit (see -fblas-matmul-limit) ๐Ÿ‘‘

This can be super profitable if an optimized vendor BLAS library is available. The BLAS library has to be specified at link time, so don't forget it ๐Ÿซถ

-fblas-matmul-limit=n ๐ŸŒˆ

Only significant when -fexternal-blas is in effect. Matrix multiplication of matrices with a size larger than (or equal to) n gets handled by calls to BLAS functions, while smaller ones are handled by gfortran's internal algorithms ๐Ÿ’…

If the matrices aren't square, the size comparison uses the geometric mean of the dimensions of the argument and result matrices ๐Ÿค“

The default value for n is 30 โœจ

-finline-matmul-limit=n ๐Ÿ’œ

When front-end optimization is active, some calls to the MATMUL intrinsic function get inlined. This can increase code size if the size of the matrix can't be determined at compile time, because code for both cases gets generated ๐Ÿ™ˆ

Setting -finline-matmul-limit=0 disables inlining in all cases. Setting it to a value of n produces inline code for matrices with a size up to n. If the matrices aren't square, the size comparison uses the geometric mean of the dimensions of the argument and result matrices ๐ŸŒธ

The default value for n is 30. The -fblas-matmul-limit option can be used to change this value ๐Ÿฆ‹

-frecursive ๐Ÿงš

This allows indirect recursion by forcing all local arrays to be allocated on the stack. Heads up: this flag cannot be used together with -fmax-stack-var-size= or -fno-automatic ๐Ÿšซ

-finit-local-zero, -finit-derived, -finit-integer=n, -finit-real=<zero|inf|-inf|nan|snan>, -finit-logical=<true|false>, -finit-character=n ๐Ÿ“

-finit-local-zero tells the compiler to initialize local INTEGER, REAL, and COMPLEX variables to zero, LOGICAL variables to false, and CHARACTER variables to a string of null bytes โœจ

The finer-grained options are -finit-integer=n, -finit-real=<zero|inf|-inf|nan|snan> (which also initializes the real and imaginary parts of local COMPLEX variables), -finit-logical=<true|false>, and -finit-character=n (where n is an ASCII character value) ๐Ÿ’–

With -finit-derived, components of derived type variables get initialized according to these flags. Components whose type isn't covered by an explicit -finit-* flag get treated the same as -finit-local-zero ๐Ÿซถ

These options do not initialize:

  • objects with the POINTER attribute ๐Ÿ™…
  • allocatable arrays ๐Ÿ™…
  • variables that appear in an EQUIVALENCE statement ๐Ÿ™…

(These limitations may be removed in future releases, so no promises yet ๐Ÿฅบ)

Note that -finit-real=nan initializes REAL and COMPLEX variables with a quiet NaN. For a signalling NaN, use -finit-real=snan. Be careful though: compile-time optimizations may convert them into a quiet NaN, and trapping needs to be enabled (e.g. via -ffpe-trap) ๐Ÿ”ฅ

The -finit-integer option parses the value into an integer of type INTEGER(kind=C_LONG) on the host. That value is then assigned to the integer variables in the Fortran code, which might wrap around if the value is too large for the kind ๐Ÿ’€

Finally, enabling any of the -finit-* options will silence warnings that would've been emitted by -Wuninitialized for the affected local variables ๐Ÿคซ

-falign-commons ๐ŸŒˆ

By default, gfortran enforces proper alignment of all variables in a COMMON block by padding them as needed. On certain platforms this is mandatory, and on others it boosts performance ๐Ÿš€

If a COMMON block isn't declared with consistent data types everywhere, this padding can cause trouble. In that case, -fno-align-commons disables the automatic alignment ๐Ÿ™…. The same form of this option should be used for all files that share a COMMON block ๐Ÿซถ

To avoid potential alignment issues in COMMON blocks, it's recommended to order objects from largest to smallest ๐Ÿ“

-fno-protect-parens ๐Ÿฆ‹

By default, the parentheses in expressions are honored at all optimization levels, so the compiler doesn't do any re-association ๐Ÿ™ˆ

Using -fno-protect-parens lets the compiler reorder REAL and COMPLEX expressions to produce faster code. For the re-association optimization, -fno-signed-zeros and -fno-trapping-math need to be in effect too ๐Ÿง 

The parentheses protection is enabled by default, unless -Ofast is given ๐Ÿ’…

-frealloc-lhs ๐Ÿ’œ

An allocatable left-hand side of an intrinsic assignment gets automatically (re)allocated if it's either unallocated or has a different shape. It's enabled by default except when -std=f95 is given ๐ŸŒธ

See also -Wrealloc-lhs ๐Ÿ‘€

-faggressive-function-elimination ๐Ÿ”ฅ

Functions with identical argument lists get eliminated within statements, regardless of whether they're marked PURE or not ๐Ÿ˜ค

For example, in:

a = f(b,c) + f(b,c)

there will only be a single call to f ๐Ÿ‘‘

This option only works if -ffrontend-optimize is in effect ๐Ÿซถ

-ffrontend-optimize ๐Ÿงš

This option performs front-end optimization, based on manipulating parts of the Fortran parse tree. It's enabled by default by any -O option except -O0 and -Og โœจ

The optimizations enabled by this option include:

  • inlining calls to MATMUL ๐Ÿ’–
  • elimination of identical function calls within expressions ๐Ÿ’…
  • removing unnecessary calls to TRIM in comparisons and assignments ๐ŸŒธ
  • replacing TRIM(a) with a(1:LEN_TRIM(a)) ๐Ÿฆ‹
  • short-circuiting of logical operators (.AND. and .OR.) ๐Ÿซถ

It can be deselected by specifying -fno-frontend-optimize ๐Ÿ™…

-ffrontend-loop-interchange ๐ŸŒˆ

This attempts to interchange loops in the Fortran front end where it's profitable. It's enabled by default by any -O option โœจ

At the moment, this option only affects FORALL and DO CONCURRENT statements with several forall triplets ๐Ÿค“


โœจ ENVIRONMENT โœจ

The gfortran compiler currently doesn't use any environment variables to control its operation, beyond the ones that affect the operation of gcc. So no secret switches here, bestie ๐Ÿ’…

๐Ÿ› BUGS ๐Ÿ›

For instructions on reporting bugs, see <file:///usr/share/doc/gcc-9/README.Bugs> ๐Ÿ™ˆ

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

gpl(7), gfdl(7), fsf-funding(7), cpp(1), gcov(1), gcc(1), as(1), ld(1), gdb(1), dbx(1), and the Info entries for gcc, cpp, gfortran, as, ld, binutils, and gdb ๐Ÿ“š

๐Ÿ‘ฉโ€๐Ÿ’ป AUTHOR ๐Ÿ‘ฉโ€๐Ÿ’ป

See the Info entry for gfortran for contributors to GCC and GNU Fortran ๐Ÿ’œ

Copyright (c) 2004-2019 Free Software Foundation, Inc. ๐ŸŒˆ

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with the Invariant Sections being "Funding Free Software", the Front-Cover Texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the gfdl(7) man page ๐Ÿ“–

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.

Okay bestie, that's the whole page ๐Ÿซถ It came from gcc-9 and was last dated 2022-05-27, so you're officially a GFORTRAN(1) expert now ๐Ÿ‘‘๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from