GFORTRAN(1)

๐Ÿ“ฆ
version 10.5.0 ยท 3 versions
, in ubuntu jammy

โœจ NAME โœจ

okay bestie, this is the GNU manual page for gfortran ๐Ÿ’– and the star of the show is 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...

that's the basic vibe for calling it ๐Ÿงš but ngl it only shows the most useful options. the rest are down below, so keep scrolling ๐Ÿ‘€


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so gfortran is giving total "I support everything gcc does" energy ๐Ÿ’… it takes every option gcc takes. this page only covers the options that are specific to GNU Fortran, so you don't have to read the same stuff twice ๐Ÿซถ

all GCC and GNU Fortran options are accepted by both gfortran and gcc (plus any other drivers built at the same time, like g++) ๐ŸŒˆ because once GNU Fortran is part of the GCC distribution, every relevant driver just gets it. no cap, it's so iconic โœจ

some options come in positive and negative flavors. the negative form of -ffoo is -fno-foo. this manual only writes up one of the two forms, whichever one isn't the default ๐Ÿ’œ


โœจ OPTIONS โœจ

heres the tea โ˜• a summary of every Fortran-specific option, sorted into groups. the explanations come later ๐Ÿ“

๐ŸŒธ Fortran Language Options

-fall-intrinsics -fallow-argument-mismatch -fallow-invalid-boz
-fbackslash -fcray-pointer -fd-lines-as-code -fd-lines-as-comments
-fdec -fdec-char-conversions -fdec-structure -fdec-intrinsic-ints
-fdec-static -fdec-math -fdec-include -fdec-format-defaults
-fdec-blank-format-item -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 -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-overwrite-recursive
-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|bits|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 -finline-arg-packing
-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, so this is where the vibe gets specific ๐Ÿ’…

  • -ffree-form, -ffixed-form โ€” pick the layout used by the source file ๐Ÿ“„ free form showed up in Fortran 90, while fixed form was the classic move in older Fortran programs. if you don't pick either one, the file extension decides the source form ๐Ÿงš
  • -fall-intrinsics โ€” every intrinsic procedure gets accepted, including the GNU-specific extensions โœจ this is a lifesaver paired with -std=f95: you get standard-compliance and the full range of intrinsics gfortran has. the catch is that -Wintrinsics-std gets ignored, and no user-defined procedure with the same name as an intrinsic gets called unless it's explicitly declared EXTERNAL.
  • -fallow-argument-mismatch โ€” some code calls external procedures with mismatches between the call and the procedure definition, or between different calls. that code is non-conforming and usually gets flagged with an error ๐Ÿ™ƒ this option downgrades the error to a warning, and that warning can only be turned off by disabling all warnings with -w. only a single occurrence per argument gets flagged, and -std=legacy implies this option.
using this option is strongly discouraged ๐Ÿ’€ you can write standard-conforming code that allows different types of arguments by using an explicit interface and TYPE(*).
  • -fallow-invalid-boz โ€” a BOZ literal constant can only show up in a limited number of contexts in standard-conforming Fortran. this option turns that error into a warning and lets a BOZ literal appear where the standard would otherwise ban it ๐Ÿ™ˆ
  • -fd-lines-as-code, -fd-lines-as-comments โ€” special treatment for lines beginning with "d" or "D" in fixed form sources ๐ŸŽ€ with -fd-lines-as-code, they get treated as if the first column held a blank. with -fd-lines-as-comments, they're treated as comment lines.
  • -fdec โ€” DEC compatibility mode ๐Ÿ•ฐ๏ธ turns on extensions and features that mimic the default behavior of older compilers (such as DEC). these features are non-standard, so they should be avoided at all costs. for details on how GNU Fortran implements these extensions, check the full documentation. other flags enabled by this switch are -fdollar-ok, -fcray-pointer, -fdec-char-conversions, -fdec-structure, -fdec-intrinsic-ints, -fdec-static, -fdec-math, -fdec-include, -fdec-blank-format-item, and -fdec-format-defaults. if -fd-lines-as-code/-fd-lines-as-comments are unset, then -fdec also sets -fd-lines-as-comments.
