GFORTRAN(1)

๐Ÿ“ฆ
version 11.5.0 ยท 5 versions
, in ubuntu questing, ubuntu resolute, ubuntu stonking

๐ŸŽ€ GFORTRAN(1) ๐ŸŽ€

Okay bestie, this is the GNU manual page for GFORTRAN(1), so grab your snacks ๐Ÿ“โœจ

โœจ NAME โœจ

gfortran is 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...

Heads up: the synopsis only shows the most useful options, and the rest are waiting for you further down ๐Ÿงš

๐Ÿ’– DESCRIPTION ๐Ÿ’–

The gfortran command is literally a queen. It supports all the options that the gcc command supports ๐Ÿ‘‘ Only the options that are specific to GNU Fortran get documented here, so you don't have to read the same stuff twice.

Every GCC and GNU Fortran option works with both gfortran and gcc, plus any other drivers that got built at the same time, like g++. Adding GNU Fortran to the GCC distribution means all the relevant drivers accept GNU Fortran options, so it's giving teamwork ๐Ÿซถ

Some options come in positive and negative forms. The negative form of -ffoo is -fno-foo. This manual only documents whichever of the two forms is not the default, so keep that in mind ๐ŸŒˆ

๐ŸŒธ OPTIONS ๐ŸŒธ

Here's the full tea on every option that's specific to GNU Fortran, sorted into groups by type. The explanations come after the lists โœจ

๐Ÿ’… 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
-fdebug-aux-vars -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

Okay so these options are the ones that control the exact flavor of Fortran the compiler accepts ๐Ÿฆ‹

  • -ffree-form, -ffixed-form โ€” These pick the layout of the source file. Free form came along in Fortran 90, while fixed form is the classic look that older Fortran programs were written in. If you don't pick either one, the compiler figures out the source form from the file extension, which is kind of iconic of it ๐Ÿ’…
  • -fall-intrinsics โ€” This makes the compiler accept every intrinsic procedure, GNU-specific extensions included, so you get the whole intrinsics buffet โœจ It's super handy with -std=f95 if you want standard compliance but still want full gfortran access. The catch: -Wintrinsics-std gets ignored, and no user-defined procedure that shares a name with an intrinsic will be called unless it's explicitly declared EXTERNAL ๐Ÿ™ˆ
  • -fallow-argument-mismatch โ€” Some code calls external procedures with mismatched arguments, either against the procedure's definition or against other calls. That code isn't conforming, and it usually gets flagged as an error. This option downgrades that error to a warning, and the only way to switch that warning off is to disable all warnings with -w. Only one occurrence per argument gets flagged. This option is implied by -std=legacy ๐Ÿ’€
