GFORTRAN(1)

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

Okay bestie, gather round because we're doing GFORTRAN(1) today ๐Ÿ’– It's the GNU manual page for the gfortran compiler and it is lowkey iconic ๐Ÿงš

โœจ NAME โœจ

gfortran - the GNU Fortran compiler ๐Ÿฆ‹

๐Ÿ“œ SYNOPSIS ๐Ÿ“œ

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

Only the most main-character options are listed in that synopsis ๐Ÿ’… The rest are down below, so keep reading!

๐ŸŒธ DESCRIPTION ๐ŸŒธ

The gfortran command is giving gcc energy. It supports all the options that gcc supports, so you already know a lot of it โœจ Only the options specific to GNU Fortran get documented here.

Every GCC and GNU Fortran option is accepted by both gfortran and gcc, plus any other drivers built at the same time, like g++. Adding GNU Fortran to the GCC distribution means all the relevant drivers accept GNU Fortran options. Slay ๐Ÿ‘‘

Some options come in a positive and a negative flavor. The negative form of -ffoo is -fno-foo. This manual only documents one of the two, whichever one is not the default ๐ŸŽ€

โœจ OPTIONS โœจ

Here is a summary of all the options specific to GNU Fortran, grouped by type. The explanations come in the following sections ๐Ÿ’–

๐Ÿ’– Fortran Language Options

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

๐Ÿงš Preprocessing Options

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

๐Ÿ™ˆ Error and Warning Options

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

๐Ÿ” Debugging Options

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

๐Ÿ“‚ Directory Options

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

โฑ๏ธ Runtime Options

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

๐Ÿค Interoperability Options

-fc-prototypes -fc-prototypes-external

๐Ÿ› ๏ธ Code Generation Options

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

๐ŸŽ€ Options controlling Fortran dialect

The following options control the details of the Fortran dialect the compiler accepts ๐Ÿ’…

  • -ffree-form / -ffixed-form โ€” pick the layout your source file uses. Free form showed up in Fortran 90, while fixed form was the traditional choice for older Fortran programs. If you pass neither one, the source form gets figured out from the file extension ๐Ÿงš
  • -fall-intrinsics โ€” makes every intrinsic procedure acceptable, including the GNU-specific extensions. This can be useful with -std=f95 if you want standard compliance but still want the full range of gfortran intrinsics. The catch: -Wintrinsics-std gets ignored, and no user-defined procedure with the same name as an intrinsic will be called, except when it's explicitly declared EXTERNAL.
  • -fd-lines-as-code / -fd-lines-as-comments โ€” special treatment for lines starting with d or D in fixed form sources. With -fd-lines-as-code, they're treated as if the first column held a blank. With -fd-lines-as-comments, they're treated as comment lines.
  • -fdec โ€” DEC compatibility mode ๐Ÿ™ˆ It turns on extensions and other features that mimic the default behavior of older compilers, like DEC's. For the details on GNU Fortran's version of these extensions, check the full documentation.
    • Other flags this switch turns on: -fdollar-ok -fcray-pointer -fdec-structure -fdec-intrinsic-ints -fdec-static -fdec-math
    • If -fd-lines-as-code/-fd-lines-as-comments are unset, then -fdec also sets -fd-lines-as-comments.