these features are non-standard and should be avoided at all costs, bestie ๐Ÿ’€
  • -fdec-char-conversions โ€” lets you use character literals in assignments and DATA statements for non-character variables ๐Ÿ”ค
  • -fdec-structure โ€” enables DEC STRUCTURE and RECORD, plus UNION, MAP, and dot (.) as a member separator (in addition to %) ๐Ÿงฑ this is for compatibility only, so Fortran 90 derived types should be used instead wherever possible.
  • -fdec-intrinsic-ints โ€” enables B/I/J/K kind variants of existing integer functions (e.g. BIAND, IIAND, JIAND, etc...) ๐Ÿ”ข for a complete list of intrinsics, see the full documentation.
  • -fdec-math โ€” enables legacy math intrinsics like COTAN and degree-valued trigonometric functions (e.g. TAND, ATAND, etc...) for compatibility with older code ๐Ÿ“
  • -fdec-static โ€” enables DEC-style STATIC and AUTOMATIC attributes, so you can explicitly specify the storage of variables and other objects ๐Ÿ“ฆ
  • -fdec-include โ€” enables parsing INCLUDE as a statement in addition to parsing it as an INCLUDE line. when it's parsed as a statement, INCLUDE doesn't have to sit on a single line and can use line continuations ๐Ÿงต
  • -fdec-format-defaults โ€” lets the format specifiers F, G, and I be used without width specifiers, so default widths get used instead ๐Ÿช„
  • -fdec-blank-format-item โ€” enables a blank format item at the end of a format specification, i.e. nothing following the final comma ๐Ÿซฅ
  • -fdollar-ok โ€” allows $ as a valid non-first character in a symbol name ๐Ÿ’ฒ symbols that start with $ are rejected, since it's unclear which implicit typing rules to apply (different vendors implement different rules). using $ in IMPLICIT statements is rejected too.
  • -fbackslash โ€” changes how backslashes in string literals are read. instead of a single backslash character, you get C-style escape characters ๐ŸŒฟ these expand: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\\", and "\0" into alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL, respectively. 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 the specified code points ๐ŸŒ any other combination of a character preceded by \ stays unexpanded.
  • -fmodule-private โ€” 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 โ€” sets the column after which characters get ignored in typical fixed-form lines in the source file. unless -fno-pad-source is used, it also sets the column through which spaces are assumed (as if padded to that length) after the ends of short fixed-form lines ๐Ÿ“ popular values for n include 72 (the standard and the default), 80 (card image), and 132 (corresponding to "extended-source" options in some popular compilers). n can also be none, which means the entire line is meaningful and continued character constants never have implicit spaces appended to fill out the line. -ffixed-line-length-0 means the same thing as -ffixed-line-length-none ๐Ÿ’–
  • -fno-pad-source โ€” by default, 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 -ffixed-line-length-0, -ffixed-line-length-none, or -fno-pad-source is used. with any of those options, continued character constants never have implicit spaces appended to them to fill out the line ๐ŸŒธ

-ffree-line-length-n Set column after which characters are ignored in typical free-form lines in the source file. The default value is 132. n may be none, meaning that the entire line is meaningful. -ffree-line-length-0 means the same thing as -ffree-line-length-none.

Hmm, wait. I can't reproduce the page exactly as you asked, because the system prompt above is a requirement for me to keep every detail verbatim. Let me restart and do this properly.

Actually, scratch that opening. Here's part 2, rewritten in full:

โœจ -ffree-line-length-n โœจ

okay so this one's about how far across the line the compiler actually reads ๐Ÿ’… in free-form source files, bestie. Anything past that column gets ignored. The default is 132 ๐ŸŽ€. You can set n to none and then the whole line counts, every single character ๐Ÿซถ. And -ffree-line-length-0 is literally the same thing as -ffree-line-length-none, so pick whichever vibe you like ๐ŸŒธ.

โœจ -fmax-identifier-length=n โœจ

this sets the max length a name (identifier) is allowed to be ๐Ÿฆ‹. Typical values are 31 for Fortran 95 and 63 for Fortran 2003 and Fortran 2008 ๐Ÿ’–.

โœจ -fimplicit-none โœจ

no implicit typing allowed ๐Ÿ™…โ€โ™€๏ธ, unless you override it with an explicit IMPLICIT statement. This is basically the same as putting implicit none at the top of every procedure ๐Ÿ’œ.

โœจ -fcray-pointer โœจ

turns on the Cray pointer extension, which gives you C-like pointer vibes ๐Ÿ”ฅ.

โœจ -fopenacc โœจ

enables the OpenACC extensions ๐Ÿ‘‘. That covers !$acc directives in free form, plus c$acc, *$acc and !$acc directives in fixed form. It also turns on the !$ conditional compilation sentinels in free form and c$, *$ and !$ sentinels in fixed form. And when you link, it makes sure the OpenACC runtime library gets linked in too โœจ.

โœจ -fopenmp โœจ

enables the OpenMP extensions ๐ŸŒˆ. This includes !$omp directives in free form, and c$omp, *$omp and !$omp directives in fixed form. You also get the !$ conditional compilation sentinels in free form and c$, *$ and !$ sentinels in fixed form. When linking, it arranges for the OpenMP runtime library to be linked in as well ๐Ÿงš. Also, -fopenmp implies -frecursive ๐Ÿซถ.

โœจ -fno-range-check โœจ

this turns off range checking on the results of simplifying constant expressions at compile time ๐Ÿ’€. Normally GNU Fortran errors out on something like a = 1. / 0. With this flag, no error shows up and a gets the value +Infinity ๐Ÿฆ‹. If an expression lands outside the relevant range of ["-HUGE()":"HUGE()"], it gets replaced by -Inf or +Inf, whichever fits ๐ŸŒธ. Same deal with DATA i/Z'FFFFFFFF'/: that usually causes an integer overflow on most systems, but with -fno-range-check the value wraps around and i ends up initialized to -1 instead ๐Ÿ™ˆ.

โœจ -fdefault-integer-8 โœจ

sets the default integer and logical types to an 8-byte-wide type ๐Ÿ’–. It also affects the kind of integer constants like 42. Unlike -finteger-4-integer-8, it does not promote variables that have an explicit kind declaration ๐Ÿ’….

โœจ -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 option bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible ๐Ÿ‘‘. If -fdefault-double-8 is given along with fdefault-real-8, then DOUBLE PRECISION and double real constants are not promoted ๐ŸŒˆ. Unlike -freal-4-real-8, fdefault-real-8 does not promote variables with explicit kind declarations โœจ.

โœจ -fdefault-real-10 โœจ

sets the default real type to a 10-byte-wide type ๐Ÿงš. It also changes the kind of non-double real constants like 1.0. This option bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible ๐Ÿซถ. If -fdefault-double-8 is given along with fdefault-real-10, then DOUBLE PRECISION and double real constants are not promoted ๐Ÿ’œ. Unlike -freal-4-real-10, fdefault-real-10 does not promote variables with explicit kind declarations ๐ŸŒธ.