Using this option is strongly discouraged, bestie ๐Ÿšซ You can write standard-conforming code that allows different argument types by using an explicit interface and TYPE(*), so there's zero reason to lean on this one.
  • -fallow-invalid-boz โ€” A BOZ literal constant can only show up in a few contexts in standard-conforming Fortran. This option turns the error into a warning and lets a BOZ literal constant appear where the Fortran standard would normally ban it ๐Ÿ™ƒ
  • -fd-lines-as-code, -fd-lines-as-comments โ€” These give special treatment to lines that start with d or D in fixed form sources. With -fd-lines-as-code, those lines get treated as if their first column held a blank. With -fd-lines-as-comments, they get 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). Heads up, though: these features are non-standard and should be avoided at all costs ๐Ÿ’€ For details on how GNU Fortran implements these extensions, check the full documentation. Other flags that this switch turns on 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 or -fd-lines-as-comments aren't set, then -fdec also sets -fd-lines-as-comments ๐ŸŽ€
  • -fdec-char-conversions โ€” This lets you use character literals in assignments and DATA statements for non-character variables ๐Ÿ“
  • -fdec-structure โ€” This enables DEC STRUCTURE and RECORD, plus UNION, MAP, and the dot (.) as a member separator, which is in addition to %. It's only here for compatibility, so use Fortran 90 derived types instead whenever you can ๐Ÿซ 
  • -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 complete list of intrinsics, see 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 specify the storage of variables and other objects ๐Ÿ“ฆ
  • -fdec-include โ€” This lets INCLUDE be parsed as a statement in addition to parsing it as an INCLUDE line. When it's parsed as a statement, the INCLUDE doesn't have to sit on a single line and can use line continuations โœจ
  • -fdec-format-defaults โ€” This lets the format specifiers F, G, and I get used without width specifiers, and default widths get used instead ๐ŸŒธ
  • -fdec-blank-format-item โ€” This enables a blank format item at the end of a format specification, meaning there's nothing after the final comma ๐Ÿ™ˆ
  • -fdollar-ok โ€” This allows $ as a valid non-first character in a symbol name. Symbols that start with $ are rejected, because it's unclear which implicit typing rules should apply since different vendors do different things. Using $ in IMPLICIT statements is also rejected ๐Ÿšซ
  • -fbackslash โ€” This changes how backslashes work in string literals. Instead of a single backslash character, you get "C-style" escape characters. These combos get expanded: "\a" (alert), "\b" (backspace), "\f" (form feed), "\n" (newline), "\r" (carriage return), "\t" (horizontal tab), "\v" (vertical tab), "\\" (backslash), and "\0" (NUL). 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 ๐ŸŒˆ Every other combo 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. Unless you use -fno-pad-source, it also sets the column through which spaces are assumed (as if the line were padded 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 have implicit spaces appended to fill out the line. -ffixed-line-length-0 does the exact 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 you use -ffixed-line-length-0, -ffixed-line-length-none, or -fno-pad-source. With any of those options, continued character constants never get implicit spaces appended to them to fill out the line ๐Ÿงš
  • -ffree-line-length-n โ€” okay so this sets the column where gfortran stops caring about a typical free-form source line, and everything past it gets ignored ๐Ÿ’… The default is 132. You can pass n as none so the entire line counts, and -ffree-line-length-0 is the exact same thing as -ffree-line-length-none ๐Ÿฆ‹
  • -fmax-identifier-length=n โ€” sets the longest identifier name you're allowed to use ๐Ÿงš Typical values are 31 (Fortran 95) and 63 (Fortran 2003 and Fortran 2008) โœจ
  • -fimplicit-none โ€” no implicit typing allowed, unless you override it with explicit IMPLICIT statements ๐Ÿ™ˆ It's basically the same as slapping implicit none at the top of every procedure ๐Ÿ”ฅ
  • -fcray-pointer โ€” turns on the Cray pointer extension, which gives you C-style pointer vibes ๐ŸŒธ
  • -fopenacc โ€” enables the OpenACC extensions ๐Ÿ‘‘ That means !$acc directives in free form, and c$acc, *$acc and !$acc directives in fixed form. It also covers the !$ conditional compilation sentinels in free form, and c$, *$ and !$ sentinels in fixed form. And when you link, the OpenACC runtime library gets linked in too ๐Ÿซถ
  • -fopenmp โ€” enables the OpenMP extensions ๐ŸŒˆ That means !$omp directives in free form, and c$omp, *$omp and !$omp directives in fixed form. It also covers the !$ sentinels in free form, and c$, *$ and !$ sentinels in fixed form, and linking pulls in the OpenMP runtime library. This option implies -frecursive ๐Ÿ”ฅ
  • -fno-range-check โ€” 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 option there's no error, and a gets +Infinity. If an expression evaluates to something outside the relevant range ["-HUGE()":HUGE()], it gets replaced by -Inf or +Inf, whichever fits. Similarly, DATA i/Z'FFFFFFFF'/ causes an integer overflow on most systems, but with -fno-range-check the value wraps around and i gets initialized to -1 instead โœจ
  • -fdefault-integer-8 โ€” the default integer and logical types become 8 bytes wide ๐Ÿ”ข This also changes the kind of integer constants like 42. Unlike -finteger-4-integer-8, it does not promote variables with an explicit kind declaration.
  • -fdefault-real-8 โ€” the default real type becomes 8 bytes wide ๐Ÿ’– This also affects the kind of non-double real constants like 1.0. It bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible. If you pass -fdefault-double-8 along with fdefault-real-8, then DOUBLE PRECISION and double real constants are not promoted. Unlike -freal-4-real-8, fdefault-real-8 doesn't promote variables with explicit kind declarations.
  • -fdefault-real-10 โ€” same energy, but the default real type becomes a 10 byte wide type ๐Ÿง This also affects the kind of non-double real constants like 1.0. It bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible. If you pass -fdefault-double-8 along with fdefault-real-10, then DOUBLE PRECISION and double real constants are not promoted. Unlike -freal-4-real-10, fdefault-real-10 doesn't promote variables with explicit kind declarations.
  • -fdefault-real-16 โ€” the default real type becomes a 16 byte wide type ๐ŸŒŸ This also affects the kind of non-double real constants like 1.0. It bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible. If you pass -fdefault-double-8 along with fdefault-real-16, then DOUBLE PRECISION and double real constants are not promoted. Unlike -freal-4-real-16, fdefault-real-16 doesn't 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 should be used with care, and it 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. Inspecting the intermediate representation of the translated Fortran code, produced by -fdump-tree-original, is suggested.
  • -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 promote all REAL(KIND=M) entities to REAL(KIND=N) entities ๐Ÿ‘‘ If REAL(KIND=N) isn't available, you'll get an error. The -freal-4- flags also affect the default real kind, and the -freal-8- flags affect the double-precision real kind. Every other real-kind type is left alone. The promotion also applies to real literal constants of default and double-precision kind with a specified kind number of 4 or 8, respectively. However, -fdefault-real-8, -fdefault-real-10, -fdefault-real-10, and -fdefault-double-8 take precedence for the default and double-precision real kinds, both for real literal constants and for declarations without a kind number. Heads up: for REAL(KIND=KIND(1.0)), the literal may get promoted and then the result may get promoted again ๐Ÿ™ˆ
๐Ÿšจ These options should be used with care, and they may not suit your codes. Areas of concern include calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, I/O, and calls to intrinsic procedures when passing a value to the kind= dummy argument. Inspecting the intermediate representation of the translated Fortran code, produced by -fdump-fortran-original or -fdump-tree-original, is suggested.
  • -std=std โ€” pick the standard your program is expected to follow ๐Ÿ“œ The options are f95, f2003, f2008, f2018, gnu, or legacy.
    • gnu โ€” the default ๐Ÿ’… It's a superset of the latest Fortran standard that includes every extension GNU Fortran supports. You'll still get warnings for obsolete extensions that aren't recommended for new code.
    • legacy โ€” basically the same as gnu, just without those warnings for obsolete extensions. It's handy for old non-standard programs ๐Ÿง“
    • f95, f2003, f2008, f2018 โ€” strict conformance to the Fortran 95, Fortran 2003, Fortran 2008 and Fortran 2018 standards, respectively ๐ŸŽฏ You get errors for every extension beyond the relevant language standard, and warnings for the Fortran 77 features that are still permitted but obsolescent in later standards.
    • > ๐Ÿšจ The deprecated -std=f2008ts acts as an alias for -std=f2018. It only exists for backwards compatibility with earlier gfortran versions, so please don't use it any more ๐Ÿ’€
  • -ftest-forall-temp โ€” a test coverage booster ๐Ÿงช It forces most forall assignments to use a temporary.

๐ŸŽ€ Enable and customize preprocessing ๐ŸŽ€

Preprocessor related options ๐Ÿงฉ For more detailed info on preprocessing in gfortran, check out the section called Preprocessing and conditional compilation.

  • -cpp, -nocpp โ€” -cpp turns preprocessing on ๐ŸŒธ The preprocessor gets invoked automatically if the file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03 or .F08. Use this flag 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. Restrictions on the file format, especially the line length limits, apply to preprocessed output too, 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 all the macros defined while the preprocessor ran, including the predefined ones ๐Ÿ” This is a great way to find out what's predefined in your version of the preprocessor. Assuming you have no file foo.f90, run:
touch foo.f90; gfortran -cpp -E -dM foo.f90

and it'll show all the predefined macros โœจ

  • -dD โ€” like -dM, except it doesn't include 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, not their expansions ๐Ÿซง
  • -dU โ€” like dD except only macros that are expanded, or whose definedness is tested in preprocessor directives, get output ๐Ÿฆ‹ The output is delayed until the macro is used or tested, and #undef directives are also output for macros that were tested but undefined at the time.
  • -dI โ€” outputs #include directives in addition to the result of preprocessing ๐Ÿ“‚
  • -fworking-directory โ€” enables linemarkers in the preprocessor output so the compiler knows the current working directory at preprocessing time ๐Ÿ—บ๏ธ When this is on, the preprocessor emits a second linemarker after the initial one, containing the current working directory followed by two slashes. GCC uses that directory, when it's present in the preprocessed input, as the directory it emits as the current working directory in some debugging information formats. This option is implicitly enabled if debugging information is enabled, but you can inhibit it with the negated form -fno-working-directory. If the -P flag is on the command line, this option has no effect, since no #line directives are emitted whatsoever ๐Ÿ™ˆ
  • -idirafter dir โ€” searches dir for include files, but only after all the directories specified with -I and the standard system directories have been exhausted ๐ŸŒˆ dir is treated as a system include directory. If dir begins with =, then that = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐Ÿงš

-imultilib dir โ€” โœจ Okay so this one lets you point to a dir that becomes a subdirectory of the folder holding the target-specific C++ headers. Very niche, very bestie-only ๐Ÿงš

  • -iprefix prefix โ€” ๐ŸŒธ This sets the prefix for any -iwithprefix options that come after it. If your prefix is a directory, don't forget the final / ๐Ÿ™ˆ
  • -isysroot dir โ€” ๐Ÿ—บ๏ธ This one is basically a sibling of --sysroot, but it only touches header files. Go check the --sysroot option for the full tea โ˜•
  • -iquote dir โ€” ๐Ÿฆ‹ This searches dir only for headers pulled in with #include "file". It skips #include <file> entirely, and it runs before all the -I directories and the standard system directories. If dir starts with =, that = gets swapped out for the sysroot prefix. See --sysroot and -isysroot ๐Ÿ’…
  • -isystem dir โ€” โญ This searches dir for headers after all the -I directories but before the standard system ones. It also gets marked as a system directory, so it gets the same VIP treatment as the standard system dirs. Same deal with =: if dir begins with =, it's replaced by the sysroot prefix. See --sysroot and -isysroot ๐Ÿ‘‘
  • -nostdinc โ€” ๐Ÿ™… Don't search the standard system directories for headers at all. Only the -I directories get checked (plus the directory of the current file, if that's relevant) ๐Ÿซถ
  • -undef โ€” ๐Ÿงผ Don't predefine any system-specific or GCC-specific macros. The standard predefined macros stay put, so they're still defined โœจ
  • -Apredicate=answer โ€” ๐Ÿ’œ This makes an assertion with the predicate predicate and the answer answer. It's the preferred version over the older -A predicate(answer) form, which still works, but this one is way better because it doesn't use shell special characters ๐Ÿ™Œ
  • -A-predicate=answer โ€” ๐Ÿ—‘๏ธ Cancels an assertion with the predicate predicate and the answer answer. Bye bye ๐Ÿ‘‹
  • -C โ€” ๐Ÿ’ฌ Don't throw out comments! Every comment gets passed through to the output file, except comments inside processed directives, which get deleted along with their directive.

