GFORTRAN(1)

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

๐ŸŽ€ GFORTRAN(1) โ€” the GNU Fortran compiler ๐ŸŽ€

Okay bestie, this is the manual page for GFORTRAN(1), and it's giving full compiler-nerd energy ๐Ÿค“๐Ÿ’– Let's go through it together!

โœจ NAME โœจ

Okay so the NAME part is short and sweet: gfortran is the GNU Fortran compiler ๐Ÿ’…

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

The basic shape of the command is this ๐Ÿ‘‡

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

Only the most useful options are listed here, so don't panic if you don't see everything. The rest is further down the page ๐Ÿฆ‹

๐ŸŒธ DESCRIPTION ๐ŸŒธ

The gfortran command is literally iconic because it supports all the options that gcc supports ๐ŸŒˆ This page only documents the stuff that's specific to GNU Fortran, so you're not getting a repeat of the GCC basics here.

All GCC and GNU Fortran options work with both gfortran and gcc, along with any other drivers built at the same time (like g++). The reason is that once GNU Fortran is part of the GCC distribution, all the relevant drivers accept GNU Fortran options. Main character energy ๐Ÿ‘‘

Some options come in a positive and a negative form. For example, the negative form of -ffoo is -fno-foo. This manual only documents one of the two, whichever one isn't the default โœจ

๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

Here's the vibe check on every option specific to GNU Fortran, grouped by type. The actual explanations come after the summary, so keep scrolling ๐Ÿ’•

๐ŸŒˆ Quick summary