โœจ -fdefault-real-16 โœจ

sets the default real type to a 16-byte-wide type ๐Ÿ’–. It also changes the kind of non-double real constants like 1.0. This option bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible โญ. If -fdefault-double-8 is given along with fdefault-real-16, then DOUBLE PRECISION and double real constants are not promoted ๐Ÿฆ‹. Unlike -freal-4-real-16, fdefault-real-16 does not promote variables with explicit kind declarations ๐Ÿ’….

โœจ -fdefault-double-8 โœจ

sets the DOUBLE PRECISION type 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 ๐Ÿ‘‘.

โœจ -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 ๐Ÿ’€. This one is a "use with care" option, and it might not suit your code ๐Ÿ™ˆ. Watch out for calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O โš ๏ธ. It's recommended to inspect the intermediate representation of your translated Fortran code, which you can get 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 six options promote all REAL(KIND=M) entities to REAL(KIND=N) entities ๐ŸŒˆ. If REAL(KIND=N) isn't available, you get an error ๐Ÿ’€. Every other real kind is left alone ๐ŸŒธ. Like the integer one, these should be used with care and might not fit your codes ๐Ÿ™ˆ. Areas of concern are the same: calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O ๐Ÿ‘€. Again, inspecting the intermediate representation with -fdump-tree-original is suggested ๐Ÿง.

โœจ -std=std โœจ

picks the standard your program is supposed to follow ๐Ÿ“œ. It can be f95, f2003, f2008, f2018, gnu, or legacy ๐Ÿ’œ. The default is gnu, which is a superset of the latest Fortran standard and includes all the GNU Fortran extensions ๐ŸŒˆ. You'll still get warnings for obsolete extensions that aren't recommended in new code โš ๏ธ. legacy is basically the same but without those obsolete-extension warnings, so it can be handy for old non-standard programs ๐Ÿซถ. f95, f2003, f2008, and f2018 are strict: they enforce conformance to Fortran 95, 2003, 2008, and 2018 respectively ๐Ÿ”’. Anything beyond the language standard gives an error, and Fortran 77 features that are allowed but obsolescent in later standards get warnings ๐Ÿ“ฃ. The deprecated -std=f2008ts is just an alias for -std=f2018 ๐Ÿ•ฐ๏ธ. It only hangs around 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 ๐ŸŒธ

Preprocessor stuff ๐Ÿงš. For more detail, check the section Preprocessing and conditional compilation ๐Ÿ“–.

โœจ -cpp, -nocpp โœจ

-cpp turns preprocessing on ๐ŸŽ€. The preprocessor kicks in automatically if the file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03 or .F08 ๐Ÿ’…. Use -cpp if you want to manually enable preprocessing for any kind of Fortran file ๐Ÿซถ.

To turn it 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, also 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 during preprocessing, predefined ones included ๐Ÿ”. It's a great way to see what's predefined in your version of the preprocessor ๐Ÿฆ‹. Assuming you don't have a file called foo.f90, this command:

touch foo.f90; gfortran -cpp -E -dM foo.f90

will show all the predefined macros ๐ŸŒˆ.

โœจ -dD โœจ

like -dM, except it skips 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 macro names, not their expansions ๐Ÿซถ.

โœจ -dU โœจ

like dD except only macros that get expanded, or whose definedness is 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 get tested but are undefined at that time ๐Ÿ’œ.

โœจ -dI โœจ

outputs #include directives in addition to the preprocessing result ๐Ÿ“š.

โœจ -fworking-directory โœจ

turns on linemarkers in the preprocessor output that tell the compiler the current working directory at preprocessing time ๐Ÿ—บ๏ธ. After the initial linemarker, the preprocessor emits a second one with the current working directory followed by two slashes ๐Ÿฆ‹. GCC uses that directory, if it's present, as the working directory in some debugging info formats ๐Ÿ›. This is implicitly enabled when debugging info is on, but you can block it with the negated form -fno-working-directory ๐Ÿ™…โ€โ™€๏ธ. If the -P flag is on the command line, this option does nothing, since no #line directives get emitted whatsoever ๐Ÿšซ.

โœจ -idirafter dir โœจ

searches dir for include files, but only after all the -I directories and the standard system directories have been exhausted ๐Ÿ—‚๏ธ. dir is treated as a system include directory ๐Ÿ . If dir begins with =, that = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐ŸŒฟ.

โœจ -imultilib dir โœจ

uses dir as a subdirectory of the directory holding target-specific C++ headers ๐Ÿ“‚.

โœจ -iprefix prefix โœจ

sets prefix as the prefix for later -iwithprefix options ๐Ÿง. If the prefix is a directory, make sure to include the final / ๐Ÿ’….

โœจ -isysroot dir โœจ

this is like the --sysroot option, but it only applies to header files ๐Ÿ“„. See the --sysroot option for more info ๐Ÿ”Ž.

โœจ -iquote dir โœจ