Heads up, though: using -C has side effects, because the preprocessor starts treating comments as tokens in their own right. For example, a comment sitting at the start of what would be a directive line turns that line into an ordinary source line, since the first token is no longer a # ๐Ÿ˜ญ

โš ๏ธ Warning: this currently handles C-style comments only. The preprocessor doesn't recognize Fortran-style comments yet.
  • -CC โ€” ๐Ÿ’ฌ๐Ÿ’ฌ Also don't discard comments, including during macro expansion. It's like -C, except comments inside macros get passed through to the output wherever the macro is expanded.

On top of everything -C does, -CC converts all C++-style comments inside a macro into C-style comments. Why? So using that macro later can't accidentally comment out the rest of your source line ๐Ÿ’€ This one is mostly used to support lint comments.

โš ๏ธ Warning: this currently handles C- and C++-style comments only. The preprocessor doesn't recognize Fortran-style comments yet.
  • -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. Heads up that the definition gets cut off at any embedded newline characters.

If you're calling the preprocessor from a shell (or something shell-like), you might need the shell's quoting syntax to protect characters like spaces, since they mean something to the shell too.

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

-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 every -D and -U ๐Ÿซถ

  • -H โ€” ๐Ÿ” Prints the name of each header file used, on top of all the usual stuff. Each name is indented to show how deep it sits in the #include stack, which is lowkey so helpful for tracking down nested includes ๐ŸŒˆ
  • -P โ€” ๐Ÿšซ Inhibits the 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 could 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 ๐Ÿšจ