๐Ÿšจ Warning: these features are non-standard and should be avoided at all costs, bestie!
  • -fdec-structure โ€” enables DEC "STRUCTURE" and "RECORD", plus "UNION", "MAP", and the dot ('.') as a member separator (in addition to '%'). This is only here for compatibility. Fortran 90 derived types should be used instead wherever possible ๐Ÿ’œ
  • -fdec-intrinsic-ints โ€” enables B/I/J/K kind variants of existing integer functions, like BIAND, IIAND, JIAND, etc... For a complete list of intrinsics, see the full documentation.
  • -fdec-math โ€” enables legacy math intrinsics such as COTAN and degree-valued trigonometric functions (like TAND, ATAND, etc...) for compatibility with older code.
  • -fdec-static โ€” enables DEC-style STATIC and AUTOMATIC attributes, which explicitly specify the storage of variables and other objects.
  • -fdec-include โ€” enables parsing INCLUDE as a statement, in addition to parsing it as an INCLUDE line. When it's parsed as a statement, INCLUDE doesn't have to sit on a single line and can use line continuations.
  • -fdollar-ok โ€” allows $ as a valid non-first character in a symbol name. Symbols that start with $ are still rejected, since it's unclear which implicit typing rules to apply (different vendors implement different rules). Using $ in IMPLICIT statements is rejected too.
  • -fbackslash โ€” changes how backslashes in string literals are read. Instead of a single backslash character, you get C-style escape characters. These expand: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\\", and "\0" into the ASCII characters alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL, respectively. On top of that, "\x"nn, "\u"nnnn, and "\U"nnnnnnnn (where each n is a hexadecimal digit) get translated into the Unicode characters for those code points. Any other combination of a character preceded by \ stays unexpanded.
  • -fmodule-private โ€” sets the default accessibility of module entities to "PRIVATE". Use-associated entities won't be accessible unless they're explicitly declared "PUBLIC" ๐Ÿฆ‹
  • -ffixed-line-length-n โ€” sets the column after which characters get ignored in typical fixed-form lines in the source file. Unless -fno-pad-source is used, it also sets the column through which spaces are assumed (as if padded to that length) after the ends of short fixed-form lines.
    • Popular values for n include 72 (the standard and the default), 80 (card image), and 132 (corresponding to "extended-source" options in some popular compilers).
    • n may also be none, meaning the entire line is meaningful, and continued character constants never get 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 -ffixed-line-length-0, -ffixed-line-length-none, or -fno-pad-source is used. With any of those options, continued character constants never get implicit spaces appended to fill out the line.
  • -ffree-line-length-n โ€” sets the column after which characters get ignored in typical free-form lines in the source file. The default value is 132. n may be none, meaning the entire line is meaningful. -ffree-line-length-0 means the same thing as -ffree-line-length-none.
  • -fmax-identifier-length=n โ€” specifies the maximum allowed identifier length. Typical values are 31 (Fortran 95) and 63 (Fortran 2003 and Fortran 2008) โœจ
  • -fimplicit-none โ€” no implicit typing is allowed, unless it's overridden by explicit "IMPLICIT" statements. This is the equivalent of adding "implicit none" to the start of every procedure.
  • -fcray-pointer โ€” enables the Cray pointer extension, which provides C-like pointer functionality ๐Ÿงš
  • -fopenacc โ€” enables the OpenACC extensions. That includes OpenACC "!$acc" directives in free form, and "c$acc", *$acc, and "!$acc" directives in fixed form. It also covers "!$" conditional compilation sentinels in free form, and "c$", "*$", and "!$" sentinels in fixed form. When linking, it arranges for the OpenACC runtime library to be linked in.
โš ๏ธ Heads up: this is an experimental feature, incomplete, and subject to change in future versions of GCC. See https://gcc.gnu.org/wiki/OpenACC for more information.
  • -fopenmp โ€” enables the OpenMP extensions. That includes OpenMP "!$omp" directives in free form, and "c$omp", *$omp, and "!$omp" directives in fixed form. It also covers "!$" conditional compilation sentinels in free form, and "c$", "*$", and "!$" sentinels in fixed form. When linking, it arranges for the OpenMP runtime library to be linked in. Also, the option -fopenmp implies -frecursive ๐Ÿ’–
  • -fno-range-check โ€” okay bestie, this one lets you turn off the range police ๐Ÿšจ During compilation, GNU Fortran normally checks the results of simplifying constant expressions and throws an error for stuff like a = 1. / 0. With this flag, no error shows up and a just gets +Infinity ๐Ÿ’–. If an expression lands outside the relevant range of ["-HUGE()":"HUGE()"], it gets replaced with -Inf or +Inf, whichever fits โœจ. Same vibe with DATA i/Z'FFFFFFFF'/: that usually causes an integer overflow on most systems, but with -fno-range-check the value wraps around and i gets initialized to -1 instead ๐Ÿ™ˆ.
  • -fdefault-integer-8 โ€” sets the default integer and logical types to an 8 byte wide type ๐Ÿ’œ It also changes the kind of integer constants like 42. Unlike -finteger-4-integer-8, it does not promote variables that have an explicit kind declaration ๐ŸŽ€.
  • -fdefault-real-8 โ€” sets the default real type to an 8 byte wide type ๐Ÿฆ‹ It also changes the kind of non-double real constants like 1.0. This one bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible ๐Ÿ‘‘. But if you pass -fdefault-double-8 along with fdefault-real-8, then DOUBLE PRECISION and double real constants don't get promoted ๐Ÿซถ. Unlike -freal-4-real-8, fdefault-real-8 does not promote variables with explicit kind declarations ๐Ÿ’….
  • -fdefault-real-10 โ€” sets the default real type to a 10 byte wide type ๐ŸŒˆ It also changes the kind of non-double real constants like 1.0. It bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible โœจ. If you give -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 does not promote variables with explicit kind declarations ๐Ÿ’–.
  • -fdefault-real-16 โ€” sets the default real type to a 16 byte wide type ๐ŸŒธ It also changes the kind of non-double real constants like 1.0. It bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if possible ๐Ÿ’ซ. If you pair it with -fdefault-double-8, 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 one also 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 option should be used with care and may not be suitable for your codes! Areas of possible concern 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 six are a whole family of promotion options ๐Ÿฉต Each one promotes all REAL(KIND=M) entities to REAL(KIND=N) entities, depending on which pair you pick. If REAL(KIND=N) isn't available, you'll get an error. Every other real kind type is left alone ๐ŸŒท