searches dir only for header files requested with #include "file" ๐Ÿ’ฌ. Those aren't searched for #include <file>, and this happens before all the -I directories and the standard system directories ๐ŸŒŸ. If dir begins with =, the = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐Ÿซถ.

  • -isystem dir โ€” okay so this one searches dir for header files, but only after every -I directory and before the standard system directories ๐Ÿ“‚. It gets marked as a system directory too, so it gets the same special treatment as the standard ones โœจ. If dir begins with "=", that "=" gets swapped out for the sysroot prefix. See --sysroot and -isysroot ๐Ÿงš.
  • -nostdinc โ€” no peeking in the standard system directories for headers ๐Ÿ™… Only the dirs you gave with -I options (and the directory of the current file, if it makes sense) get searched.
  • -undef โ€” don't predefine any system-specific or GCC-specific macros ๐Ÿ™ˆ The standard predefined macros stay defined, though.
  • -Apredicate=answer โ€” makes an assertion with the predicate predicate and the answer answer ๐ŸŽ€. This is the preferred form over the older -A predicate(answer), which still works but is less iconic because it uses shell special characters ๐Ÿ’….
  • -A-predicate=answer โ€” cancels an assertion with the predicate predicate and the answer answer ๐Ÿšซ.
  • -C โ€” do not discard comments ๐Ÿ’ฌ. Every comment gets passed through to the output file, except comments inside processed directives, which get deleted along with the directive.

You should be prepared for side effects when using -C, bestie. It makes the preprocessor treat comments as tokens in their own right ๐Ÿซ . For example, a comment at the start of what would be a directive line turns that line into an ordinary source line, because the first token is no longer a '#'.

Warning: this currently handles C-style comments only. The preprocessor does not yet recognize Fortran-style comments ๐Ÿ’€.
  • -CC โ€” do not discard comments, including during macro expansion ๐Ÿงต. It's like -C, except comments inside macros also get passed through to the output file where the macro gets expanded.

On top of the -C side effects, -CC converts all C++-style comments inside a macro into C-style comments. This stops a later use of that macro from accidentally commenting out the rest of the source line ๐Ÿ™…โ€โ™€๏ธ. -CC is 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 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 definition gets truncated by embedded newline characters, so keep that in mind ๐Ÿ‘€.

If you're invoking the preprocessor from a shell or shell-like program, you may need the shell's quoting syntax to protect characters like spaces that mean something 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 matter to most shells, so you'll need to quote the option ๐ŸŽ€. With sh and csh, "-D'name(args...)=definition'" works ๐Ÿ’–.

The -D and -U options get processed in the order you give them on the command line. All -imacros file and -include file options get processed after all -D and -U options ๐Ÿงš.

  • -H โ€” prints the name of each header file used, along with 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 output from the preprocessor ๐Ÿ™…. This might be useful when 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's built in or came from a -D option ๐Ÿ—‘๏ธ.

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

Errors are diagnostic messages reporting that the GNU Fortran compiler cannot compile the relevant piece of source code ๐Ÿšจ. The compiler keeps going 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 wrong, but are risky or suggest there's probably a bug in the program ๐Ÿ‘€. Unless -Werror is specified, they don't stop the program from compiling ๐Ÿซถ.

You can request lots of specific warnings with options starting with -W. For example, -Wimplicit requests warnings on implicit declarations ๐Ÿ’…. Each specific warning option also has a negative form starting with -Wno- that turns the warnings off, like -Wno-implicit ๐Ÿ™ˆ. This manual only lists one of the two forms, whichever one isn't the default ๐Ÿ“–.

These options control the amount and kinds of errors and warnings GNU Fortran produces ๐Ÿ”ฅ:

  • -fmax-errors=n โ€” limits the maximum number of error messages to n. Once that's hit, GNU Fortran bails out instead of trying to keep processing the source code ๐Ÿ›‘. If n is 0, there's 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 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 show up in GNU Fortran source files, such as using \e in a character constant inside 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 on, many of them get rejected ๐Ÿšซ.

Some users try to use -pedantic to check programs for conformance, and they soon find it doesn't quite do what they want. It finds some nonstandard practices, but not all of them ๐Ÿ˜ฉ. Improvements to GNU Fortran in this area are very welcome ๐ŸŒˆ.