Okay, let's talk about the difference between errors and warnings, because it matters ๐Ÿ’…

Errors are diagnostic messages saying the GNU Fortran compiler cannot compile that piece of source code. The compiler keeps going anyway, trying to report more errors to help you 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 your program. Unless -Werror is specified, warnings don't stop your program from compiling ๐Ÿฆ‹

You can request lots of specific warnings with options that begin with -W. For example, -Wimplicit asks for warnings on implicit declarations. Each of these specific warning options also has a negative form starting with -Wno- to turn the warnings off, like -Wno-implicit. This manual only lists one of the two forms, whichever one is not the default โœจ

These options control how many errors and warnings GNU Fortran produces, and what kinds they are:

  • -fmax-errors=n โ€” ๐Ÿ›‘ Limits the number of error messages to n. Once that limit is hit, GNU Fortran bails out instead of trying to keep processing the source code. If n is 0, there's no limit on how many error messages get 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 when 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, certain GNU extensions and traditional Fortran features are still supported. With it, a lot of those 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 welcome, though ๐Ÿ™

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

  • -pedantic-errors โ€” ๐Ÿšจ Like -pedantic, except errors get produced instead of warnings. Serious energy.
  • -Wall โ€” ๐Ÿ‘‘ Enables commonly used warning options for usage that 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: if there's no ampersand in a continued character constant, GNU Fortran assumes continuation happens at the first non-comment, non-whitespace character after the ampersand that started the continuation ๐Ÿซถ

  • -Warray-temporaries โ€” ๐Ÿงบ Warns about array temporaries generated by the compiler. The info from this warning can be useful for optimization, so you can avoid those temporaries.
  • -Wc-binding-type โ€” ๐Ÿ”— Warns if a variable might not be C interoperable. In particular, it warns if the variable was declared using an intrinsic type with the 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 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 that this only checks that an explicit interface is present. It doesn't 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. Math is not mathing ๐Ÿ’€
  • -Wintrinsics-std โ€” okay so this one is a little heads-up bestie ๐Ÿ”” If gfortran spots a procedure named like an intrinsic that isn't available in the standard you picked (with -std), it warns you and treats that procedure as an EXTERNAL procedure. If you never want that to happen and always want the intrinsic linked no matter which standard you chose, -fall-intrinsics is the move โœจ
  • -Wno-overwrite-recursive โ€” no warning when you use -fno-automatic with -frecursive ๐Ÿ™ˆ Recursion gets broken if the relevant local variables don't have the AUTOMATIC attribute explicitly declared. Use this to silence the warning when you already know recursion isn't broken. It's super useful for build setups that use -Werror ๐Ÿซถ
  • -Wreal-q-constant โ€” get a warning if a real literal constant has a q exponent letter ๐ŸŒธ
  • -Wsurprising โ€” this one catches "suspicious" code ๐Ÿ‘€ It's technically legal, but these constructs usually mean you made a mistake somewhere. Right now it warns in these situations:
    • An INTEGER SELECT construct has a CASE that can never be matched, because its lower value is greater than its upper value.
    • A LOGICAL SELECT construct has three CASE statements.
    • A TRANSFER has 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, not just a warning ๐Ÿ’€
    • A CHARACTER variable is declared with a negative length.
  • -Wtabs โ€” tabs are accepted as whitespace by default, but tabs are NOT members of the Fortran Character Set, so be careful ๐Ÿ“ For continuation lines, a tab followed by a digit between 1 and 9 is supported. With -Wtabs, you'll get a warning whenever a tab shows up. It's 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 underflows or overflows while the induction variable is iterating ๐ŸŒˆ This one is implied by -Wall.
  • -Wunderflow โ€” warns when numerical constant expressions are encountered that UNDERFLOW during compilation โญ It's enabled by default, so you get it 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 later calls get resolved to the procedure you actually meant. This one is implied by -Wall.
  • -Wuse-without-only โ€” warns if a USE statement has no ONLY qualifier, because then it quietly imports every public entity of the used module ๐Ÿงš
  • -Wunused-dummy-argument โ€” warns about unused dummy arguments. Implied by -Wall ๐ŸŽ€
  • -Wunused-parameter โ€” okay, plot twist: this is NOT the same as gcc's version ๐Ÿ˜ญ gfortran's version doesn't warn about unused dummy arguments (check -Wunused-dummy-argument for that). Instead it warns about unused PARAMETER values. -Wunused-parameter is implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons โ€” by default, gfortran warns about any time variables get padded for proper alignment inside a COMMON block ๐Ÿงธ You can turn that off with -Wno-align-commons. See also -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 code for allocation or reallocation of an allocatable array variable of intrinsic type in intrinsic assignments ๐Ÿ”ฅ In hot loops, the Fortran 2003 reallocation feature may hurt performance. If the array is already allocated with the right shape, consider using a whole-array array-spec (e.g. (:,:,:)) for the variable on the left-hand side to skip the reallocation check. Heads up: sometimes this warning shows up even when the compiler would optimize those reallocation checks away, like when the right-hand side contains the same variable multiplied by a scalar. See also -frealloc-lhs.
  • -Wrealloc-lhs-all โ€” warns when the compiler inserts code for allocation or reallocation of an allocatable variable. This includes scalars and derived types too ๐Ÿ’œ
  • -Wcompare-reals โ€” warns when you compare real or complex types for equality or inequality ๐Ÿ‘€ Implied by -Wextra.
  • -Wtarget-lifetime โ€” warns if the pointer in a pointer assignment might outlive its target ๐Ÿฆ‹ Implied by -Wall.
  • -Wzerotrip โ€” warns if a DO loop is known to execute zero times at compile time ๐Ÿ™ƒ 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 ๐Ÿ˜ฌ Like in this case:
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 after the source program gets translated into internal representation ๐ŸŒธ This one is mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and it may also generate 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 might change between releases, and it may also cause internal compiler errors for features that were only recently added.
  • -fdump-parse-tree โ€” outputs the internal parse tree after translating the source program into internal representation. It's mostly useful for debugging the GNU Fortran compiler itself, and the output might change between releases. It may also generate internal compiler errors for features that were only recently added. > This one is deprecated, so use -fdump-fortran-original instead ๐Ÿ˜ญ
  • -fdebug-aux-vars โ€” renames the internal variables the gfortran front end creates and makes them accessible to a debugger ๐Ÿ” Their names start with upper-case letters followed by an underscore. This is handy for debugging the compiler's code generation alongside -fdump-tree-original, and for debugging the executable by using -g or -ggdb3.
  • -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 generate 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 aborts, producing a core file that's useful for debugging. list is a (possibly empty) comma-separated list of these exceptions:
    • invalid โ€” an invalid floating point operation, such as 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 correspond to the five IEEE 754 exceptions. The last one, denormal, isn't part of IEEE 754, 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 ways of dealing with them, enabling traps for these three is probably a good idea ๐Ÿซถ