โš ๏ธ These options should be used with care and may not be suitable for your codes! Same vibe as before: watch out for 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 ๐Ÿ’€
  • -std=std โ€” this is where you tell the compiler which standard your program is supposed to follow ๐Ÿ“œ std can be one of f95, f2003, f2008, f2018, gnu, or legacy.
    • gnu is the default ๐Ÿ’œ It's a superset of the latest Fortran standard that includes all the extensions GNU Fortran supports. You'll still get warnings for obsolete extensions that aren't recommended in new code.
    • legacy is basically the same as gnu, but without the warnings for obsolete extensions. It may be handy for old non-standard programs ๐Ÿง“
    • f95, f2003, f2008, and f2018 are 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 Fortran 77 features that are permitted 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 please don't use it anymore ๐Ÿ˜ญ
  • -ftest-forall-temp โ€” this one's for test coverage ๐Ÿงช It enhances test coverage by forcing most forall assignments to use a temporary ๐Ÿ”ฌ

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

Okay so these are all the preprocessor related options ๐Ÿงš Check out section Preprocessing and conditional compilation for more detailed information on preprocessing in gfortran ๐Ÿ“–

  • -cpp, -nocpp โ€” the preprocessor is the main character here ๐Ÿ’…
    • -cpp turns preprocessing on. It gets invoked automatically if the file extension is .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03, or .F08. Use this one to manually enable preprocessing for any kind of Fortran file ๐Ÿซถ
    • -nocpp is the negative form, and it's how you disable preprocessing for files with any of the extensions listed above ๐Ÿšซ
    • The preprocessor runs in traditional mode โœจ Any restrictions of the file format, especially the limits on line length, apply to preprocessed output too. So it might be advisable to use the -ffree-line-length-none or -ffixed-line-length-none options ๐Ÿ’–
  • -dM โ€” instead of the normal output, this spits out a list of #define directives for all the macros defined while the preprocessor was running, 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, but with two differences ๐Ÿฉต 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 use or test of the macro. Also, #undef directives are 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 โ€” this one helps the compiler know where you were working ๐Ÿ—บ๏ธ It enables generation of linemarkers in the preprocessor output, letting the compiler know the current working directory at the time of preprocessing. When it's on, the preprocessor emits a second linemarker after the initial one, with the current working directory followed by two slashes ๐Ÿ’ซ GCC uses that directory, when it's present in the preprocessed input, as the directory emitted 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 in the command line, this option does nothing, since no #line directives get emitted whatsoever ๐Ÿฅฒ
  • -idirafter dir โ€” searches dir for include files, but only after all the 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 the = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐Ÿ‘‘
  • -imultilib dir โ€” uses dir as a subdirectory of the directory containing target-specific C++ headers ๐Ÿงฉ
  • -iprefix prefix โ€” specifies prefix as the prefix for subsequent -iwithprefix options ๐ŸŽฏ If the prefix represents a directory, you should include the final / ๐Ÿ“
  • -isysroot dir โ€” this is like the --sysroot option, but it 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 directories specified by -I and before the standard system directories ๐Ÿšช If dir begins with =, then the = gets replaced by the sysroot prefix; see --sysroot and -isysroot โœจ
  • -isystem dir โ€” searches dir for header files, after all directories specified by -I but before the standard system directories ๐Ÿซง It gets marked as a system directory, so it gets the same special treatment as the standard system directories ๐Ÿฅฐ If dir begins with =, then the = gets replaced by the sysroot prefix; see --sysroot and -isysroot ๐ŸŒท
  • -nostdinc โ€” no standard system directories for header files, bestie ๐Ÿ™… Only the directories you've specified with -I options (and the directory of the current file, if appropriate) are searched ๐Ÿ”Ž
  • -undef โ€” doesn't predefine any system-specific or GCC-specific macros ๐Ÿงผ The standard predefined macros stay defined ๐Ÿ’œ
  • -Apredicate=answer โ€” makes an assertion with the predicate predicate and the answer answer ๐ŸŽ‰ This form is preferred over the older form -A predicate(answer), which is still supported, because it doesn't use shell special characters ๐Ÿš
  • -A-predicate=answer โ€” cancels an assertion with the predicate predicate and the answer answer ๐Ÿšซ๐Ÿ’–
  • -C โ€” do not discard comments ๐Ÿ’œ All comments are passed through to the output file, except for comments in processed directives, which get deleted along with the directive ๐Ÿ—‘๏ธ

Okay so the big thing about -C is that it comes with side effects ๐Ÿ™ˆ It makes the preprocessor treat comments as tokens in their own right. That means a comment at the start of what would be a directive line turns the whole line into an ordinary source line, because the first token is no longer a # ๐Ÿ’€ Wild, right?