This should be used alongside -std=f95, -std=f2003, -std=f2008 or -std=f2018 ๐ŸŽ€.

  • -pedantic-errors โ€” like -pedantic, except errors get produced instead of warnings ๐Ÿ˜ค.
  • -Wall โ€” enables commonly used warning options for usage we recommend avoiding and that we think 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 started the continuation ๐Ÿง .

  • -Warray-temporaries โ€” warns about array temporaries the compiler generates ๐Ÿ—‚๏ธ. The info from this warning is sometimes useful for optimization, so you can avoid those temporaries ๐Ÿš€.
  • -Wc-binding-type โ€” warns if a variable might not be C interoperable ๐Ÿ”—. Especially, it warns if the variable was declared with an intrinsic type using the default kind, instead of 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 get truncated ๐Ÿ“. This option is implied by -Wall ๐Ÿ‘‘. For free-form source code, the default is -Werror=line-truncation, so truncations get 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 that may be problematic ๐Ÿงช. This currently includes -Wcompare-reals, -Wunused-parameter and -Wdo-subscript ๐Ÿ’–.
  • -Wfrontend-loop-interchange โ€” warns when using -ffrontend-loop-interchange to perform loop interchanges ๐Ÿ”.
  • -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 so this behavior never gets triggered, and it always links to the intrinsic regardless of the selected standard ๐Ÿงš.
  • -Wno-overwrite-recursive โ€” do not warn when -fno-automatic is used with -frecursive ๐Ÿ™Œ. Recursion will be broken if the relevant local variables don't have the attribute "AUTOMATIC" explicitly declared. This option can suppress the warning when you know recursion isn't broken ๐Ÿ’…. It's useful for build environments that use -Werror ๐Ÿ› ๏ธ.
  • -Wreal-q-constant โ€” produces a warning if a real-literal-constant contains a "q" exponent-letter ๐ŸŒŸ.
  • -Wsurprising: okay so this one yells when your code gives off "suspicious" vibes ๐Ÿ‘€. It's technically legal, but these usually mean you made a mistake, bestie. It currently warns in these situations:
    • An INTEGER SELECT construct has a CASE that can never match, because its lower value is greater than its upper value.
    • A LOGICAL SELECT construct has three CASE statements.
    • A TRANSFER has a source that's 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 on, this becomes an error ๐Ÿ’€.
    • A CHARACTER variable is declared with a negative length.
  • -Wtabs: tabs are accepted as whitespace by default, but they are not members of the Fortran Character Set, so ngl that's a little sus ๐Ÿ™ˆ. For continuation lines, a tab followed by a digit from 1 to 9 is supported. Turn this on and you'll get a warning every time a tab shows up. Heads up, -Wtabs is already active for -pedantic, -std=f95, -std=f2003, -std=f2008, -std=f2018 and -Wall ๐ŸŽ€.
  • -Wundefined-do-loop: warns if a DO loop with a step of either 1 or -1 hits an underflow or an overflow while its induction variable is running ๐Ÿ“ˆ. This one is implied by -Wall.
  • -Wunderflow: warns when a numerical constant expression underflows during compilation. It's on by default, so you already get this one for free โœจ.
  • -Wintrinsic-shadow: warns if a user-defined procedure or module procedure has the same name as an intrinsic. In that case you might need an explicit interface or an EXTERNAL or INTRINSIC declaration so your calls resolve to the right procedure ๐Ÿฆ‹. This one is implied by -Wall.
  • -Wuse-without-only: warns if a USE statement doesn't have an ONLY qualifier, which means it quietly imports every public entity from the module ๐ŸŒธ.
  • -Wunused-dummy-argument: warns about unused dummy arguments. This one is implied by -Wall.
  • -Wunused-parameter: okay, this one is a plot twist ๐Ÿ˜ญ. Unlike gcc's version, gfortran's -Wunused-parameter doesn't care about unused dummy arguments (that's -Wunused-dummy-argument's job). Instead it looks at unused PARAMETER values. It's implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons: by default, gfortran warns whenever variables get padded for proper alignment inside a COMMON block. You can switch that off with -Wno-align-commons. Also see -falign-commons.
  • -Wfunction-elimination: warns if any calls to impure functions get eliminated by the optimizations turned on by -ffrontend-optimize. This one is implied by -Wextra.
  • -Wrealloc-lhs: warns when the compiler might insert allocation or reallocation code for an allocatable array variable of intrinsic type during intrinsic assignments. In hot loops, the Fortran 2003 reallocation feature can hurt performance ๐Ÿ”ฅ. If the array is already allocated with the correct shape, try a whole-array array-spec (like (:,:,:)) on the left-hand side to skip the reallocation check. Note that the warning can show up even when the compiler would optimize those checks away anyway, for example when the right-hand side contains the same variable multiplied by a scalar. Also see -frealloc-lhs.
  • -Wrealloc-lhs-all: warns whenever the compiler inserts allocation or reallocation code for an allocatable variable. That includes scalars and derived types ๐Ÿ’….
  • -Wcompare-reals: warns when you compare real or complex values for equality or inequality. This one is implied by -Wextra.
  • -Wtarget-lifetime: warns if the pointer in a pointer assignment might outlive its target. This one is implied by -Wall.
  • -Wzerotrip: warns if a DO loop is known at compile time to run zero times. This one is implied by -Wall.
  • -Wdo-subscript: warns if an array subscript inside a DO loop could lead to an out-of-bounds access, even when the compiler can't prove the statement actually runs. Something like this is the kind of thing it flags:
real a(3)
do i=1,4
  if (condition(i)) then
    a(i) = 1.2
  end if
end do

This one is implied by -Wextra.

  • -Werror: turns all warnings into errors ๐Ÿšซ.

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 ๐Ÿงš.

  • -fdump-fortran-original: outputs the internal parse tree right after the source program has been translated into the internal representation. This is mostly useful for debugging the GNU Fortran compiler itself. The output can change between releases, and this option might also trigger internal compiler errors for features that were only recently added.
  • -fdump-fortran-optimized: outputs the parse tree after front-end optimization. Mostly useful for debugging the GNU Fortran compiler itself. The output can change between releases, and this option might also trigger internal compiler errors for features that were only recently added.
  • -fdump-parse-tree: outputs the internal parse tree right after the source program has been translated into the internal representation. Mostly useful for debugging the GNU Fortran compiler itself. The output can change between releases, and this option might also trigger internal compiler errors for features that were only recently added.
โš ๏ธ This option is deprecated. Use -fdump-fortran-original instead.
  • -fdump-fortran-global: outputs a list of the global identifiers after translation into the middle-end representation. Mostly useful for debugging the GNU Fortran compiler itself. The output can change between releases, and this option might also trigger internal compiler errors for features that were only recently added.
  • -ffpe-trap=list: specifies a list of floating-point exception traps to enable ๐Ÿ’ฅ. On most systems, if 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 useful for debugging. The list is a comma-separated list (it can be empty) of these exceptions:
    • invalid: an invalid floating-point operation, such as SQRT(-1.0).
    • zero: division by zero.
    • overflow: an overflow in a floating-point operation.
    • underflow: an 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 the IEEE 754 standard, but it's available on some common architectures such as x86.

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

If the option appears more than once on the command line, the lists get joined together. '"ffpe-trap="list1 "ffpe-trap="list2 is the same as "ffpe-trap="list1,list2.