If the option is used more than once on the command line, the lists get joined. '"ffpe-trap="list1 "ffpe-trap="list2' is equivalent to "ffpe-trap="list1,list2.

Once an exception is enabled, it can't be disabled. There's no negative form!

Lots of floating point operations lose precision due to rounding, so ffpe-trap=inexact is likely to be pretty uninteresting 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 and ERROR STOP are invoked ๐Ÿ“‹ list can be none, all, or a comma-separated list of these exceptions: invalid, zero, overflow, underflow, inexact and denormal. (Check -ffpe-trap for descriptions of the exceptions.)

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

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

  • -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 output a backtrace of the error ๐Ÿชฆ -fno-backtrace turns the backtrace generation off. This option only has an effect when compiling the Fortran main program.

These options change how GNU Fortran looks for files specified 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 โ€” these affect how the INCLUDE directive gets interpreted (and the #include directive of the cpp preprocessor too) ๐Ÿฆ‹

okay bestie, the way -I and "INCLUDE" work is basically the same as -I with "#include" in the cpp preprocessor ๐ŸŒธ when it comes to hunting down header.gcc files and other stuff like that. no cap, it's giving consistency โœจ

This path is also used to search for .mod files when previously compiled modules are required by a "USE" statement ๐Ÿ’œ

  • -Jdir โ€” this one tells the compiler where to drop the .mod files for the modules it compiles ๐Ÿ“ it also gets added to the list of directories a "USE" statement searches ๐Ÿ”

The default is the current directory.

  • -fintrinsic-modules-path dir โ€” point this at the pre-compiled intrinsic modules if they're chilling somewhere other than the default spot the compiler expects ๐Ÿงš

โšก Influencing the linking step โšก

these options only come into play when the compiler links object files into an executable output file ๐Ÿ”— if the compiler isn't doing a link step, they're totally meaningless, so don't stress ๐Ÿ™ˆ

  • -static-libgfortran โ€” on systems that have libgfortran as both a shared and a static library, this forces the static version ๐Ÿ’œ if no shared version got built when the compiler was configured, this option does absolutely nothing ๐Ÿ’€

๐ŸŒˆ Influencing runtime behavior ๐ŸŒˆ

these options change how programs compiled with GNU Fortran behave at runtime โœจ

  • -fconvert=conversion โ€” this sets how data is represented in unformatted files ๐Ÿฆ‹ the valid values for conversion are:
    • native โ€” the default, no cap ๐Ÿ‘‘
    • swap โ€” swaps between big- and little-endian ๐Ÿ”„
    • big-endian โ€” uses big-endian representation for unformatted files
    • little-endian โ€” uses little-endian representation for unformatted files
โš ๏ธ this one only has an effect when it's used in the main program. The "CONVERT" specifier and the GFORTRAN_CONVERT_UNIT environment variable both override whatever default you set with -fconvert, so they basically get the final say ๐Ÿ’…
  • -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 older gfortran versions, which defaulted to a record marker length of 8 on most systems ๐Ÿ™ˆ if you want to read or write files that are compatible with earlier gfortran versions, use -frecord-marker=8.
  • -fmax-subrecord-length=length โ€” sets the maximum length for a subrecord ๐Ÿ“ the max permitted value for length is 2147483639, which is also the default. honestly it's only really useful for the gfortran testsuite, so unless you're in that world, you can skip it ๐Ÿงช
  • -fsign-zero โ€” when this is enabled, floating point numbers with a value of zero but the sign bit set get written as negative numbers in formatted output, and they're treated as negative in the "SIGN" intrinsic ๐Ÿ’– -fno-sign-zero goes the other way: it doesn't print the negative sign of zero values (or values rounded to zero for I/O), and it counts zero as a positive number in the "SIGN" intrinsic, which is for compatibility 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 ๐Ÿงš

most of them have both positive and negative forms, and the negative form of -ffoo is -fno-foo ๐Ÿ’œ in the table below, only one of the forms is listed, the one that isn't the default. you can figure out the other form by either removing no- or adding it ๐Ÿ™ˆ

  • -fno-automatic โ€” treats each program unit (except those marked as 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 give this option the name -static or -save.) the default, which is -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 the option -frecursive ๐ŸŒธ

Local variables or arrays that have an explicit "SAVE" attribute are silently ignored unless the -pedantic option is added.

  • -ff2c โ€” generates code designed to be compatible with code generated by g77 and f2c ๐Ÿซถ

the calling conventions used by g77 (originally implemented in f2c) require functions that return type default "REAL" to actually return the C type "double", and functions that return type "COMPLEX" to return their values via an extra argument in the calling sequence that points to where the return value should be stored ๐Ÿ˜ญ under the default GNU calling conventions, those functions just return their results the way GNU C does: default "REAL" functions return the C type "float", and "COMPLEX" functions return the GNU C type "complex" โœจ on top of that, this option implies the -fsecond-underscore option, unless -fno-second-underscore is explicitly requested.

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 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 โ€” do not transform names of entities specified 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 ensure compatibility with code produced by many UNIX Fortran compilers ๐ŸŒˆ

โš ๏ธ Caution: the default behavior of GNU Fortran is incompatible with f2c and g77, so please use the -ff2c option if you want object files compiled with GNU Fortran to be compatible with object code created with these tools.

use of -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

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);