Warning: this currently handles C-style comments only. The preprocessor does not yet recognize Fortran-style comments. ๐Ÿ˜ญ
  • -CC โ€” Don't discard comments, not even during macro expansion ๐Ÿซถ This is basically -C, except comments sitting inside a macro also get passed through to the output file wherever that macro gets expanded.

Besides the -C side effects, -CC converts all C++-style comments inside a macro into C-style comments. The reason is bestie-protective: a later use of that macro can't accidentally comment out the rest of the source line ๐Ÿ™… Lowkey genius. -CC is generally used to support lint comments โœจ

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

If you're calling the preprocessor from a shell (or something shell-like), you may need the shell's quoting syntax to protect characters like spaces that mean something to the shell ๐Ÿš

Want a function-like macro on the command line? Write its argument list in surrounding parentheses before the equals sign (if there is one) ๐Ÿ’œ 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 get processed in the order you give them on the command line. All -imacros file and -include file options get processed after all -D and -U options ๐ŸŽ€

  • -H โ€” Prints the name of each header file used, on top of all the other normal stuff ๐Ÿฆ‹ Each name gets indented to show how deep it is in the #include stack.
  • -P โ€” Inhibits generation of linemarkers in the output from the preprocessor ๐Ÿงผ This might be useful when you're running the preprocessor on something that isn't C code and is going 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 ๐Ÿšจ

Errors are diagnostic messages that report that the GNU Fortran compiler cannot compile the relevant piece of source code. The compiler keeps going so it can report more errors and help you debug, but it won't produce any compiled output ๐Ÿ˜ค

Warnings are diagnostic messages that report constructions which aren't inherently erroneous but are risky, or that suggest there's probably a bug in the program. Unless -Werror is specified, they don't stop the program from compiling ๐Ÿ’…

You can request lots of specific warnings with options that begin with -W. For example, -Wimplicit asks for warnings on implicit declarations. Each specific warning option also has a negative form beginning with -Wno- that turns the warnings off, like -Wno-implicit. This manual only lists one of the two forms, whichever one isn't the default โœจ

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

  • -fmax-errors=n โ€” Limits the maximum number of error messages to n. Once you hit that number, 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 where they show up in GNU Fortran source files, like using \e in a character constant inside a directive such as "#include".

Valid Fortran programs should compile properly with or without this option. Without it, 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, and then they're like "wait, this isn't catching everything." It finds some nonstandard practices, but not all of them. Improvements to GNU Fortran in this area are still very welcome ๐Ÿซถ