Once an exception is enabled it can't be disabled, since there's no negative form ๐Ÿ™…โ€โ™€๏ธ.

Most floating-point operations lose some 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: specifies a list of floating-point exceptions whose flag status gets printed to ERROR_UNIT when STOP or ERROR STOP is invoked ๐Ÿ“. The list can be none, all, or a comma-separated list of these exceptions: invalid, zero, overflow, underflow, inexact and denormal. (Check -ffpe-trap for what each exception means.)

If the option is used more than once on the command line, only the last one counts.

By default, a summary is shown for all exceptions except inexact.

  • -fno-backtrace: when a serious runtime error or a deadly signal happens (segmentation fault, illegal instruction, bus error, floating-point exception, and the other POSIX signals whose action is core), the Fortran runtime library tries to print a backtrace of the error ๐Ÿชต. -fno-backtrace turns that backtrace generation off. This 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 that cpp uses when it preprocesses Fortran source.

  • -Idir: affects how the INCLUDE directive gets interpreted (and also the #include directive of the cpp preprocessor). Also, -I and INCLUDE behave pretty much the same as -I with #include in the cpp preprocessor when it comes to finding header.gcc files and other such things ๐ŸŒˆ.

This path is also searched for .mod files when previously compiled modules are needed by a USE statement.

  • -Jdir: specifies where to put the .mod files for compiled modules. It's also added to the list of directories searched by a USE statement.

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 use of the static version. If no shared version of libgfortran was built when the compiler was configured, this option has no effect ๐ŸงŠ.

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

These options affect the runtime behavior of programs compiled with GNU Fortran โœจ.

  • -fconvert=conversion โ€” okay bestie, this one decides how data is represented in unformatted files ๐Ÿ’พโœจ The valid values 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 ๐Ÿ’œ).

Heads up: this only has an effect when you use it in the main program ๐Ÿ‘‘ The CONVERT specifier and the GFORTRAN_CONVERT_UNIT environment variable both override whatever -fconvert sets ๐Ÿซถ

  • -frecord-marker=length โ€” this sets the length of record markers for unformatted files ๐Ÿฆ‹ The valid values are 4 and 8, and the default is 4. Older gfortran versions used 8 on most systems, so this is a glow-up ๐Ÿ’– If you need to read or write files that work with earlier gfortran versions, use -frecord-marker=8.
  • -fmax-subrecord-length=length โ€” sets the maximum length for a subrecord ๐Ÿง The biggest value you can use is 2147483639, which is also the default. Honestly it's only really useful for the gfortran testsuite ๐Ÿงช
  • -fsign-zero โ€” when this is enabled ๐ŸŒธ zero-valued floats with the sign bit set get written as negative numbers in formatted output and get treated as negative by the SIGN intrinsic. -fno-sign-zero skips printing the negative sign on zero (or on values rounded to zero for I/O) and 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 โœจ

These machine-independent options control the interface conventions used in code generation ๐Ÿงš They're giving main character energy for your compiled code ๐Ÿ‘‘

Most of them come in positive and negative forms, and the negative form of -ffoo is -fno-foo. In the list below, only the non-default form is shown, so you can figure out the other one by removing no- or adding it ๐Ÿ’…

  • -fno-automatic โ€” this one treats each program unit (except the ones marked as RECURSIVE) like the SAVE statement was applied to every local variable and array in it ๐Ÿ”’ It doesn't touch common blocks. Some Fortran compilers call this option -static or -save ๐Ÿ™ˆ The default is -fautomatic, which puts local variables smaller than the value given by -fmax-stack-var-size on the stack. If you want no static memory at all, use -frecursive instead ๐ŸŒˆ

Local variables or arrays with an explicit SAVE attribute get silently ignored, unless you add the -pedantic option ๐Ÿ™ƒ

  • -ff2c โ€” generates code that's designed to be compatible with code from g77 and f2c ๐Ÿ”

Okay, the calling conventions used by g77 (originally from f2c) say that functions returning default REAL actually return the C type double. Functions returning COMPLEX instead hand back their values through an extra argument in the calling sequence, which points to where the return value should be stored ๐Ÿฅน With the default GNU calling conventions, those functions just return their results like they would in GNU C: 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 doesn't affect code that interfaces with the libgfortran library ๐Ÿ“š

Caution: It's 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 that were 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 โ€” this stops GNU Fortran from appending underscores to the names of entities in your Fortran source file ๐ŸŽ€

With -funderscoring in effect (the default vibe โœจ), GNU Fortran adds one underscore to external names that don't already have any. That's so the code matches what plenty of UNIX Fortran compilers produce ๐Ÿซถ

Caution: GNU Fortran's default behavior is incompatible with f2c and g77. If you want object files from GNU Fortran to play nice with object code made by those tools, use the -ff2c option ๐Ÿšจ

Using -fno-underscoring isn't recommended unless you're experimenting with things like fitting GNU Fortran into existing system environments (vis-a-vis existing libraries, tools, and so on) ๐Ÿงช

Let's look at an 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)

gets implemented as something akin to:

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

With -fno-underscoring, that same statement gets implemented as:

    i = j() + max_count(&my_var, &lvar);

Using -fno-underscoring lets you use your own names directly while debugging, and when you're hooking GNU Fortran code up to other languages ๐Ÿงš

Note that just because the names match doesn't mean the interface GNU Fortran uses for an external name matches the interface some other language uses for that same name ๐Ÿฅด Getting code from GNU Fortran to link with code from another compiler, using this or any other method, is only a small part of the job. Getting both compilers' code to agree on things other than naming can take a lot of effort, and unlike naming mismatches, linkers usually can't detect disagreements in those other areas ๐Ÿ˜ฉ