use of -fno-underscoring allows direct specification of user-defined names while debugging and when interfacing GNU Fortran code with other languages ๐Ÿซถ

note that just because the names match doesn't mean the interface implemented by GNU Fortran for an external name matches the interface implemented by some other language for that same name ๐Ÿค” getting code produced by GNU Fortran to link to code produced by some other compiler using this or any other method can be only a small part of the overall solution. getting the code generated by both compilers to agree on issues other than naming can require significant effort, and unlike naming disagreements, linkers normally cannot detect disagreements in these other areas.

also, note that with -fno-underscoring, the lack of appended underscores introduces the very real possibility that a user-defined external name will conflict with a name in a system library ๐Ÿ˜ฑ this 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 at run time.

in future versions of GNU Fortran, we hope to improve naming and linking issues so that debugging always involves using the names as they appear in the source, even if the names as seen by the linker are mangled to prevent accidental linking between procedures with incompatible interfaces ๐Ÿ”ฎ

  • -fsecond-underscore โ€” by default, GNU Fortran appends an underscore to external names ๐Ÿ’ซ if this option is used, GNU Fortran appends two underscores to names with underscores and one underscore to external names with no underscores. GNU Fortran 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.

otherwise, 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 use of the -ff2c option ๐Ÿฆ‹

  • -fcoarray=<keyword> โ€” controls coarray support ๐Ÿ‘‘ the keyword can be:
    • none โ€” disables 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> โ€” enables the generation of run-time checks ๐Ÿ” the argument should be a comma-delimited list of the following keywords. prefixing a check with no- disables it if it was activated by a previous specification ๐Ÿ’…
    • all โ€” enables all run-time tests of -fcheck ๐ŸŒŸ
    • array-temps โ€” warns at run time when, for passing an actual argument, a temporary array had to be generated. the info from this warning is sometimes useful in optimization, so you can avoid such temporaries ๐Ÿง 