๐Ÿ’– 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 that the compiler accepts ๐Ÿ’…

  • -ffree-form, -ffixed-form โ€” These pick the layout of your source file. Free form arrived with Fortran 90, while fixed form is what the older Fortran programs were written in. If you don't specify either one, the source form gets figured out from the file extension โœจ
  • -fall-intrinsics โ€” This makes every intrinsic procedure work, including the GNU-specific extensions ๐ŸŒˆ It's super useful alongside -std=f95 if you want standard compliance but still want the full range of intrinsics gfortran offers. The catch is that -Wintrinsics-std gets ignored, and no user-defined procedure with the same name as 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 definition or between different calls. That code is non-conforming and usually gets flagged with an error. This option downgrades that error to a warning, which you can only turn off by disabling all warnings with -w. Only a single occurrence per argument gets flagged. This is implied by -std=legacy ๐Ÿ™ˆ
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 downgrades that error to a warning and lets a BOZ literal constant appear where the Fortran standard would otherwise 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, they're treated as if the first column had a blank in it. With -fd-lines-as-comments, they're treated as comment lines ๐Ÿฆ‹
  • -fdec โ€” This is DEC compatibility mode ๐Ÿ•ฐ๏ธ It turns on extensions and other features that copy the default behavior of older compilers (like DEC's). These features are non-standard, so you should avoid them at all costs! For the details on how GNU Fortran implements these extensions, check the full documentation. The other flags 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/-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 (on top of %). It's only here for compatibility though, so Fortran 90 derived types are the better choice wherever you can use them ๐ŸŒธ
  • -fdec-intrinsic-ints โ€” This turns on the B/I/J/K kind variants of existing integer functions (e.g. BIAND, IIAND, JIAND, etc...). For the complete list of intrinsics, peep the full documentation ๐Ÿ“œ
  • -fdec-math โ€” This enables legacy math intrinsics like COTAN and the degree-valued trig functions (e.g. TAND, ATAND, etc...), so older code keeps working for compatibility ๐Ÿงฎ
  • -fdec-static โ€” This enables DEC-style STATIC and AUTOMATIC attributes, so you can explicitly say how variables and other objects are stored ๐Ÿ’พ
  • -fdec-include โ€” This lets INCLUDE be parsed as a statement in addition to the usual 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 โ€” This lets you use the format specifiers F, G, and I without width specifiers. The default widths get used instead ๐Ÿ“
  • -fdec-blank-format-item โ€” This enables a blank format item at the end of a format specification, i.e. nothing following the final comma ๐Ÿ™ˆ
  • -fdollar-ok โ€” This allows $ as a valid non-first character in a symbol name. Symbols that start with $ get 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, switching from a single backslash character to "C-style" escape characters ๐ŸŒˆ The combinations \a, \b, \f, \n, \r, \t, \v, \\, and \0 expand to the ASCII alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL characters, in that order. On top of that, the \x escape followed by nn, the \u escape followed by nnnn, and the \U escape followed by nnnnnnnn (where each n is a hexadecimal digit) get translated into the Unicode characters for those code points. Every other combination of a character preceded by \ stays unexpanded ๐Ÿฆ‹
  • -fmodule-private โ€” This sets the default accessibility of module entities to PRIVATE. Use-associated entities won't be accessible unless they're explicitly declared PUBLIC ๐Ÿ”’
  • -ffixed-line-length-n โ€” This sets the column after which characters are ignored in typical fixed-form lines in the source file. Unless -fno-pad-source is used, spaces are also assumed (as if 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 (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 padding 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 have implicit spaces appended to fill out the line ๐Ÿซถ
  • -ffree-line-length-n โ€” okay so this one sets the column where the compiler stops caring ๐Ÿ“. In typical free-form source files, anything past column n gets ignored. The default is 132 โœจ. You can pass none as the n value so the entire line counts ๐Ÿ’–. -ffree-line-length-0 does the exact same thing as -ffree-line-length-none.
  • -fmax-identifier-length=n โ€” sets the longest identifier 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 start of every procedure ๐Ÿ’….
  • -fcray-pointer โ€” turns on the Cray pointer extension, which gives you C-like pointer vibes ๐Ÿงš.
  • -fopenacc โ€” enables the OpenACC extensions ๐Ÿš€. That includes !$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 the c$, *$ and !$ sentinels in fixed form. When you link, it makes sure the OpenACC runtime library gets linked in too ๐Ÿ”—.
  • -fopenmp โ€” enables the OpenMP extensions ๐Ÿ”ฅ. That covers !$omp directives in free form, c$omp, *$omp and !$omp directives in fixed form, and the same !$, c$, *$ and !$ sentinels as above. Linking also arranges for the OpenMP runtime library to be linked in. Heads up, bestie: -fopenmp implies -frecursive ๐Ÿซถ.
  • -fno-range-check โ€” disables range checking on the results of simplifying constant expressions during compilation ๐Ÿ™ˆ. Normally GNU Fortran throws a compile-time error for something like a = 1. / 0. With this flag there's no error, 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. Also, DATA i/Z'FFFFFFFF'/ would overflow on most systems, but with this flag the value wraps around and i ends up as -1 ๐ŸŒ€.
  • -fdefault-integer-8 โ€” sets the default integer and logical types to an 8 byte wide type ๐Ÿ“. It also changes the kind of integer constants like 42. Unlike -finteger-4-integer-8, it won't promote variables that have an explicit kind declaration.
  • -fdefault-real-8 โ€” sets the default real type to an 8 byte wide type ๐Ÿ’œ. This also affects the kind of non-double real constants like 1.0. It promotes the default width of DOUBLE PRECISION and double real constants like 1.d0 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 ๐ŸŒˆ. This also affects the kind of non-double real constants like 1.0. It promotes the default width of DOUBLE PRECISION and double real constants like 1.d0 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 ๐Ÿ‘‘. This also affects the kind of non-double real constants like 1.0. It promotes the default width of DOUBLE PRECISION and double real constants like 1.d0 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 needs care and may not suit your codebase.
โš ๏ธ Be careful with this one, bestie! Possible trouble spots include calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O. It's suggested that you inspect the intermediate representation of the translated Fortran code, produced by -fdump-tree-original ๐Ÿซฃ.
  • -freal-4-real-8, -freal-4-real-10, -freal-4-real-16, -freal-8-real-4, -freal-8-real-10, -freal-8-real-16 โ€” these all promote every REAL(KIND=M) entity to REAL(KIND=N) ๐Ÿช„. If REAL(KIND=N) isn't available, you'll get an error. Every other real kind type is unaffected. Same deal as before: use with care, since these may not suit your code. The areas of concern are the same too: external procedure calls, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O. Inspecting the intermediate representation, produced by -fdump-tree-original, is suggested ๐Ÿ”.
  • -std=std โ€” picks the standard your program is expected to follow ๐Ÿ“œ. The options are f95, f2003, f2008, f2018, gnu, or legacy. The default is gnu, which is 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 in new code. legacy is the same, but without those obsolete-extension warnings, which makes it handy for old non-standard programs ๐Ÿซ . The f95, f2003, f2008 and f2018 values are strict: they conform to Fortran 95, Fortran 2003, Fortran 2008 and Fortran 2018 respectively. You'll get errors for any extension beyond the chosen standard, and warnings for Fortran 77 features that are still allowed but obsolescent in later standards.
๐Ÿ’€ The deprecated -std=f2008ts option is an alias for -std=f2018. It's only there for backwards compatibility with earlier gfortran versions, so it should not be used any more.
  • -ftest-forall-temp โ€” boosts test coverage by forcing most forall assignments to use a temporary ๐Ÿงช.

โœจ Enable and customize preprocessing โœจ

Preprocessor related options, bestie ๐ŸŽ€. For more detail, see the section Preprocessing and conditional compilation for everything about preprocessing in gfortran.

  • -cpp, -nocpp โ€” -cpp enables preprocessing ๐Ÿช„. The preprocessor gets invoked automatically if the file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03 or .F08. Use this option to manually enable preprocessing for any kind of Fortran file. To turn off preprocessing for files with any of those extensions, use the negative form, -nocpp ๐Ÿ™…โ€โ™€๏ธ. The preprocessor runs in traditional mode. Any file-format restrictions, especially the limits on line length, also apply to 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, this generates a list of #define directives for all the macros defined during the preprocessor run, including the predefined ones ๐Ÿ“‹. It's a great way to find out what's predefined in your version of the preprocessor ๐Ÿ”ฎ. Assuming you have no file foo.f90, this command:
touch foo.f90; gfortran -cpp -E -dM foo.f90

will show all the predefined macros ๐ŸŒธ.

  • -dD โ€” like -dM, except it doesn't include the predefined macros. It also 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 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 were tested but were undefined at the time.
  • -dI โ€” outputs #include directives in addition to the result of preprocessing ๐Ÿ“‚.
  • -fworking-directory โ€” enables linemarkers in the preprocessor output that tell the compiler the current working directory at the time of preprocessing ๐Ÿ—บ๏ธ. When this is on, the preprocessor emits a second linemarker after the initial one, containing the current working directory followed by two slashes. GCC will use this directory, when it's present in the preprocessed input, as the directory emitted as the current working directory in some debugging information formats. This 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 get 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 =, that = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐Ÿงญ.
  • -imultilib dir โ€” uses dir as a subdirectory of the directory containing target-specific C++ headers ๐ŸŽฏ.
  • -iprefix prefix โ€” sets prefix as the prefix for subsequent -iwithprefix options ๐Ÿท๏ธ. If the prefix represents a directory, you should include the final /.
  • -isysroot dir โ€” works like the --sysroot option, but only applies to header files ๐Ÿ“. See the --sysroot option for more information.
  • -iquote dir โ€” searches dir only for header files requested with #include "file" ๐Ÿ’ฌ. They're not searched for #include <file>. This happens before all the directories specified by -I and before the standard system directories. If dir begins with =, that = gets replaced by the sysroot prefix; see --sysroot and -isysroot โœจ.
  • -isystem dir โ€” okay bestie, this tells the compiler to hunt for header files in dir ๐Ÿ” It gets searched after every directory you gave with -I, but before the standard system directories. It's also marked as a system directory, so it gets the same VIP treatment as the standard ones ๐Ÿ‘‘ If dir starts with =, that = gets swapped out for the sysroot prefix. Check out --sysroot and -isysroot for the deets โœจ
  • -nostdinc โ€” no more snooping through the standard system directories for headers ๐Ÿ™…โ€โ™€๏ธ Only the directories you gave with -I options (plus the directory of the current file, if appropriate) get searched.
  • -undef โ€” don't predefine any system-specific or GCC-specific macros ๐Ÿงผ The standard predefined macros stay put, though.
  • -Apredicate=answer โ€” make an assertion with the predicate predicate and the answer answer ๐ŸŽ€ This is the fave form over the older -A predicate(answer), which still works but isn't as chill. This one doesn't use shell special characters, so it's giving clean vibes ๐Ÿ’…
  • -A-predicate=answer โ€” cancel an assertion with the predicate predicate and the answer answer ๐Ÿ™…
  • -C โ€” don't toss the comments! All comments get passed through to the output file, except comments in processed directives, which get deleted along with the directive ๐Ÿ’ฌ

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

Warning: this currently handles C-Style comments only. The preprocessor does not yet recognize Fortran-style comments. ๐Ÿ™ˆ
  • -CC โ€” don't discard comments, even 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 side effects of -C, the -CC option 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 โ€” predefine 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: the definition gets truncated by embedded newline characters.

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

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

-D and -U options are processed in the order they're given on the command line. All -imacros file and -include file options get processed after all -D and -U options ๐Ÿ“‹

  • -H โ€” print the name of each header file used, along with the other normal stuff ๐Ÿ“œ Each name is indented to show how deep it sits in the #include stack ๐Ÿชœ
  • -P โ€” inhibit generation of linemarkers in the preprocessor output ๐Ÿšซ This might be useful when you run the preprocessor on something that isn't C code and will be sent to a program that could get confused by the linemarkers ๐Ÿซถ
  • -Uname โ€” cancel any previous definition of name, whether it was built in or provided with a -D option ๐Ÿ—‘๏ธ

๐Ÿฆ‹ Options to request or suppress errors and warnings ๐Ÿฆ‹

Errors are diagnostic messages saying the GNU Fortran compiler can't compile the relevant piece of source code ๐Ÿ˜ญ The compiler keeps processing the program to report further errors and help you debug, but it won't produce any compiled output.

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

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

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

  • -fmax-errors=n โ€” caps the number of error messages at n, and then GNU Fortran bails instead of trying to keep going ๐Ÿ›‘ 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 ๐Ÿ“ It'll generate module files for each module present in the code, but no other output file.
  • -Wpedantic, -pedantic โ€” issue warnings for uses of extensions to Fortran ๐Ÿง -pedantic also applies to C-language constructs where they occur in GNU Fortran source files, like use of \e in a character constant within a directive like "#include".

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

Some users try to use -pedantic to check programs for conformance. They soon find it doesn't do quite what they want, since it finds some nonstandard practices but not all ๐Ÿ˜ฉ Still, improvements to GNU Fortran in this area are welcome ๐Ÿ’œ

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

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

The following example will trigger the warning.

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

call bar(a,a)
  • -Wampersand โ€” warns about a missing ampersand in continued character constants โœ‚๏ธ The warning is given with -Wampersand, -pedantic, -std=f95, -std=f2003, -std=f2008 and -std=f2018.
Note: With no ampersand given in a continued character constant, GNU Fortran assumes continuation at the first non-comment, non-whitespace character after the ampersand that initiated the continuation. ๐Ÿง
  • -Warray-temporaries โ€” warns about array temporaries generated by the compiler ๐ŸŒ€ The information from this warning is sometimes useful in optimization, in order to avoid such temporaries.
  • -Wc-binding-type โ€” warns if a variable might not be C interoperable ๐Ÿ”— In particular, it warns if the variable has been declared using an intrinsic type with default kind instead of 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 error ๐Ÿ˜ฑ
  • -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 for performing 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 ๐Ÿ• For example, 3/5 evaluates to 0.
  • -Wintrinsics-std โ€” warns if gfortran finds a procedure named like an intrinsic that isn't available in the currently selected standard (with -std), and treats it as an "EXTERNAL" procedure because of this ๐Ÿ•ต๏ธ -fall-intrinsics can be used to never trigger this behavior and always link to the intrinsic regardless of the selected standard.
  • -Wno-overwrite-recursive โ€” don't 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 be used to suppress the warning when it's known that recursion is not broken. Useful for build environments that use -Werror ๐Ÿ› ๏ธ
  • -Wreal-q-constant โ€” produces a warning if a real-literal-constant contains a "q" exponent-letter ๐Ÿ’œ
  • -Wsurprising: ๐Ÿ‘€ Heads up, bestie! This flags "suspicious" code constructs. They're technically legal, but they usually mean someone messed up, so lowkey double-check. It currently 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 specifies 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 is an error ๐Ÿ’€.
    • A CHARACTER variable is declared with a negative length.
  • -Wtabs: ๐Ÿ“ Tabs are accepted as whitespace by default, but they're not part of the Fortran Character Set, so they're kinda 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. Also, -Wtabs is active for -pedantic, -std=f95, -std=f2003, -std=f2008, -std=f2018 and -Wall.
  • -Wundefined-do-loop: Warns if a DO loop with step either 1 or -1 hits an underflow or an overflow while its induction variable is iterating. Implied by -Wall.
  • -Wunderflow: Warns when numerical constant expressions yield an UNDERFLOW during compilation. Enabled by default โœจ.
  • -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 procedure you actually wanted ๐ŸŽฏ. Implied by -Wall.
  • -Wuse-without-only: Warns if a USE statement has no ONLY qualifier, which means it quietly imports every public entity of the module.
  • -Wunused-dummy-argument: Warns about unused dummy arguments. Implied by -Wall.
  • -Wunused-parameter: Plot twist ๐Ÿ™ˆ! Unlike gcc, gfortran's version does not warn about unused dummy arguments (for that, see -Wunused-dummy-argument). It warns about unused PARAMETER values instead. Implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons: By default, gfortran warns whenever variables get padded for 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 enabled by -ffrontend-optimize. Implied by -Wextra.
  • -Wrealloc-lhs: Warns when the compiler might insert allocation or reallocation code for 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 correct shape, use a whole-array array-spec (e.g. (:,:,:)) on the left-hand side to skip the reallocation check. Note that sometimes this warning shows up even when the compiler would optimize the checks away anyway, like when the right-hand side has the same variable multiplied by a scalar. See also -frealloc-lhs.
  • -Wrealloc-lhs-all: Warns when the compiler inserts allocation or reallocation code for an allocatable variable. This includes scalars and derived types.
  • -Wcompare-reals: Warns when real or complex types get compared 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 at compile time to execute zero times. 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. Here's the kind of situation it's talking about ๐Ÿง:
real a(3)
do i=1,4
  if (condition(i)) then
    a(i) = 1.2
  end if
end do

This option 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. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and this option 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 generate internal compiler errors for features that were only recently added.
  • -fdump-parse-tree: Outputs the internal parse tree after the source program gets translated into internal 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. This option is deprecated, so use -fdump-fortran-original instead โš ๏ธ.
  • -fdump-fortran-global: Outputs a list of the global identifiers after translating into middle-end representation. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and it may also generate internal compiler errors for features that were only recently added.
  • -ffpe-trap=list: Enables a list of floating point exception traps. On most systems, if a floating point exception happens and its trap is enabled, a SIGFPE signal gets sent and the program is aborted, 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 match the five IEEE 754 exceptions. The last one, denormal, isn't part of IEEE 754, but it's available on some common architectures, like x86.

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

If the option is used more than once on the command line, the lists get joined. So -ffpe-trap=list1 plus -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, so choose wisely ๐Ÿง .

Lots of floating point operations lose precision due to rounding, so -ffpe-trap=inexact is likely to be 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 or ERROR STOP is invoked ๐Ÿ“. list can be none, all, or a comma-separated list of invalid, zero, overflow, underflow, inexact and denormal. (See -ffpe-trap for what each exception means.)

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 shows up (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 backtrace generation off. It only has an effect when compiling the Fortran main program.

These options affect how GNU Fortran searches for files specified by the INCLUDE directive, and where it searches for previously compiled modules ๐Ÿ—‚๏ธ.

They also affect the search paths used by cpp when it preprocesses Fortran source.

  • -Idir: These affect how the INCLUDE directive gets interpreted, as well as the #include directive of the cpp preprocessor. The general behavior of -I and INCLUDE is pretty much the same as -I with #include in cpp, when it comes to looking for header.gcc files and other such things ๐Ÿ”Ž. This path is also used to search for .mod files when previously compiled modules are required by a USE statement.
  • -Jdir: Specifies where to put .mod files for compiled modules. It also gets added to the list of directories searched by a USE statement. The default is the current directory ๐Ÿ“.
  • -fintrinsic-modules-path dir: Specifies the location of pre-compiled intrinsic modules, if they aren't in the default location the compiler expects.

โœจ Influencing the linking step โœจ

These options come into play 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 so this one decides how data gets represented in unformatted files ๐Ÿ’– The valid values for conversion are native (the default ๐ŸŒธ), swap (flips between big- and little-endian ๐Ÿ”„), big-endian (uses big-endian representation for unformatted files), and little-endian (uses little-endian representation for unformatted files).

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

  • -frecord-marker=length โ€” this sets the length of record markers for unformatted files ๐Ÿฆ‹. The valid values for length are 4 and 8, and the default is 4. Be warned that this is different from older versions of gfortran, which defaulted to a record marker length of 8 on most systems ๐Ÿ™ˆ. If you need 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 biggest allowed value for length is 2147483639, which is also the default ๐Ÿ’œ. It's only really useful for the gfortran testsuite, so don't stress about it ๐Ÿงš.
  • -fsign-zero โ€” when this is enabled, zero-valued floats with the sign bit set get written as negative numbers in formatted output, and they're treated as negative by 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 treats zero as positive in the SIGN intrinsic ๐ŸŒˆ. That's 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 ๐Ÿฆ‹. Below, only one of the forms is listed (the one that is not the default), so you can figure out the other by removing no- or adding it ๐Ÿ“.

  • -fno-automatic โ€” treats each program unit (except the ones marked as RECURSIVE) as if the SAVE statement were specified for every local variable and array in it ๐Ÿ”’. It doesn't affect common blocks. (Some Fortran compilers give this option the name -static or -save ๐ŸŒธ.) The default, -fautomatic, uses the stack for local variables that are smaller than the value given by -fmax-stack-var-size ๐Ÿฅž. If you want to use no static memory at all, use the option -frecursive ๐Ÿ’œ.

Local variables or arrays with an explicit SAVE attribute are silently ignored, unless the -pedantic option is added ๐Ÿ™ˆ.

  • -ff2c โ€” generates code that's compatible with code generated by g77 and f2c ๐Ÿค.

The calling conventions used by g77 (originally from f2c) require functions that return default REAL to actually return the C type double, and functions that return type COMPLEX to return their values through an extra argument in the calling sequence. That extra argument points to where the return value gets stored ๐Ÿ“. Under the default GNU calling conventions, these 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 ๐Ÿง. On top of that, this option implies -fsecond-underscore, unless -fno-second-underscore is explicitly requested โœจ.

This doesn't affect the generation of code that interfaces with the libgfortran library.

Caution: mixing Fortran code compiled with -ff2c and code compiled with the default -fno-f2c calling conventions is a really bad idea ๐Ÿ’€. Calling COMPLEX or default REAL functions between program parts compiled with different calling conventions will break at execution time ๐Ÿ˜ญ.
Caution: this will break code that passes intrinsic functions of type default REAL or COMPLEX as actual arguments ๐Ÿšจ, because the library implementations use the -fno-f2c calling conventions ๐Ÿ’”.
  • -fno-underscoring โ€” stops GNU Fortran from transforming the names of entities in your Fortran source file by appending underscores to them ๐Ÿ™…โ€โ™€๏ธ.

With -funderscoring in effect, GNU Fortran appends one underscore to external names that have no underscores already ๐ŸŒŸ. This is done so it stays compatible with code produced by many UNIX Fortran compilers ๐Ÿซถ.

Caution: the default behavior of GNU Fortran is incompatible with f2c and g77 ๐Ÿ‘€. If you want object files compiled with GNU Fortran to be compatible with object code created by those tools, please use the -ff2c option ๐Ÿ’–.

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

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

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

gets implemented as something akin to this:

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

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

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

Using -fno-underscoring lets you specify user-defined names directly while debugging, and when you're interfacing GNU Fortran code with other languages ๐Ÿ”.

Just because the names match, though, doesn't mean the interface GNU Fortran implements for an external name matches the interface some other language implements for that same name ๐Ÿ™ƒ. Getting GNU Fortran code to link with code from another compiler (using this or any other method) can be only a small part of the overall solution ๐Ÿงฉ. Getting the code from both compilers to agree on things other than naming can take serious effort, and, unlike naming disagreements, linkers normally can't detect disagreements in those other areas ๐Ÿ˜ค.

Also, with -fno-underscoring, the lack of appended underscores makes it very real that a user-defined external name could conflict with a name in a system library โš ๏ธ. That could make finding unresolved-reference bugs super hard, since they might only happen at run time and show up as buggy behavior ๐Ÿ’€.

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 ๐Ÿ”ฎ. This would be true even if the names 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 you use 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 that have 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 ๐ŸŽ€.

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> โ€” picks how coarray support works, using one of these keywords ๐Ÿงš:
    • 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 1๏ธโƒฃ.
    • 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 has to be a comma-delimited list of the keywords below. Putting no- in front of a check disables it, if it was turned on by a previous specification ๐Ÿ™…โ€โ™€๏ธ.
    • all โ€” enables all run-time tests of -fcheck ๐ŸŒˆ.
    • array-temps โ€” warns at run time when, to pass an actual argument, a temporary array had to be generated ๐Ÿ“ฃ. The info from this warning is sometimes useful for optimization, since you can use it to 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 it makes sure all string lengths are equal for character array constructors that don't have an explicit typespec ๐Ÿงต. Some checks require that -fcheck=bounds is set for the compilation of the main program ๐Ÿ. Note: this may also include other forms of checking in the future, 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 does not affect explicit allocations using the ALLOCATE statement, which are always checked ๐Ÿ’ฏ.
    • 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 it's used together with -frecursive and -fopenmp ๐Ÿ™ˆ.

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

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

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

  • -fbounds-check โ€” a deprecated alias for -fcheck=bounds ๐Ÿ™…โ€โ™€๏ธ. Use -fcheck=bounds instead ๐Ÿ’–.
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” some C interfaces to Fortran code violate the gfortran ABI by omitting the hidden character length arguments, as described in (the original page just trails off right there ๐Ÿ’€, so there's no reference to chase down ๐Ÿ™ˆ).
Heads up: this can lead to crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿšจ.

Okay so this is the rest of the -ftail-call-workaround tea ๐Ÿซ–. It's a band-aid for existing binary packages ๐Ÿฉน, and it turns off tail call optimization for gfortran procedures with character arguments. -ftail-call-workaround=2 does it in every gfortran procedure with character arguments, while -ftail-call-workaround=1 (or plain -ftail-call-workaround, which is equivalent) only does it 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. Bestie, be careful!

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, so you can include them 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, switches this off ๐Ÿฆ‹.

Default is currently -ftail-call-workaround, and this will change in future releases.


โœจ OPTIONS โœจ

  • -fcheck-array-temporaries โ€” โš ๏ธ deprecated, but still works as an alias for -fcheck=array-temps ๐Ÿ•ฐ๏ธ.
  • -fmax-array-constructor=n โ€” this is how you raise the upper limit allowed in array constructors ๐Ÿ“ˆ. The code below needs it so the array can be expanded 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 โ€” sets the size in bytes of the biggest array allowed on the stack ๐Ÿ“ฆ. If an array goes over that size, it gets static memory instead (except in procedures marked RECURSIVE) ๐ŸงŠ. Use -frecursive if you want recursive procedures that don't have the RECURSIVE attribute, or if you're doing parallel programs ๐Ÿงต. Use -fno-automatic if you never want the stack touched ๐Ÿšซ. 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, so stay tuned ๐Ÿ‘€. The default value for n is 65536.
  • -fstack-arrays โ€” puts all arrays of unknown size and array temporaries onto stack memory ๐Ÿฅž. If your program uses very large local arrays, you might have to extend your runtime stack memory limits 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 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 ๐Ÿš€, but it can add a lot of overhead to the function call, especially when the passed data is noncontiguous ๐Ÿ˜ตโ€๐Ÿ’ซ.
  • -fshort-enums โ€” this is 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 dummy, or to a procedure without an explicit interface, the argument may have to be packed (put into contiguous memory) ๐Ÿ“ฆโœจ. Here's the classic example, with the call to foo in bar:
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 with inline code ๐Ÿง‘โ€๐Ÿณ. That allows more optimization, but the code gets bigger ๐Ÿ“ˆ. This is implied by any of the -O options, except when you optimize for size with -Os ๐Ÿค. If your code has a very large number of arguments that need packing, the code size and compile time can get out of hand ๐Ÿซ . If that happens, it might be better to disable this option. You can find the instances of packing by using -Warray-temporaries ๐Ÿ”.

  • -fexternal-blas โ€” makes gfortran call BLAS functions for some matrix operations, like MATMUL, instead of its own algorithms ๐Ÿงฎ. This only happens if the matrices involved are larger than a given limit (see -fblas-matmul-limit) ๐Ÿ“. This can be a total 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 with matrices of size larger than or equal to n will go through BLAS calls, and everything smaller gets 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 make your code bigger if the matrix size can't be determined at compile time, because code for both cases gets generated ๐Ÿฑ. Setting -finline-matmul-limit=0 disables inlining in all cases ๐Ÿ™…โ€โ™€๏ธ. Setting it to a value of n produces inline code for matrices 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, and -fblas-matmul-limit can be used to change this value ๐Ÿ”„.
  • -frecursive โ€” allows indirect recursion by forcing all local arrays to be allocated on the stack ๐ŸŒ€. This flag cannot be used together with -fmax-stack-var-size= or -fno-automatic ๐Ÿšซ.
  • -finit-local-zero, -finit-derived, -finit-integer=n, -finit-real=<zero|inf|-inf|nan|snan>, -finit-logical=<true|false>, -finit-character=n โ€” the initialization crew ๐ŸŒฑ. -finit-local-zero tells the compiler to set local INTEGER, REAL, and COMPLEX variables to zero, LOGICAL variables to false, and CHARACTER variables to a string of null bytes ๐Ÿซง. For finer control, there's -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 convert those into quiet NaNs, 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 then gets 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 the 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 can be used to turn automatic alignment off ๐Ÿ™…. Use the same form of this option for all files that share a COMMON block ๐Ÿค. To dodge 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 every optimization level, so the compiler doesn't do any re-association ๐Ÿ™Œ. Using -fno-protect-parens lets the compiler reorder REAL and COMPLEX expressions to produce faster code ๐ŸŽ๏ธ. For the re-association optimization, -fno-signed-zeros and -fno-trapping-math need to be in effect too ๐Ÿงพ. The parentheses protection is enabled by default, unless -Ofast is given ๐Ÿ’….
  • -frealloc-lhs โ€” an allocatable left-hand side of an intrinsic assignment gets automatically (re)allocated if it's either unallocated or has a different shape ๐Ÿช„. 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 those functions are 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 โ€” performs front-end optimization, based on manipulating parts of the Fortran parse tree ๐ŸŒณ. It's enabled by default by any -O option except -O0 and -Og ๐Ÿ’–. Optimizations enabled by this option include:

Okay so the front end does a bunch of cleanup for you, and it's giving efficiency ๐Ÿ’…โœจ

  • 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.). ๐Ÿฆ‹

If you ever want it to chill and stop doing all that, just pass -fno-frontend-optimize. Your call, bestie ๐Ÿซถ


โœจ -ffrontend-loop-interchange โœจ

  • -ffrontend-loop-interchange โ€” this one tries to swap loops around in the Fortran front end when 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 specific, no cap ๐Ÿ’–

๐ŸŒธ ENVIRONMENT ๐ŸŒธ

Honestly, the gfortran compiler doesn't read any environment variables to control how it runs, apart from the ones that already affect gcc. So if you've been hunting for a secret gfortran env var, sorry bestie, it's just gcc vibes ๐Ÿ™ˆ

๐Ÿ› BUGS ๐Ÿ›

Want to report a bug? The instructions are in file:///usr/share/doc/gcc-10/README.Bugs. Go read them and help us squash it ๐Ÿชฒ๐Ÿ’œ

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

Here's your reading list, the whole squad ๐Ÿ“š

๐Ÿ‘‘ 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. ๐ŸŒˆ

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

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

A GNU Manual

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

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

And that's a wrap, bestie! ๐ŸŽ€ This whole page was gcc-10 energy, dated 2023-07-07, and it's officially GFORTRAN(1) certified ๐Ÿ’…๐Ÿ’–

๐Ÿ“ฆ where this page comes from