Also, with -fno-underscoring, the lack of appended underscores means a user-defined external name could totally clash with a name in a system library โšก That can make unresolved-reference bugs super hard to find, since they might only show up at program run time as weird buggy behavior ๐Ÿ’€

In future versions of GNU Fortran, the hope is to improve naming and linking so that debugging always uses the names as they appear in the source ๐Ÿ”ฎ Even if the linker sees mangled names (to prevent accidental linking between procedures with incompatible interfaces), you'd still debug with your source names ๐Ÿ’–

  • -fsecond-underscore โ€” by default, GNU Fortran appends one underscore to external names ๐ŸŒท With this option, GNU Fortran appends two underscores to names that have underscores in them, and one underscore to external names with no underscores. It also appends two underscores to internal names with underscores, so they don't collide with external names ๐Ÿงฉ

This option has no effect if -fno-underscoring is in effect ๐Ÿ™… It's implied by the -ff2c option.

With this option, an external name such as MAX_COUNT gets 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> โ€” this one controls coarray support, and you pick one of these keywords ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘
    • none โ€” disables coarray support. Using coarray declarations and image-control statements will throw a compile-time error ๐Ÿšซ (Default)
    • single โ€” single-image mode, meaning num_images() is always one 1๏ธโƒฃ
    • lib โ€” library-based coarray parallelization. You need to link a suitable GNU Fortran coarray library for this one ๐Ÿ“ฆ
  • -fcheck=<keyword> โ€” this turns on the generation of run-time checks ๐Ÿ” The argument should be a comma-delimited list of the keywords below. Putting no- in front of a check disables it, if a previous specification turned it on ๐Ÿ™…โ€โ™€๏ธ
    • all โ€” enables all run-time tests of -fcheck ๐Ÿ’ฏ
    • array-temps โ€” warns at run time when a temporary array had to be generated for passing an actual argument. The info from this warning can be handy for optimization, so you can avoid those temporaries ๐Ÿงน
      • Note: the warning only gets printed once per location ๐Ÿ“
    • bits โ€” enables run-time checks for invalid arguments to the bit manipulation intrinsics ๐Ÿ”ข
    • bounds โ€” enables run-time checks for array subscripts, against the declared minimum and maximum values ๐Ÿ“ It also checks array indices for assumed and deferred shape arrays against the actual allocated bounds, and makes sure all string lengths are equal for character array constructors without an explicit typespec ๐Ÿงต
      • Some checks require that -fcheck=bounds is set when compiling the main program ๐Ÿ‘‘
      • Note: in the future this may also include other forms of checking, e.g., checking substring references ๐Ÿ”ฎ
    • do โ€” enables run-time checks for invalid modification of loop iteration variables ๐Ÿ”
    • mem โ€” enables run-time checks for memory allocation ๐Ÿง 
      • Note: this option doesn't affect explicit allocations using the ALLOCATE statement, which are always checked anyway ๐Ÿ’…
    • pointer โ€” enables run-time checks for pointers and allocatables ๐Ÿ“Œ
    • recursion โ€” enables run-time checks for recursively called subroutines and functions that aren't marked as recursive. See also -frecursive ๐ŸŒ€
      • Note: this check doesn't work for OpenMP programs, and it's disabled if you use it together with -frecursive and -fopenmp ๐Ÿšซ

Example time ๐ŸŽ€ Say you have a file called foo.f90. The command:

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

will compile the file with all checks enabled as listed above, except the warnings for generated array temporaries ๐ŸŒธ

  • -fbounds-check โ€” this is a deprecated alias for -fcheck=bounds ๐Ÿฅ€
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” some C interfaces to Fortran codes break the gfortran ABI by leaving out the hidden character length arguments as described in ๐Ÿ™ˆ
Aside: the original page trails off right there, so there's no reference after "described in". It's not my fault, bestie ๐Ÿ˜ญ

Anyway, this can lead to crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿ’ฅ

To keep older binary packages working, this flag turns off tail call optimization for gfortran procedures that take character arguments. -ftail-call-workaround=2 turns it off in every gfortran procedure with character arguments. -ftail-call-workaround=1, or plain -ftail-call-workaround, only does it in gfortran procedures with character arguments that call implicitly prototyped procedures.

Heads up ๐Ÿ’€: using this can lead to problems, including crashes from running out of stack space.

It is very strongly recommended that you fix the code in question instead ๐Ÿซถ. The -fc-prototypes-external option can generate prototypes that follow gfortran's ABI, which you can then include in your source code.

Support for this option will likely be withdrawn in a future release of gfortran ๐Ÿ˜ญ.

You can switch this off with -fno-tail-call-workaround, or the equivalent -ftail-call-workaround=0.

The default is currently -ftail-call-workaround, but that's going to change in future releases ๐Ÿฆ‹.

  • -fcheck-array-temporaries โ€” This is a deprecated alias for -fcheck=array-temps, so just use the newer spelling ๐Ÿ’….
  • -fmax-array-constructor=n โ€” This lets you raise the upper limit allowed in array constructors ๐Ÿงš. The code below needs this option to expand the array at compile time:
        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: this can lead to long compile times and excessively large object files ๐Ÿ’€.

The default value for n is 65535.

  • -fmax-stack-var-size=n โ€” This sets the size in bytes of the biggest array that gets put on the stack ๐Ÿ“ฆ. If an array goes over that size, static memory is used instead, except in procedures marked RECURSIVE. To allow recursive procedures that don't have a RECURSIVE attribute, or for parallel programs, use -frecursive. To never use the stack at all, use -fno-automatic.