This should be used together 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 started the continuation.
  • -Wargument-mismatch โ€” Warns about type, rank, and other mismatches between formal parameters and actual arguments to functions and subroutines ๐ŸŽฏ These warnings are recommended, so they're enabled by default.
  • -Warray-temporaries โ€” Warns about array temporaries generated by the compiler ๐Ÿ“ฆ The info from this warning is sometimes useful in 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 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 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 โ€” Enables a warning for loop interchanges performed by the -ffrontend-loop-interchange option ๐Ÿ”
  • -Wimplicit-interface โ€” Warns if a procedure is called without an explicit interface ๐Ÿ™ˆ Note this only checks that an explicit interface is present. It does not check that the declared interfaces are consistent across program units.
  • -Wimplicit-procedure โ€” Warns if a procedure is called that has neither an explicit interface nor has been declared as EXTERNAL ๐Ÿซ 
  • -Winteger-division โ€” Warns if a constant integer division truncates its result ๐Ÿ• As an example, 3/5 evaluates to 0.
  • -Wintrinsics-std โ€” Warns if gfortran finds a procedure named like an intrinsic that isn't available in the currently selected standard (with -std) and treats it as an EXTERNAL procedure because of this ๐Ÿ‘ป -fall-intrinsics can be used to never trigger this behavior and always link to the intrinsic, regardless of the selected standard.
  • -Wreal-q-constant โ€” Produces a warning if a real-literal-constant contains a q exponent-letter ๐Ÿงฎ
  • -Wsurprising โ€” Produces a warning when "suspicious" code constructs show up ๐Ÿšฉ While technically legal, these usually mean an error has been made. This currently produces a warning under the following circumstances:
    • An INTEGER SELECT construct has a CASE that can never be matched, because its lower value is greater than its upper value.
    • A LOGICAL SELECT construct has three CASE statements.
    • A TRANSFER specifies a source that is shorter than the destination.
    • The type of a function result is declared more than once with the same type. If -pedantic or standard-conforming mode is enabled, this is an error.
    • A CHARACTER variable is declared with negative length.
  • -Wtabs โ€” By default, tabs are accepted as whitespace, but tabs are not members of the Fortran Character Set ๐Ÿซฃ For continuation lines, a tab followed by a digit between 1 and 9 is supported. -Wtabs will cause a warning to be issued if a tab is encountered. Note, -Wtabs is active for -pedantic, -std=f95, -std=f2003, -std=f2008, -std=f2018 and -Wall.
  • -Wundefined-do-loop โ€” Warns if a DO loop with step either 1 or -1 yields an underflow or an overflow during iteration of an induction variable of the loop ๐Ÿ”ข This option is implied by -Wall.
  • -Wunderflow โ€” Produces a warning when numerical constant expressions are encountered that 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. You might need an explicit interface or an EXTERNAL or INTRINSIC declaration so your calls get resolved to the intrinsic or procedure you actually want. Implied by -Wall.
  • -Wuse-without-only โ€” ๐Ÿซฃ Warns if a USE statement has no ONLY qualifier, because then it quietly imports every public entity from the module.
  • -Wunused-dummy-argument โ€” ๐Ÿงน Warns about unused dummy arguments. Implied by -Wall.
  • -Wunused-parameter โ€” ๐ŸŒ€ Plot twist: this one is NOT like gcc's version. gfortran's version skips unused dummy arguments (that's -Wunused-dummy-argument) and goes after unused PARAMETER values instead. Implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons โ€” ๐Ÿงฑ By default, gfortran warns any time variables get padded for alignment inside a COMMON block. Turn that off with -Wno-align-commons. See also -falign-commons.
  • -Wfunction-elimination โ€” โœ‚๏ธ Warns if 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 like (:,:,:) on the left-hand side to skip the reallocation check. Heads up: sometimes this warning shows up even when the compiler would optimize those checks away, like when the right-hand side has the same variable multiplied by a scalar. See also -frealloc-lhs.
  • -Wrealloc-lhs-all โ€” ๐Ÿ“ฃ Warns whenever the compiler inserts allocation or reallocation code for an allocatable variable. That includes scalars and derived types.
  • -Wcompare-reals โ€” โš–๏ธ Warns when you compare real or complex values 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 classic example:
real a(3)
do i=1,4
  if (condition(i)) then
    a(i) = 1.2
  end if
end do

Implied by -Wextra.

  • -Werror โ€” ๐Ÿšจ Turns every warning into an error. No mercy.

Some of these have no effect when compiling programs written in Fortran. ๐Ÿ™ƒ

โœจ Options for debugging your program or GNU Fortran โœจ

Okay so 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 throw 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 this option may also throw 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 this option may also throw internal compiler errors for features that were only recently added.
โš ๏ธ This option is deprecated; use -fdump-fortran-original instead.
  • -fdump-fortran-global โ€” ๐ŸŒ Outputs a list of the global identifiers after translating into middle-end representation. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and this option may also throw internal compiler errors for features that were only recently added.
  • -ffpe-trap=list โ€” ๐Ÿ’ฅ Enables 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 aborts, producing a core file that's super useful for debugging. list is a (possibly empty) comma-separated list of these exceptions:
    • invalid โ€” 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 exceptions match the five IEEE 754 exceptions. The last one, denormal, isn't part of the IEEE 754 standard, but it's available on some common architectures like x86. โœจ

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

If the option shows up 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 because of 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 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 gets used. ๐Ÿ™„

By default, a summary for all exceptions except inexact is shown. ๐Ÿ“Š

  • -fno-backtrace โ€” ๐Ÿ™ˆ When a serious runtime error happens or a deadly signal gets emitted (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. This only has influence when compiling the Fortran main program.

These options affect how GNU Fortran searches for files named by the INCLUDE directive, and where it looks for previously compiled modules. ๐Ÿ“‚

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

  • -Idir โ€” ๐Ÿ—‚๏ธ These affect how the INCLUDE directive gets interpreted, as well as the #include directive of the cpp preprocessor.

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

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 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 โ€” ๐Ÿ”„ Specifies the representation of data for unformatted files. Valid values for conversion are:
    • native โ€” the default
    • swap โ€” swap between big- and little-endian
    • big-endian โ€” use big-endian representation for unformatted files
    • little-endian โ€” use little-endian representation for unformatted files

This option only has an effect when used in the main program. The CONVERT specifier and the GFORTRAN_CONVERT_UNIT environment variable override the default specified by -fconvert. ๐Ÿ’…

  • -frecord-marker=length โ€” ๐Ÿ“ Specifies the length of record markers for unformatted files. Valid values for length are 4 and 8. Default is 4.

This is different from previous versions of gfortran, which specified a default record marker length of 8 on most systems. If you want to read or write files compatible with earlier versions of gfortran, use -frecord-marker=8. ๐Ÿ•ฐ๏ธ

  • -fmax-subrecord-length=length โ€” ๐Ÿงฉ Specifies the maximum length for a subrecord. The maximum permitted value for length is 2147483639, which is also the default. It's only really useful for the gfortran testsuite. ๐Ÿงช
  • -fsign-zero โ€” okay, so when this is on ๐Ÿ’–, floating point numbers that are zero but have the sign bit set get written out as negative numbers in formatted output, and the SIGN intrinsic treats them as negative. -fno-sign-zero is the chill alternative ๐Ÿ™ˆ: it doesn't print the negative sign of zero values (or values that round to zero for I/O), and SIGN treats zero as a positive number, which keeps things compatible with Fortran 77. The default is -fsign-zero โœจ.

โœจ Options for code generation conventions โœจ

These machine-independent options are the ones that control the interface conventions used in code generation ๐Ÿงš. They don't care what machine you're on, which is so iconic.

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

  • -fno-automatic โ€” this one treats every program unit (except the ones marked as RECURSIVE) like the SAVE statement was written for every local variable and array in it ๐Ÿ”’. It doesn't touch common blocks. (Some Fortran compilers call this option -static or -save, fun fact ๐ŸŒธ.) The default is -fautomatic, which puts local variables smaller than the value given by -fmax-stack-var-size on the stack. If you want zero static memory, use -frecursive instead.
    • Local variables or arrays with an explicit SAVE attribute get silently ignored unless you add the -pedantic option ๐Ÿ™ˆ.
  • -ff2c โ€” this generates code that plays nicely with code from g77 and f2c ๐Ÿฆ‹.
    • Here's the tea โ˜•: g77 (which started out in f2c) needs functions that return default REAL to actually return the C type double, and functions that return COMPLEX to hand back their value through an extra argument in the calling sequence, which is a pointer to where the return value should go. With the default GNU calling conventions, those functions just return their results like GNU C would: default REAL functions return float, and COMPLEX functions return the GNU C type complex. This option also implies -fsecond-underscore, unless you explicitly ask for -fno-second-underscore.
    • This does not change the code that talks to the libgfortran library ๐Ÿ’œ.
Caution: mixing Fortran code built with -ff2c and code built with the default -fno-f2c calling conventions is not a good idea. If you call COMPLEX or default REAL functions across parts that were compiled with different conventions, it will break at execution time ๐Ÿ’€.
Caution: this will break code that passes intrinsic functions of type default REAL or COMPLEX as actual arguments, because the library implementations use the -fno-f2c calling conventions ๐Ÿ˜ญ.
  • -fno-underscoring โ€” this stops GNU Fortran from changing the names of entities in your Fortran source by adding underscores to them ๐ŸŽ€.
    • With -funderscoring in effect, GNU Fortran adds one underscore to external names that don't already have any. This is done so the code matches what a lot of UNIX Fortran compilers produce ๐Ÿซถ.
    • > Caution: GNU Fortran's default behavior is incompatible with f2c and g77. If you want object files built with GNU Fortran to be compatible with object code from those tools, please use the -ff2c option ๐Ÿ’….
    • Using -fno-underscoring is not recommended unless you're experimenting with things like fitting GNU Fortran into existing system environments (think existing libraries, tools, and so on) ๐Ÿ™ˆ.
    • Here's an example. Say j() and max_count() are external functions, while my_var and lvar are local variables. With -funderscoring, a statement like this:
I = J() + MAX_COUNT (MY_VAR, LVAR)

is implemented as something like this:

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

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

i = j() + max_count(&my_var, &lvar);
  • -fno-underscoring lets you use your own names directly while debugging, and when you're interfacing GNU Fortran code with other languages ๐Ÿงš.
  • Just because the names match doesn't mean the interface GNU Fortran uses for an external name matches the interface some other language uses for that same name ๐Ÿคจ. Getting GNU Fortran code to link with code from another compiler this way (or any other way) can be just a small part of the job. Getting both compilers to agree on things other than naming can take serious effort, and unlike naming disagreements, linkers normally can't detect those other mismatches ๐Ÿ’€.
  • Also, with -fno-underscoring, the lack of appended underscores creates a very real chance that a user-defined external name will clash with a name in a system library. That could make unresolved-reference bugs super hard to find, since they might only show up at program run time as weird buggy behavior ๐Ÿ˜ญ.
  • In future versions of GNU Fortran, the hope is to improve the naming and linking situation so debugging always uses the names exactly as they appear in the source, even when the linker sees mangled names to stop accidental linking between procedures with incompatible interfaces โœจ.
  • -fsecond-underscore โ€” by default, GNU Fortran adds an underscore to external names. With this option, names that contain underscores get two underscores appended, and external names without underscores get one underscore ๐ŸŒˆ. Internal names with underscores also get two underscores, so they don't collide with external names.
    • This option does nothing if -fno-underscoring is in effect. It's implied by the -ff2c option ๐Ÿซถ.
    • With this option, an external name like MAX_COUNT becomes a reference to the link-time external symbol max_count__, instead of max_count_. This is required for compatibility with g77 and f2c, and it's implied by using -ff2c ๐Ÿ’–.
  • -fcoarray=<keyword> โ€” this picks how coarray support works ๐Ÿ‘‘. The keyword is one of these:
    • none โ€” coarray support is disabled, so using coarray declarations and image-control statements gives a compile-time error. (This is the default ๐Ÿ™ˆ.)
    • single โ€” single-image mode, meaning num_images() is always one ๐Ÿฆ‹.
    • lib โ€” library-based coarray parallelization. You need to link a suitable GNU Fortran coarray library for this one ๐Ÿงš.
  • -fcheck=<keyword> โ€” this turns on run-time checks โญ. The argument has to be a comma-delimited list of the keywords below. Putting no- in front of a check turns it off if an earlier specification turned it on ๐Ÿ’….
    • all โ€” enables all the run-time tests of -fcheck ๐Ÿ‘‘.
    • array-temps โ€” warns at run time when a temporary array had to be generated to pass an actual argument. The info from this warning can be handy for optimization, since you can avoid those temporaries ๐ŸŒธ.
      • Heads up: this warning only prints once per location โœจ.
    • bounds โ€” enables run-time checks for array subscripts and the declared minimum and maximum values. It also checks array indices for assumed and deferred shape arrays against the actual allocated bounds, and makes sure all string lengths are equal for character array constructors that don't have an explicit typespec ๐Ÿงš.
      • Some checks require -fcheck=bounds to be set when compiling the main program ๐Ÿ™ˆ.
      • Note: in the future, this may also cover other kinds of checking, like 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 doesn't affect explicit allocations using the ALLOCATE statement, because those 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 you use it together with -frecursive and -fopenmp ๐Ÿ’€.

Example time ๐Ÿ’…. 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 for the warnings about generated array temporaries โœจ.

  • -fbounds-check โ€” this is a deprecated alias for -fcheck=bounds ๐Ÿ™ˆ. Bye-bye, old friend ๐Ÿฅ€.
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” some C interfaces to Fortran code break the gfortran ABI by leaving out the hidden character length arguments as described in (the reference got cut off here, bestie ๐Ÿ˜ญ). This can lead to crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿ’€.
    • To give existing binary packages a workaround, this option disables tail call optimization for gfortran procedures with character arguments ๐Ÿฉน.
      • With -ftail-call-workaround=2, tail call optimization is disabled in all gfortran procedures with character arguments.
      • With -ftail-call-workaround=1 (or plain -ftail-call-workaround, which is equivalent), it's disabled only in gfortran procedures with character arguments that call implicitly prototyped procedures.
    • Using this option can lead to problems, including crashes from insufficient stack space ๐Ÿ˜ฌ.
    • It is very strongly recommended that you fix the code in question instead ๐Ÿซถ. The -fc-prototypes-external option can generate prototypes that conform to the gfortran 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 equivalently -ftail-call-workaround=0, can be used to turn this option off ๐Ÿ™….
    • The default is currently -ftail-call-workaround, but this will change in future releases ๐ŸŒธ.
  • -fcheck-array-temporaries โ€” this is a deprecated alias for -fcheck=array-temps ๐Ÿ™ˆ.
  • -fmax-array-constructor=n โ€” this can be used to raise the upper limit allowed in array constructors ๐Ÿ“ˆ. The code below requires this option so the array can be expanded at compile time ๐Ÿงฉ.

The example above is what the sample -fmax-array-constructor is about: a 100000-element constant array built with an implied DO loop. Bestie, fair warning, the compile times get wild.

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. Be careful, bestie ๐Ÿ’€

The default value for n is 65535 ๐ŸŒธ

-fmax-stack-var-size=n

okay so this one sets the size, in bytes, of the biggest array that's allowed to live on the stack. If an array goes over that size, it gets static memory instead, except inside procedures marked RECURSIVE ๐Ÿฆ‹ If you want recursive procedures that don't have the RECURSIVE attribute, or you're writing parallel programs, pair it with -frecursive. If you want to never use the stack at all, reach for -fno-automatic โœจ

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

The default value for n is 32768 ๐Ÿ’–

-fstack-arrays

this flag tells the Fortran compiler to put every array of unknown size and every array temporary onto stack memory ๐Ÿฅž If your program uses really big local arrays, you might have to raise your runtime stack limits on some operating systems. This flag is enabled by default at optimization level -Ofast, unless -fmax-stack-var-size is specified ๐Ÿ’…

-fpack-derived

this one packs derived type members as tightly as possible ๐Ÿ“ฆ Code compiled with it is likely to be incompatible with code compiled without it, and it may run slower. Be careful with that combo, it's a whole thing ๐Ÿ™ˆ

-frepack-arrays

sometimes GNU Fortran passes assumed shape array sections through a descriptor that points at a noncontiguous chunk of memory ๐Ÿงฉ This option adds code to the function prologue that repacks the data into a contiguous block at runtime. The result should be faster array accesses, but it can add significant overhead to the function call, especially when the data being passed is noncontiguous. So it's a trade-off, babe ๐Ÿ’œ

-fshort-enums

this one 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 then gives all of its enumerators that kind.

-fexternal-blas

with this on, gfortran will generate calls to BLAS functions for some matrix operations, like MATMUL, instead of using its own algorithms, as long as the matrices are bigger than a given limit (see -fblas-matmul-limit) ๐Ÿ”ฅ This can be super profitable if you have an optimized vendor BLAS library available. The BLAS library has to be specified at link time, so don't forget that step ๐Ÿซ 

-fblas-matmul-limit=n

this only matters when -fexternal-blas is in effect ๐ŸŽ€ Matrix multiplication of matrices with a size larger than, or equal to, n gets handled by calls to BLAS functions, and everything smaller is handled by gfortran's internal algorithms. If the matrices aren't square, the size comparison uses the geometric mean of the dimensions of the argument and result matrices.

The default value for n is 30 ๐ŸŒˆ

-finline-matmul-limit=n

when front-end optimization is active, some calls to the MATMUL intrinsic get inlined โœจ This can bloat your code size if the size of the matrix can't be figured out at compile time, since code for both cases gets generated. Setting -finline-matmul-limit=0 disables inlining in all cases. Setting it to a value of n produces inline code for matrices with a size up to n. If the matrices aren't square, the size comparison uses the geometric mean of the dimensions of the argument and result matrices, same energy as before ๐Ÿง 

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

-frecursive

this lets you allow indirect recursion by forcing all local arrays to be allocated on the stack ๐ŸŒ€ It can't be used together with -fmax-stack-var-size= or -fno-automatic, so pick a lane ๐Ÿ›ฃ๏ธ

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

these are 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.

If you want finer-grained control, you've got -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. Main character energy ๐Ÿ‘‘

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

Heads up, these options do not initialize:

  • objects with the POINTER attribute
  • allocatable arrays
  • variables that appear in an EQUIVALENCE statement

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

Note that -finit-real=nan initializes REAL and COMPLEX variables with a quiet NaN. If you want a signalling NaN, use -finit-real=snan. Be warned though: compile-time optimizations may convert those into quiet NaN, and trapping needs to be enabled (for example 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 warnings that would have been emitted by -Wuninitialized for the affected local variables ๐Ÿคซ

-falign-commons

by default, gfortran enforces proper alignment of all variables in a COMMON block by padding them as needed ๐Ÿงฑ On 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 alignment issues in COMMON blocks, it's recommended to order objects from largest to smallest ๐Ÿ“

-fno-protect-parens

by default, the parentheses in expressions are honored at every optimization level, so the compiler doesn't re-associate anything ๐Ÿซง Using -fno-protect-parens lets the compiler reorder REAL and COMPLEX expressions to make 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 option is enabled by default, except when -std=f95 is given. See also -Wrealloc-lhs ๐Ÿ’–

-faggressive-function-elimination

functions with identical argument lists get eliminated within statements, whether or not they're marked PURE ๐Ÿ”ช For example, in

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

there will only be a single call to f. This option only works if -ffrontend-optimize is in effect.

-ffrontend-optimize

this one performs front-end optimization by manipulating parts of the Fortran parse tree ๐ŸŒณ It's enabled by default by any -O option except -O0 and -Og. The optimizations this option 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))
  • short-circuiting of logical operators (.AND. and .OR.)

It can be deselected by specifying -fno-frontend-optimize ๐Ÿ™…โ€โ™€๏ธ

-ffrontend-loop-interchange

this attempts to interchange loops in the Fortran front end where it's profitable ๐Ÿ” It's enabled by default by any -O option. At the moment, it only affects FORALL and DO CONCURRENT statements with several forall triplets ๐Ÿงš


โœจ ENVIRONMENT โœจ

The gfortran compiler doesn't currently use any environment variables to control its operation, beyond the ones that affect the operation of gcc. So if you're hunting for gfortran-specific env vars, you're out of luck, bestie ๐Ÿฅฒ

๐Ÿ› BUGS ๐Ÿ›

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

๐Ÿ”— SEE ALSO ๐Ÿ”—

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

๐Ÿ’โ€โ™€๏ธ AUTHOR ๐Ÿ’โ€โ™€๏ธ

See the Info entry for gfortran for contributors to GCC and GNU Fortran. The whole squad is listed there ๐Ÿซถ

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

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

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

A GNU Manual

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

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

That's the whole page, bestie! ๐Ÿ’– It's the gcc-9 build, dated 2022-05-27, and it's all GFORTRAN(1) ๐ŸŽ€โœจ Slay the compile times, and don't forget to init your locals ๐Ÿฆ‹๐Ÿ’…

๐Ÿ“ฆ where this page comes from