๐Ÿ™ˆ Note: the warning is only printed once per location.

  • bits โ€” enables generation of run-time checks for invalid arguments to the bit manipulation intrinsics ๐Ÿฆ‹
  • bounds โ€” enables 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 โ€” enables generation of run-time checks for invalid modification of loop iteration variables ๐Ÿ”
  • mem โ€” ๐Ÿง  okay so this one turns on run-time checks for memory allocation. Heads up, bestie: it does not touch explicit allocations made with the ALLOCATE statement. Those are always checked, no exceptions ๐Ÿ’…
  • pointer โ€” ๐Ÿฆ‹ checks pointers and allocatables at run time
  • recursion โ€” ๐ŸŒ€ checks for subroutines and functions that get called recursively but aren't marked as RECURSIVE. See also -frecursive. โš ๏ธ This check doesn't work for OpenMP programs, and it gets switched off if you use it 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 โ€” ๐Ÿซถ a deprecated alias for -fcheck=bounds, so it still works but it's on its way out
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” ๐Ÿ”ง okay this one is a whole saga. Some C interfaces to Fortran code violate the gfortran ABI by leaving out the hidden character length arguments as described in. This can lead to crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿ˜ญ

To help out existing binary packages, this option turns off 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 equivalent -ftail-call-workaround, it's only disabled 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 generate prototypes that conform to gfortran's ABI, which you can include in your source code ๐ŸŒธ

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

The negative form, -fno-tail-call-workaround or equivalent -ftail-call-workaround=0, can be used to disable this option.

Default is currently -ftail-call-workaround. This will change in future releases.

  • -fcheck-array-temporaries โ€” ๐Ÿ™ˆ a deprecated alias for -fcheck=array-temps
  • -fmax-array-constructor=n โ€” ๐Ÿ“ raises 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 option can lead to long compile times and excessively large object files.

The default value for n is 65535.

  • -fmax-stack-var-size=n โ€” ๐Ÿ“š sets the size in bytes of the largest array that gets put on the stack. If the size is exceeded, static memory is used instead (except in procedures marked as RECURSIVE). Use -frecursive to allow recursive procedures that don't have a RECURSIVE attribute, or for parallel programs. Use -fno-automatic to never use the stack.

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