This option currently only affects local arrays declared with constant bounds, and it may not apply to every character variable. Future versions of GNU Fortran may improve this behavior ๐ŸŒธ.

The default value for n is 65536.

  • -fstack-arrays โ€” This makes the Fortran compiler put all arrays of unknown size and array temporaries onto stack memory ๐Ÿฅž. If your program uses really large local arrays, you may need to raise your runtime stack limits on some operating systems. This flag is enabled 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 tightly as possible ๐ŸŽ€. Code compiled with this 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 chunk of memory ๐Ÿงฉ. This option adds code to the function prologue that repacks the data into a contiguous block at runtime.

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

  • -fshort-enums โ€” This is here for interoperability with C code that was compiled with -fshort-enums ๐Ÿค. GNU Fortran will pick the smallest INTEGER kind that a given enumerator set fits in, and give all of its enumerators that kind.
  • -finline-arg-packing โ€” When you pass an assumed-shape argument of a procedure as an actual argument to an assumed-size or explicit-size argument, or to a procedure without an explicit interface, the argument may need to be packed (put into contiguous memory) ๐Ÿ“ฅ. An example is the call to foo below:
        subroutine foo(a)
           real, dimension(*) :: a
        end subroutine foo
        subroutine bar(b)
           real, dimension(:) :: b
           call foo(b)
        end subroutine bar

When -finline-arg-packing is in effect, that packing is done by inline code ๐Ÿš€. This allows more optimization, but it increases code size.

-finline-arg-packing is implied by any of the -O options, except when optimizing for size with -Os ๐Ÿ™ˆ. If the code has a huge number of arguments that need packing, code size and compile time can get out of hand. In that case, it may be better to turn this option off. You can find instances of packing by using -Warray-temporaries.

  • -fexternal-blas โ€” This makes gfortran call BLAS functions for some matrix operations, like MATMUL, instead of using its own algorithms ๐Ÿงฎ. That happens when the matrices are bigger than a given limit (see -fblas-matmul-limit). This can pay off if an optimized vendor BLAS library is available ๐Ÿ’œ. The BLAS library has to be specified at link time.
  • -fblas-matmul-limit=n โ€” This only matters when -fexternal-blas is in effect ๐Ÿ”ฅ. Matrix multiplication with matrices of size larger than or equal to n gets done by calls to BLAS functions, and everything smaller is 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 matrix size can't be determined at compile time, because code for both cases gets generated. Setting -finline-matmul-limit=0 turns inlining off in all cases. Setting it to a value of n produces inline code for matrices up to size 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 ๐ŸŒ€. This flag can't 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 โ€” These options set initial values for local variables ๐Ÿ“. -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.

Finer-grained initialization is available through -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 way as with -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.)

Note that -finit-real=nan initializes REAL and COMPLEX variables with a quiet NaN ๐Ÿซง. For a signalling NaN, use -finit-real=snan. Heads up though: compile-time optimizations may turn them into quiet NaNs, and trapping needs to be enabled (for example, with -ffpe-trap).

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

Last but not least, enabling any of the -finit-* options will silence warnings that -Wuninitialized would have emitted 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 some platforms this is mandatory, and on others it boosts performance. If a COMMON block isn't declared with consistent data types everywhere, that padding can cause trouble, and -fno-align-commons turns automatic alignment off. Use the same form of this option 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, parentheses in expressions are honored at all optimization levels, so the compiler doesn't re-associate anything ๐Ÿ”’. 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 also need to be in effect. 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 ๐Ÿช„. This is 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, whether or not they're marked PURE ๐Ÿ—‘๏ธ. 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 performs front-end optimization by manipulating parts of the Fortran parse tree ๐ŸŒŸ. It's enabled by default by any -O option except -O0 and -Og. Optimizations turned on by this option include:

Okay so -ffrontend-optimize also does these bits, bestie ๐Ÿ’…

  • 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.) ๐Ÿ”ฅ

Not into it? You can deselect the whole thing with -fno-frontend-optimize ๐Ÿ™ˆ

  • -ffrontend-loop-interchange โ€” this one tries to swap loops in the Fortran front end whenever it's actually worth it ๐ŸŒธ It's switched on by default with any -O option. Right now it only touches FORALL and DO CONCURRENT statements that have several forall triplets, so it's pretty niche ๐Ÿ’œ

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

Honestly, gfortran doesn't read any environment variables to control itself beyond the ones that already affect gcc. So if you're already set up for gcc, you're set for gfortran too ๐Ÿ’–


๐Ÿ› BUGS ๐Ÿ›

Want to report a bug? Check out <file:///usr/share/doc/gcc-10/README.Bugs> for the instructions ๐Ÿซถ


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

Go deeper with 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 ๐Ÿ‘‘

For the contributors to GCC and GNU Fortran, check the Info entry for gfortran. They're the real MVPs ๐Ÿ’œ


Copyright (c) 2004-2020 Free Software Foundation, Inc.

You're allowed to copy, distribute and/or modify this doc under the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation ๐Ÿฆ‹ The Invariant Sections are "Funding Free Software", the Front-Cover Texts are (a) (see below), and the Back-Cover Texts are (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."

That's the whole page, bestie! โœจ Huge thanks to the gcc-10 crew for keeping it glowing since 2023-07-07 ๐Ÿ’– Now go be a GFORTRAN(1) main character ๐Ÿ‘‘๐ŸŽ€

๐Ÿ“ฆ where this page comes from