The default value for n is 65536.

  • -fstack-arrays โ€” ๐Ÿฅž stacks up every array of unknown size plus array temporaries onto stack memory. If your program uses very large local arrays, you might have to extend your runtime limits for stack memory on some operating systems. This flag is enabled by default at optimization level -Ofast, unless -fmax-stack-var-size is specified.
  • -fpack-derived โ€” ๐Ÿ“ฆ tells GNU Fortran to pack derived type members as closely as possible. Code compiled with this option is likely to be incompatible with code compiled without it, and it may execute slower ๐Ÿข
  • -frepack-arrays โ€” ๐Ÿช„ sometimes GNU Fortran passes assumed shape array sections via 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 array accesses faster. However, it can add significant overhead to the function call, especially when the passed data is noncontiguous ๐Ÿค”

  • -fshort-enums โ€” ๐Ÿ”ข provided for interoperability with C code that was compiled with -fshort-enums. It makes GNU Fortran choose the smallest INTEGER kind that a given enumerator set will fit in, and gives all 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 in:
subroutine foo(a)
   real, dimension(*) :: a
end subroutine foo
subroutine bar(b)
   real, dimension(:) :: b
   call foo(b)
end subroutine bar

With -finline-arg-packing in effect, this packing is done by inline code. That allows more optimization, but the code size goes up ๐Ÿ“ˆ

-finline-arg-packing is implied by any of the -O options, except when optimizing for size via -Os. If the code has a very large number of arguments that need packing, the code size and compilation time may get excessive. In that case, it may be better to disable this option. You can find instances of packing by using -Warray-temporaries.

  • -fexternal-blas โ€” ๐Ÿงฎ makes gfortran call BLAS functions for some matrix operations like MATMUL, instead of using its own algorithms, if the matrices involved are bigger than a given limit (see -fblas-matmul-limit). This can be a win if an optimized vendor BLAS library is available. The BLAS library has to be specified at link time ๐Ÿ”—
  • -fblas-matmul-limit=n โ€” ๐ŸŽฏ only matters when -fexternal-blas is in effect. Matrix multiplication of matrices with a size larger than (or equal to) n gets done 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 is generated. Setting -finline-matmul-limit=0 disables inlining in all cases. Setting this option with 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 โ€” ๐Ÿ” 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 โ€” ๐ŸŒฑ the init crew! -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 way they are with -finit-local-zero, as described above.

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 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 -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 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, so -fno-align-commons can be used to disable automatic alignment. 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 โ€” okay so by default, parentheses in an expression are honored at every optimization level, which means the compiler is not allowed to do any re-association ๐Ÿ™…โ€โ™€๏ธ. Using -fno-protect-parens lets the compiler reorder REAL and COMPLEX expressions to get faster code โšก. Heads up, bestie: for that 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 ๐Ÿฆ‹. This is on by default except when -std=f95 is given. Also check out -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 one does front-end optimization by messing with parts of the Fortran parse tree ๐Ÿงš. It's on by default with any -O option except -O0 and -Og ๐Ÿ’–. The optimizations it turns on 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)), and
    • short-circuiting of logical operators (.AND. and .OR.).

You can deselect it with -fno-frontend-optimize ๐Ÿ™ˆ.

  • -ffrontend-loop-interchange โ€” this tries to swap loops around in the Fortran front end when it's worth it ๐ŸŒˆ. It's on by default with any -O option ๐Ÿ‘‘. Right now it only affects FORALL and DO CONCURRENT statements that have several forall triplets ๐Ÿ“.

โœจ ENVIRONMENT โœจ

okay so gfortran doesn't use any environment variables to control how it runs, beyond the ones that already affect gcc ๐ŸŒธ. Main character energy, but make it minimal ๐Ÿ’….


๐ŸŽ€ BUGS ๐ŸŽ€

If you find a bug and want to report it, the instructions live in <file:///usr/share/doc/gcc-11/README.Bugs> ๐Ÿ›. Go read it, bestie ๐Ÿซถ.


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

Here's the squad of related pages and docs ๐Ÿ’œ: gpl(7), gfdl(7), fsf-funding(7), cpp(1), gcov(1), gcc(1), as(1), ld(1), gdb(1), and dbx(1). You can also check the Info entries for gcc, cpp, gfortran, as, ld, binutils, and gdb ๐Ÿ“šโœจ.


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

For the contributors to GCC and GNU Fortran, look up the Info entry for gfortran ๐Ÿ™Œ. They're the real MVPs ๐Ÿ’–.


Copyright (c) 2004-2021 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 ๐Ÿ“œ. 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! ๐ŸŽ‰ It's the GFORTRAN(1) manual from the gcc-11 docs, last updated 2024-07-19, and you're officially a gfortran expert now ๐Ÿ’…โœจ periodt ๐Ÿฆ‹๐Ÿ’œ

๐Ÿ“ฆ where this page comes from