GFORTRAN(1)

๐ŸŽ€ GFORTRAN(1) โ€” GNU Manual ๐ŸŽ€

Part 1 of 7, straight from the GNU manual ๐Ÿงš


๐ŸŒธ NAME ๐ŸŒธ

gfortran โ€” the GNU Fortran compiler ๐Ÿ’–

โœจ SYNOPSIS โœจ

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

Quick heads up: that synopsis only shows the most useful options ๐Ÿ’…. The rest are waiting for you down below โœจ.

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay so, the gfortran command supports every option that gcc supports ๐Ÿฆ‹. Only the options that are specific to GNU Fortran get documented here, so you're not missing a mountain of stuff ๐Ÿซถ.

Every GCC and GNU Fortran option is accepted by both gfortran and gcc, plus any other drivers built at the same time, like g++ ๐Ÿ‘‘. That's because adding GNU Fortran to the GCC distribution means all the relevant drivers accept GNU Fortran options. Main character energy, honestly โœจ.

Some options come in positive and negative forms. So the negative version of -ffoo is -fno-foo ๐Ÿ™ˆ. This manual only documents whichever form isn't the default ๐Ÿ’….

๐ŸŒˆ OPTIONS ๐ŸŒˆ

Here's the vibe check of every GNU Fortran option, grouped by type ๐Ÿ’…. Explanations live in the sections that follow ๐Ÿฆ‹.

๐Ÿ’ซ Fortran Language Options

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

๐ŸŽ€ Preprocessing Options

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

๐Ÿšจ Error and Warning Options

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

๐Ÿ› Debugging Options

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

๐Ÿ“ Directory Options

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

๐Ÿƒ Runtime Options

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

๐Ÿค Interoperability Options

-fc-prototypes -fc-prototypes-external

๐Ÿ›  Code Generation Options

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

๐Ÿฆ‹ Options controlling Fortran dialect

These options tweak the exact flavour of Fortran the compiler accepts ๐Ÿ’…:

  • -ffree-form, -ffixed-form โ€” pick the layout of your source file ๐Ÿ“œ. Free form showed up in Fortran 90, while fixed form is what older Fortran programs were traditionally written in. If you specify neither, the source form is determined by the file extension โœจ.
  • -fall-intrinsics โ€” accepts every intrinsic procedure, including the GNU-specific extensions ๐ŸŒˆ. It's great alongside -std= if you want standard-compliance but still want the full intrinsic buffet. Heads up: -Wintrinsics-std gets ignored, and no user-defined procedure sharing a name with an intrinsic will be called, unless it's explicitly declared EXTERNAL ๐Ÿ™ˆ.
  • -fallow-argument-mismatch โ€” some code calls external procedures with mismatched arguments, either between the call and the procedure definition or between different calls ๐Ÿ˜ฌ. That code is non-conforming and usually gets flagged as an error. This option downgrades the error to a warning, which can only be turned off by disabling all warnings with -w. Only a single occurrence per argument gets flagged. It's implied by -std=legacy.
Using this option is strongly discouraged ๐Ÿ’€. You can write standard-conforming code that allows different argument types by using an explicit interface and TYPE(*).
  • -fallow-invalid-boz โ€” a BOZ literal constant can show up in a limited number of contexts in standard-conforming Fortran ๐Ÿ”ข. This option downgrades the error to a warning and lets a BOZ literal constant appear where the Fortran standard would otherwise prohibit it ๐Ÿ™ˆ.
  • -fd-lines-as-code, -fd-lines-as-comments โ€” special treatment for lines beginning with "d" or "D" in fixed-form sources ๐Ÿ’ฌ. With -fd-lines-as-code, they'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 enables extensions and other features that mimic the default behavior of older compilers, like DEC's. For details on GNU Fortran's implementation of these extensions, check the full documentation. Other flags enabled by this switch are: -fdollar-ok, -fcray-pointer, -fdec-char-conversions, -fdec-structure, -fdec-intrinsic-ints, -fdec-static, -fdec-math, -fdec-include, -fdec-blank-format-item, and -fdec-format-defaults. If -fd-lines-as-code/-fd-lines-as-comments are unset, then -fdec also sets -fd-lines-as-comments.
These features are non-standard and should be avoided at all costs ๐Ÿšซ.
  • -fdec-char-conversions โ€” lets you use character literals in assignments and DATA statements for non-character variables ๐Ÿ“.
  • -fdec-structure โ€” enables DEC STRUCTURE and RECORD, plus UNION, MAP, and the dot (.) as a member separator, on top of % ๐Ÿงฑ. This is provided for compatibility only, so Fortran 90 derived types should be used instead wherever possible.
  • -fdec-intrinsic-ints โ€” enables B/I/J/K kind variants of existing integer functions, like BIAND, IIAND, JIAND, etc... ๐Ÿ”ข. Check the full documentation for a complete list of intrinsics.
  • -fdec-math โ€” enables legacy math intrinsics like COTAN and degree-valued trigonometric functions, like TAND, ATAND, etc... ๐Ÿ“, for compatibility with older code.
  • -fdec-static โ€” enables DEC-style STATIC and AUTOMATIC attributes to explicitly specify the storage of variables and other objects ๐Ÿ“ฆ.
  • -fdec-include โ€” enables parsing of INCLUDE as a statement in addition to parsing it as an INCLUDE line ๐Ÿ“Ž. When parsed as an INCLUDE statement, it doesn't have to be on a single line and can use line continuations.
  • -fdec-format-defaults โ€” enables format specifiers F, G, and I to be used without width specifiers. Default widths get used instead ๐Ÿ“.
  • -fdec-blank-format-item โ€” enables a blank format item at the end of a format specification, i.e. nothing following the final comma ๐Ÿซง.
  • -fdollar-ok โ€” allows $ as a valid non-first character in a symbol name ๐Ÿ’ฒ. Symbols that start with $ are rejected, since it's unclear which implicit typing rules to apply (different vendors implement different rules). Using $ in IMPLICIT statements is also rejected.
  • -fbackslash โ€” changes how backslashes in string literals are read, from a single backslash character to "C-style" escape characters ๐Ÿงƒ. The following combinations get expanded: "\a" (alert), "\b" (backspace), "\f" (form feed), "\n" (newline), "\r" (carriage return), "\t" (horizontal tab), "\v" (vertical tab), "\\" (backslash), and "\0" (NUL). Additionally, "\x"nn, "\u"nnnn and "\U"nnnnnnnn (where each n is a hexadecimal digit) are translated into the Unicode characters for those code points ๐ŸŒ. All other combinations of a character preceded by \ stay 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 are ignored in typical fixed-form lines in the source file ๐Ÿ“. Unless -fno-pad-source is used, 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 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 ๐Ÿงฝ. This doesn't happen if -ffixed-line-length-0, -ffixed-line-length-none, or -fno-pad-source is used. With any of those options, continued character constants never have implicit spaces appended to fill out the line.
  • -ffree-line-length-n โ€” okay so this one sets the column where the compiler starts ignoring characters on typical free-form lines in your source file. The default is 132 ๐Ÿ’…. You can pass none for n if you want the whole line to count, and -ffree-line-length-0 is exactly the same as -ffree-line-length-none ๐Ÿงš
  • -fmax-identifier-length=n โ€” this caps how long an identifier can be ๐Ÿ“. Typical values are 31 (Fortran 95) and 63 (Fortran 2003 and later) โœจ
  • -fimplicit-none โ€” bestie, this one bans implicit typing ๐Ÿ™…โ€โ™€๏ธ. The only exceptions are explicit IMPLICIT statements. It's the same as putting implicit none at the top of every procedure, so it's giving strict ๐Ÿ’œ
  • -fcray-pointer โ€” turns on the Cray pointer extension, which gives you C-like pointer functionality ๐Ÿฆ‹
  • -fopenacc โ€” enables the OpenACC extensions ๐Ÿ”ฅ. In free form that covers !$acc directives, plus the !$ conditional compilation sentinels. In fixed form you get c$acc, *$acc and !$acc directives, plus the c$, *$ and !$ sentinels. When you link, it also makes sure the OpenACC runtime library gets linked in ๐ŸŒธ
  • -fopenmp โ€” enables the OpenMP extensions ๐Ÿ‘‘. Free form gets !$omp directives and the !$ sentinels. Fixed form gets c$omp, *$omp and !$omp directives, plus the c$, *$ and !$ sentinels. Linking also pulls in the OpenMP runtime library. Heads up that -fopenmp implies -frecursive ๐Ÿซถ
  • -fno-range-check โ€” this turns off range checking on the results of simplifying constant expressions at compile time ๐Ÿงฎ. Normally GNU Fortran throws an error on something like a = 1. / 0. With this flag there's no error, and a gets +Infinity. If an expression lands outside the relevant range of -HUGE():HUGE(), it's replaced by -Inf or +Inf, whichever fits. Same deal with DATA i/Z'FFFFFFFF'/, which overflows an integer on most systems. With this flag the value wraps around, and i ends up initialized to -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 leaves variables with an explicit kind declaration alone ๐ŸŽ€
  • -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 flag bumps the default width of DOUBLE PRECISION and double real constants like 1.d0 up to 16 bytes if that's possible. If you pass -fdefault-double-8 alongside fdefault-real-8, then DOUBLE PRECISION and double real constants don't get promoted. Unlike -freal-4-real-8, fdefault-real-8 leaves variables with explicit kind declarations alone ๐ŸŒˆ
  • -fdefault-real-10 โ€” same vibe as -fdefault-real-8, but the default real type becomes 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 pass -fdefault-double-8 along with fdefault-real-10, then DOUBLE PRECISION and double real constants aren't promoted. Unlike -freal-4-real-10, fdefault-real-10 leaves variables with explicit kind declarations alone ๐Ÿฆ‹
  • -fdefault-real-16 โ€” once more, but with a 16-byte-wide default real 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 pass -fdefault-double-8 with fdefault-real-16, then DOUBLE PRECISION and double real constants aren't promoted. Unlike -freal-4-real-16, fdefault-real-16 leaves variables with explicit kind declarations alone ๐Ÿ’œ
  • -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 flag 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. Be careful with this one, because it may not suit your code ๐Ÿ˜ญ
Be careful with -finteger-4-integer-8! The areas of concern are calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, and I/O. It's suggested that you inspect the intermediate representation of the translated Fortran code, produced by -fdump-tree-original, before you trust it ๐Ÿ™ˆ
  • -freal-4-real-8, -freal-4-real-10, -freal-4-real-16, -freal-8-real-4, -freal-8-real-10, -freal-8-real-16 โ€” these promote all REAL(KIND=M) entities to REAL(KIND=N) entities ๐Ÿ‘‘. If REAL(KIND=N) isn't available, you'll get an error. The -freal-4- flags also change the default real kind, and the -freal-8- flags change the double-precision real kind. Every other real-kind type is untouched. The promotion also applies to real literal constants of default and double-precision kind, with a specified kind number of 4 or 8 respectively. Heads up though: -fdefault-real-8, -fdefault-real-10, -fdefault-real-10, and -fdefault-double-8 take precedence for the default and double-precision real kinds, both for real literal constants and for declarations without a kind number ๐Ÿง . For REAL(KIND=KIND(1.0)), the literal may get promoted and then the result may get promoted again. Lastly, these options should be used with care and may not suit your code. The areas of concern are calls to external procedures, alignment in EQUIVALENCE and/or COMMON, generic interfaces, BOZ literal constant conversion, I/O, and calls to intrinsic procedures when passing a value to the kind= dummy argument. It's suggested that you inspect the intermediate representation of the translated Fortran code, produced by -fdump-fortran-original or -fdump-tree-original, too ๐Ÿ’…
  • -std=std โ€” tells the compiler which standard your program is supposed to follow ๐Ÿ“œ. Your choices are f95, f2003, f2008, f2018, gnu, or legacy. The default is gnu, which is a superset of the latest Fortran standard and includes every extension GNU Fortran supports. You'll still get warnings for obsolete extensions that aren't recommended in new code ๐Ÿ™ƒ. The legacy value is the same, minus the warnings for obsolete extensions, so it's handy for old non-standard programs ๐Ÿง“. f95, f2003, f2008 and f2018 mean strict conformance to Fortran 95, Fortran 2003, Fortran 2008 and Fortran 2018 respectively. Any extension beyond the relevant standard throws an error, and Fortran 77 features that are allowed but obsolescent in later standards get warnings. The deprecated -std=f2008ts is an alias for -std=f2018. It only exists for backwards compatibility with earlier gfortran versions, so please don't use it anymore ๐Ÿšซ
  • -ftest-forall-temp โ€” boosts test coverage by forcing most forall assignments to use a temporary ๐Ÿงช

โœจ Enable and customize preprocessing โœจ

Okay, so many Fortran compilers, GNU Fortran included, let you pass your source code through a C preprocessor (CPP, sometimes called the Fortran preprocessor, or FPP) for conditional compilation ๐Ÿงš. For GNU Fortran that's the GNU C Preprocessor in traditional mode ๐ŸŒˆ. On systems with case-preserving file names, the preprocessor runs automatically if the filename extension is .F, .FOR, .FTN, .fpp, .FPP, .F90, .F95, .F03 or .F08. To run it manually on any file, use -cpp. To turn it off for files where it would otherwise run automatically, use -nocpp ๐Ÿ’–

If a preprocessed file pulls in another file with the Fortran INCLUDE statement, that included file is not preprocessed ๐Ÿ™…โ€โ™€๏ธ. If you want included files preprocessed, use the equivalent preprocessor statement #include instead ๐Ÿซถ

When GNU Fortran invokes the preprocessor, __GFORTRAN__ gets defined ๐ŸŽ€. You can use the macros __GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__ to figure out the compiler version ๐Ÿ”ฅ. For the full details, see Top,,Overview,cpp,The C Preprocessor ๐Ÿ“š

GNU Fortran supports several INTEGER and REAL kind types on top of the ones the Fortran standard requires ๐Ÿ‘‘. Which kind types you get depends on your architecture ๐Ÿง . These predefined preprocessor macros let you conditionally include code for those extra kinds: __GFC_INT_1__, __GFC_INT_2__, __GFC_INT_8__, __GFC_INT_16__, __GFC_REAL_10__, and __GFC_REAL_16__ ๐Ÿ’…

CPP is the de facto standard for preprocessing Fortran code, but Part 3 of the Fortran 95 standard (ISO/IEC 1539-3:1998) defines Conditional Compilation ๐Ÿฆ‹. It isn't widely used, and GNU Fortran doesn't directly support it ๐Ÿ˜ญ. If you need it, you can use the program coco to preprocess those files, and you'll find it at <http://www.daniellnagle.com/coco.html> ๐ŸŒธ

The following options control preprocessing of Fortran code ๐Ÿ“:

  • -cpp, -nocpp โ€” -cpp enables preprocessing. The preprocessor runs automatically for .fpp, .FPP, .F, .FOR, .FTN, .F90, .F95, .F03 and .F08 files, but use this flag to turn it on manually for any kind of Fortran file โœจ. -nocpp is the negative form, and it turns preprocessing off for files with any of those extensions ๐Ÿ™ˆ. The preprocessor runs in traditional mode, so any file-format restrictions (especially the line length limits) also apply to the preprocessed output. That means you might want -ffree-line-length-none or -ffixed-line-length-none ๐Ÿ’œ
  • -dM โ€” instead of the normal output, this generates a list of #define directives for every macro defined while the preprocessor runs, predefined ones included ๐Ÿงš. It's a sneaky way to find out what's predefined in your version of the preprocessor ๐Ÿ‘€. Assuming you have no file foo.f90, this command shows all the predefined macros:
touch foo.f90; gfortran -cpp -E -dM foo.f90
  • -dD โ€” Okay so this one is like -dM but with two big differences ๐Ÿ’…. It skips the predefined macros, and it spits out both the #define directives and the result of preprocessing. Both kinds of output land in the standard output file ๐Ÿ–จ๏ธโœจ.
  • -dN โ€” Basically -dD, but it only shows the macro names and skips their expansions ๐Ÿฆ‹.
  • -dU โ€” Also like -dD, but way more selective ๐ŸŽ€. It only outputs macros that get expanded, or macros whose definedness gets tested in preprocessor directives. The output is delayed until that use or test happens, and #undef directives also show up for macros that were tested but were undefined at that moment ๐Ÿ™ˆ.
  • -dI โ€” Outputs #include directives on top of the preprocessing result ๐Ÿงš.
  • -fworking-directory โ€” This one is giving main character energy ๐Ÿ‘‘. It turns on linemarkers in the preprocessor output so the compiler knows the current working directory at preprocessing time. When it's on, the preprocessor emits a second linemarker after the initial one, containing the current working directory followed by two slashes. GCC will use that directory, if it's in the preprocessed input, as the working directory in some debugging information formats ๐ŸŒธ. It's implicitly enabled when debugging information is on, but you can shut it off with the negated form -fno-working-directory. If -P is in the command line, this option does nothing at all, because no #line directives get emitted whatsoever ๐Ÿ’€.
  • -idirafter dir โ€” Searches dir for include files, but only after every directory from -I and the standard system directories have been exhausted ๐ŸŒˆ. dir gets treated as a system include directory. If dir begins with =, that = gets swapped out for the sysroot prefix. See --sysroot and -isysroot ๐Ÿซถ.
  • -imultilib dir โ€” Uses dir as a subdirectory of the directory that holds the target-specific C++ headers ๐Ÿ’œ.
  • -iprefix prefix โ€” Sets prefix as the prefix for later -iwithprefix options. If the prefix is a directory, make sure you include the final / ๐Ÿ“.
  • -isysroot dir โ€” Basically the --sysroot option, but it only applies to header files. Check out the --sysroot option for the full scoop โœจ.
  • -iquote dir โ€” Only searches dir for header files requested with "#include "file"". These don't get searched for #include <file>. It happens before all the -I directories and the standard system directories ๐ŸŽ€. If dir begins with =, the = gets replaced by the sysroot prefix. See --sysroot and -isysroot ๐Ÿ’–.
  • -isystem dir โ€” Searches dir for header files, after all the -I directories but before the standard system directories ๐Ÿ”ฅ. It gets marked as a system directory, so it gets the same special treatment as the standard ones. If dir begins with =, that = is replaced by the sysroot prefix. See --sysroot and -isysroot ๐Ÿ‘‘.
  • -nostdinc โ€” Don't search the standard system directories for header files. Only the directories you gave with -I options (plus the directory of the current file, if that applies) get searched ๐Ÿ™…โ€โ™€๏ธ.
  • -undef โ€” Don't predefine any system-specific or GCC-specific macros. The standard predefined macros stay defined ๐ŸŒท.
  • -Apredicate=answer โ€” Makes an assertion with the predicate predicate and the answer answer ๐Ÿ”ฎ. This form is the preferred one over the older -A predicate(answer) form. The old one still works, but the new one is better because it doesn't use shell special characters ๐Ÿ’….
  • -A-predicate=answer โ€” Cancels an assertion with the predicate predicate and the answer answer ๐Ÿšซ.
  • -C โ€” Don't throw away comments! ๐Ÿซง All comments get passed through to the output file, except comments inside processed directives. Those get deleted along with the directive ๐Ÿ˜ญ.

You should be ready for side effects when you use -C, bestie โš ๏ธ. It makes the preprocessor treat comments as tokens in their own right. For example, a comment at the start of what would be a directive line turns that line into an ordinary source line, since the first token is no longer a # ๐Ÿ™ˆ.

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

On top of the side effects of -C, the -CC option converts all C++-style comments inside a macro into C-style comments ๐Ÿฆ‹. That's so using the macro later can't accidentally comment out the rest of the source line. -CC is mostly used to support lint comments ๐Ÿซถ.

Warning: this currently handles C- and C++-style comments only. The preprocessor does not yet recognize Fortran-style comments ๐Ÿ’€.
  • -Dname โ€” Predefines name as a macro, with the definition 1 ๐Ÿ“.
  • -Dname=definition โ€” The contents of definition get tokenized and processed as if they showed up during translation phase three in a #define directive ๐Ÿงš. The definition gets cut off by any embedded newline characters ๐Ÿ™ˆ.

If you're calling the preprocessor from a shell (or something shell-like), you might need the shell's quoting syntax to protect characters like spaces that mean something in shell syntax ๐ŸŒธ.

If you want to define a function-like macro on the command line, write its argument list in 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 the -D and -U options ๐Ÿ”ฅ.

  • -H โ€” Prints the name of each header file that gets used, along with the other normal stuff ๐Ÿฆ‹. Each name is indented to show how deep it is in the #include stack ๐ŸŒˆ.
  • -P โ€” Inhibits linemarker generation in the preprocessor output ๐ŸŽ€. This can be useful when you're running the preprocessor on something that isn't C code and that's going to a program that might get confused by the linemarkers ๐Ÿ’œ.
  • -Uname โ€” Cancels any previous definition of name, whether it was built in or provided with a -D option ๐Ÿšซ.

๐Ÿšจ Options to request or suppress errors and warnings

Errors are diagnostic messages that say the GNU Fortran compiler can't compile that piece of source code ๐Ÿ˜ญ. The compiler keeps going anyway, trying to report more errors to help you debug, but it won't produce any compiled output ๐Ÿ’€.

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

You can request lots of specific warnings with options that start with -W. For example, -Wimplicit asks for warnings about implicit declarations ๐Ÿ”ฎ. Each of these specific warning options also has a negative form that starts with -Wno-, which turns the warnings off. For example, -Wno-implicit ๐Ÿ™…โ€โ™€๏ธ. This manual only lists one of the two forms, whichever one isn't the default ๐ŸŒท.

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

  • -fmax-errors=n โ€” Limits the maximum number of error messages to n. Once that number is hit, GNU Fortran bails out instead of trying to keep processing the source code ๐Ÿ˜ญ. If n is 0, there's no limit on how many error messages get produced ๐Ÿง.
  • -fsyntax-only โ€” Checks the code for syntax errors, but doesn't actually compile it ๐Ÿซง. It generates module files for each module in the code, but no other output file ๐Ÿ“ฆ.
  • -Wpedantic, -pedantic โ€” Issues warnings for uses of extensions to Fortran โœจ. -pedantic also applies to C-language constructs when 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 fine with or without this option ๐Ÿ’–. Without it, certain GNU extensions and traditional Fortran features are supported too ๐ŸŒˆ. With it, lots of those get rejected ๐Ÿšซ.

Some people try to use -pedantic to check programs for conformance, and then they're shocked ๐Ÿ˜ญ. It finds some nonstandard practices, but not all of them. Still, improvements to GNU Fortran in this area are very welcome ๐Ÿซถ.

This should be used together with -std=f95, -std=f2003, -std=f2008 or -std=f2018 ๐Ÿ’….

  • -pedantic-errors โ€” Like -pedantic, except that you get errors instead of warnings ๐Ÿ”ฅ.
  • -Wall โ€” Turns on commonly used warning options for usage that we recommend avoiding, and that we believe are easy to avoid ๐Ÿ‘‘. Right now this 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 ๐Ÿฆ‹.

This example will trigger the warning ๐Ÿงš:

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

call bar(a,a)
  • -Wampersand โ€” Warns about a missing ampersand in continued character constants ๐ŸŒท. The warning is given with -Wampersand, -pedantic, -std=f95, -std=f2003, -std=f2008 and -std=f2018 ๐Ÿ’œ. Note: if there's no ampersand in a continued character constant, GNU Fortran assumes the continuation happens at the first non-comment, non-whitespace character after the ampersand that started the continuation ๐Ÿซถ.
  • -Warray-temporaries โ€” Warns about array temporaries the compiler generates ๐ŸŒˆ. The info from this warning is sometimes useful for optimization, so you can avoid those temporaries โญ.

Okay so this next batch is all about warnings ๐Ÿšจ and the debugging flags ๐Ÿ›, and I'm obsessed with how many of them exist. Let's go, bestie ๐Ÿ’…

  • -Wc-binding-type โ€” โœจ warns if a variable might not be C interoperable ๐Ÿฆ‹. It's the vibe where you declare something with an intrinsic type using the default kind instead of a kind parameter from the intrinsic ISO_C_Binding module, and gfortran calls you out for it. This one is implied by -Wall.
  • -Wcharacter-truncation โ€” ๐ŸŽ€ heads up if a character assignment is going to chop off the string you're assigning โœ‚๏ธ.
  • -Wline-truncation โ€” ๐Ÿ“ warns when a source code line is about to get cut short. This is implied by -Wall. For free-form source code, the default is -Werror=line-truncation, so truncations get reported as errors instead ๐Ÿ˜ญ.
  • -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. It does not imply -Wconversion ๐Ÿ™…โ€โ™€๏ธ.
  • -Wextra โ€” ๐Ÿ’ซ turns on some warning options for language features that can be problematic. Right now that's -Wcompare-reals, -Wunused-parameter and -Wdo-subscript.
  • -Wfrontend-loop-interchange โ€” warns when you're using -ffrontend-loop-interchange to do loop interchanges ๐Ÿ”„.
  • -Wimplicit-interface โ€” warns if a procedure is called without an explicit interface. Note that it only checks that an explicit interface is present. It doesn't check that the declared interfaces are consistent across program units ๐Ÿคทโ€โ™€๏ธ.
  • -Wimplicit-procedure โ€” warns if a procedure is called that has neither an explicit interface nor has been declared as "EXTERNAL".
  • -Winteger-division โ€” warns if a constant integer division truncates its result. 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 (-std). Because of that, it treats it as an "EXTERNAL" procedure. Use -fall-intrinsics if you want to never trigger this and always link to the intrinsic, no matter which standard is selected ๐Ÿงš.
  • -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. Use this to shush the warning when you know recursion isn't broken. It's useful for build environments that use -Werror.
  • -Wreal-q-constant โ€” warns if a real-literal-constant contains a "q" exponent-letter.
  • -Wsurprising โ€” ๐Ÿ‘€ warns when "suspicious" code constructs show up. They're technically legal, but they usually mean an error was made. Right now it fires under these circumstances:
    • An INTEGER SELECT construct has a CASE that can never be matched, since 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 makes gfortran issue a warning whenever a tab is encountered. It's active for -pedantic, -std=f95, -std=f2003, -std=f2008, -std=f2018 and -Wall.
  • -Wundefined-do-loop โ€” warns if a DO loop with a step of either 1 or -1 yields an underflow or an overflow while the induction variable of the loop is iterating. This 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. In that case, an explicit interface or an "EXTERNAL" or "INTRINSIC" declaration might be needed so calls get resolved to the procedure you actually want. This is implied by -Wall.
  • -Wuse-without-only โ€” warns if a USE statement has no ONLY qualifier, which means it implicitly imports all public entities of the used module.
  • -Wunused-dummy-argument โ€” warns about unused dummy arguments. Implied by -Wall.
  • -Wunused-parameter โ€” ๐Ÿค” this one is different from gcc's meaning. gfortran's version doesn't warn about unused dummy arguments (for that, see -Wunused-dummy-argument). Instead, it warns about unused "PARAMETER" values. -Wunused-parameter is implied by -Wextra if -Wunused or -Wall is also used.
  • -Walign-commons โ€” by default, gfortran warns about any variables being padded for proper alignment inside a "COMMON" block. You can turn this off with -Wno-align-commons. See also -falign-commons.
  • -Wfunction-elimination โ€” warns if any calls to impure functions are eliminated by the optimizations enabled by -ffrontend-optimize. Implied by -Wextra.
  • -Wrealloc-lhs โ€” ๐ŸŒ warns when the compiler might insert code for allocation or reallocation of an allocatable array variable of intrinsic type in intrinsic assignments. In hot loops, the Fortran 2003 reallocation feature may reduce performance. If the array is already allocated with the correct shape, consider using a whole-array array-spec (e.g. "(:,:,:)") for the variable on the left-hand side to prevent the reallocation check. The warning can show up even when the compiler would optimize the reallocation checks away, for instance when the right-hand side contains the same variable multiplied by a scalar. See also -frealloc-lhs.
  • -Wrealloc-lhs-all โ€” warns when the compiler inserts code for allocation or reallocation of an allocatable variable. This includes scalars and derived types.
  • -Wcompare-reals โ€” warns when comparing real or complex types for equality or inequality. Implied by -Wextra.
  • -Wtarget-lifetime โ€” warns if the pointer in a pointer assignment might live longer than its target. Implied by -Wall.
  • -Wzerotrip โ€” warns if a DO loop is known to execute zero times at compile time. Implied by -Wall.
  • -Wdo-subscript โ€” ๐Ÿง warns if an array subscript inside a DO loop could lead to an out-of-bounds access, even if the compiler can't prove the statement is actually executed. Here's the sort 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. Bestie, these are for the deep dives ๐Ÿง โœจ

  • -fdump-fortran-original โ€” outputs the internal parse tree after the source program has been translated into internal representation. It's 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 this option 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 has been translated into internal representation. It's 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.
โš ๏ธ This option is deprecated; use -fdump-fortran-original instead.
  • -fdebug-aux-vars โ€” ๐Ÿ” renames internal variables created by the gfortran front end and makes them accessible to a debugger. Their names then start with upper-case letters followed by an underscore. This is useful for debugging the compiler's code generation together with -fdump-tree-original, and for debugging the executable program by using -g or -ggdb3.
  • -fdump-fortran-global โ€” outputs a list of the global identifiers after translating into middle-end representation. Mostly useful for debugging the GNU Fortran compiler itself. The output might change between releases, and this option may also generate internal compiler errors for features that were only recently added.
  • -ffpe-trap=list โ€” ๐Ÿ’ฅ specifies a list of floating point exception traps to enable. On most systems, if a floating point exception occurs and the trap for that exception is enabled, a SIGFPE signal is sent and the program is aborted, producing a core file that's useful for debugging. list is a (possibly empty) comma-separated list of the following 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 operation)
    • denormal (operation performed on a denormal value)

The first five exceptions correspond to the five IEEE 754 exceptions, whereas the last one (denormal) is not part of the IEEE 754 standard but is available on some common architectures such as x86.

The first three exceptions (invalid, zero, and overflow) often indicate serious errors. Unless the program has provisions for dealing with these, enabling traps for them is probably a good idea ๐Ÿ’–.

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

Bestie, once you turn an exception trap on, it can't be turned off again ๐Ÿ˜ญ There's no negative form, so choose wisely ๐Ÿ’…

Many, if not most, floating point operations lose a little precision from rounding ๐ŸŒธ so ffpe-trap=inexact is honestly going to be super uninteresting in real life ๐Ÿฅฑ

By default, no exception traps are enabled ๐Ÿฆ‹

  • -ffpe-summary=list โ€” Okay, this one lets you pick which floating-point exceptions get their flag status printed to ERROR_UNIT whenever STOP or ERROR STOP runs โœจ The 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 you use this option more than once on the command line, only the last one counts ๐Ÿ‘‘ By default, you get a summary for all exceptions except inexact ๐Ÿ’–
  • -fno-backtrace โ€” So when a serious runtime error hits, or a deadly signal goes off (segmentation fault, illegal instruction, bus error, floating-point exception, and any other POSIX signal whose action is core) ๐Ÿ’€ the Fortran runtime library tries to print a backtrace of the error. -fno-backtrace turns that backtrace generation off ๐Ÿ™ˆ This only has an effect when compiling the Fortran main program.

These options are all about how GNU Fortran hunts down files pulled in by the INCLUDE directive, and where it looks for previously compiled modules ๐Ÿ”

They also change the search paths that cpp uses when it preprocesses Fortran source ๐ŸŒˆ

  • -Idir โ€” These affect how the INCLUDE directive gets interpreted (and the #include directive in the cpp preprocessor too) ๐Ÿซถ Also, -I and INCLUDE basically behave the same as -I with #include in cpp, when it comes to looking for header.gcc files and other stuff like that. This path is also where it searches for .mod files when a previously compiled module is needed by a USE statement ๐Ÿ“ฆ
  • -Jdir โ€” This one tells the compiler where to put the .mod files for compiled modules ๐Ÿ“ It also gets added to the list of directories that a USE statement searches. The default is the current directory ๐Ÿ 
  • -fintrinsic-modules-path dir โ€” Use this if the pre-compiled intrinsic modules aren't in the default spot the compiler expects ๐Ÿ—บ๏ธ

๐Ÿ”— Influencing the linking step ๐Ÿ”—

These options only matter when the compiler links object files into an executable output file ๐Ÿ’… If there's no link step, they're meaningless ๐Ÿ™ƒ

  • -static-libgfortran โ€” On systems that ship libgfortran as both a shared and a static library, this forces the static version to be used ๐ŸงŠ If no shared version of libgfortran got built when the compiler was configured, this option does absolutely nothing ๐Ÿคทโ€โ™€๏ธ

๐ŸŒธ Influencing runtime behavior ๐ŸŒธ

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

  • -fconvert=conversion โ€” This sets how data is represented in unformatted files ๐Ÿ“ผ The valid values for conversion are:
    • native โ€” the default ๐Ÿ’–
    • 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 both override whatever default -fconvert sets ๐Ÿ‘‘

  • -frecord-marker=length โ€” This sets the length of record markers for unformatted files. Valid values are 4 and 8, and the default is 4 ๐Ÿ“ This is different from older gfortran versions, which defaulted to a record marker length of 8 on most systems. If you need to read or write files that are compatible with earlier versions of gfortran, use -frecord-marker=8 ๐Ÿ•ฐ๏ธ
  • -fmax-subrecord-length=length โ€” This sets the maximum length for a subrecord ๐Ÿ“ The maximum permitted value for length is 2147483639, which is also the default. Honestly, it's only really useful for the gfortran testsuite ๐Ÿงช
  • -fsign-zero โ€” When enabled, floating point zeros with the sign bit set get written as negative numbers in formatted output, and get treated as negative by the SIGN intrinsic โž– -fno-sign-zero skips printing the negative sign on zero values (or values rounded to zero for I/O), and treats zero as a positive number 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 come in both positive and negative forms, where the negative form of -ffoo is -fno-foo ๐Ÿ” The list below only shows the form that is not the default. You can figure out the other form by removing no- or adding it ๐Ÿง 

  • -fno-automatic โ€” Treats each program unit (except the ones marked 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 call this option -static or -save.) The default, which is -fautomatic, puts local variables smaller than the value given by -fmax-stack-var-size on the stack ๐Ÿฅž Use -frecursive if you want no static memory at all ๐Ÿ™…

Local variables or arrays that have an explicit SAVE attribute get silently ignored, unless you add the -pedantic option ๐Ÿค“

  • -ff2c โ€” This generates code that's compatible with code generated by g77 and f2c ๐Ÿค The g77 calling conventions (originally from f2c) make functions that return default REAL actually return the C type double, and functions that return COMPLEX send their values back through an extra argument in the calling sequence that points to where the return value should be stored ๐ŸŽˆ With the default GNU calling conventions, those functions just return their results like GNU C does: default REAL functions return the C type float, and COMPLEX functions return the GNU C type complex ๐Ÿ’ซ This option also implies -fsecond-underscore, unless you explicitly ask for -fno-second-underscore ๐Ÿซง

This does not affect code that interfaces with the libgfortran library ๐Ÿ“š

Caution: It's not a good idea to mix Fortran code compiled with -ff2c with code compiled with the default -fno-f2c calling conventions ๐Ÿ˜ฌ If you call COMPLEX or default REAL functions between program parts that were compiled with different calling conventions, things 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 transforming the names of entities in your Fortran source by appending underscores to them ๐Ÿ™…

With -funderscoring in effect, GNU Fortran appends one underscore to external names that don't have any underscores already ๐Ÿช„ This keeps things compatible with code produced by a lot of UNIX Fortran compilers ๐Ÿง

Caution: GNU Fortran's default behavior is incompatible with f2c and g77 ๐Ÿ’” If you want object files from GNU Fortran to be compatible with object code made by these tools, please use -ff2c instead.

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

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

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

gets implemented as something akin to:

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

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

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

Using -fno-underscoring lets you use your own names directly while debugging, and when you're interfacing GNU Fortran code with other languages ๐Ÿซถ

Heads up though: just because the names match doesn't mean the interface GNU Fortran uses for an external name matches the interface another language uses for that same name ๐Ÿ‘€ Getting code from GNU Fortran to link with code from some other compiler, by this method or any other, is only a small part of the overall solution ๐Ÿงฉ Getting both compilers' output to agree on everything other than naming can take serious effort, and unlike naming disagreements, linkers usually can't detect those other mismatches ๐Ÿ˜ญ

Also, with -fno-underscoring, the lack of appended underscores makes it very real that a user-defined external name could clash with a name in a system library ๐Ÿ’ฅ That can make unresolved-reference bugs really hard to find, since they might only show up at program run time as weird buggy behavior ๐Ÿ™ƒ

In future versions of GNU Fortran, the hope is to improve naming and linking so debugging always uses the names 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 appends an underscore to external names ๐ŸŒฑ If you use this option, GNU Fortran appends two underscores to names that already have underscores, and one underscore to external names with no underscores ๐ŸŒŸ It also appends two underscores to internal names with underscores, so they don't collide with external names ๐Ÿซง

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

Otherwise, with this option, an external name such as MAX_COUNT gets implemented as a reference to the link-time external symbol max_count__, instead of max_count_ ๐Ÿ”— This is required for compatibility with g77 and f2c, and it's implied by the -ff2c option ๐Ÿ’œ

  • -fcoarray=<keyword> โ€” This picks which coarray support you get ๐Ÿฆ‹
    • none โ€” Disables coarray support, so using coarray declarations and image-control statements produces a compile-time error ๐Ÿšซ (Default)
  • single โ€” Single-image mode, so num_images() is always one. Think solo act, no backup dancers ๐Ÿ’…
  • lib โ€” Library-based coarray parallelization. You'll need to link a suitable GNU Fortran coarray library for this one โœจ

๐ŸŽ€ -fcheck=<keyword>

Turns on run-time checks. The argument is a comma-delimited list of the keywords below. Slap a no- in front of any check to switch it off, but only if an earlier specification turned it on ๐Ÿ™ˆ

  • all โ€” Turns on every run-time test that -fcheck offers ๐Ÿ‘‘
  • array-temps โ€” Warns at run time when a temporary array had to be made to pass an actual argument. This info can be handy for optimization, since it helps you dodge those temporaries ๐Ÿฆ‹
    • Note: The warning only prints once per location, so don't panic when it goes quiet ๐Ÿ’œ
  • bits โ€” Run-time checks for invalid arguments to the bit manipulation intrinsics ๐Ÿงš
  • bounds โ€” Run-time checks for array subscripts against the declared minimum and maximum values. It also checks array indices for assumed and deferred shape arrays against the actual allocated bounds, and makes sure all string lengths match for character array constructors that don't have an explicit typespec ๐ŸŒธ
    • Some checks need -fcheck=bounds set when the main program gets compiled ๐Ÿซถ
    • Note: Future versions might add other kinds of checking too, like checking substring references ๐Ÿ”ฎ
  • do โ€” Run-time checks for invalid modification of loop iteration variables ๐Ÿ“
  • mem โ€” Run-time checks for memory allocation ๐Ÿ’–
    • Note: This one does not touch explicit allocations made with the ALLOCATE statement. Those are always checked, no exceptions ๐Ÿ’€
  • pointer โ€” Run-time checks for pointers and allocatables ๐ŸŒˆ
  • recursion โ€” 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. It also gets disabled if you use it together with -frecursive and -fopenmp ๐Ÿ˜ญ

Example: say you've got a file called foo.f90. This command:

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

will compile the file with every check turned on, except the warnings for generated array temporaries โœจ

  • -fbounds-check โ€” Deprecated alias for -fcheck=bounds. It's on its way out, so switch to the new spelling ๐Ÿซถ
  • -ftail-call-workaround, -ftail-call-workaround=n โ€” Okay, this one's a whole saga ๐ŸŽญ Some C interfaces to Fortran code break the gfortran ABI by leaving out the hidden character length arguments (the source text cuts off right at "as described in", so that's all the detail there is). This can cause crashes, because pushing arguments for tail calls can overflow the stack ๐Ÿ’€
    • This option works around that for existing binary packages by turning off tail call optimization for gfortran procedures that have character arguments.
    • -ftail-call-workaround=2 turns tail call optimization off in all gfortran procedures with character arguments.
    • -ftail-call-workaround=1, or just plain -ftail-call-workaround, only does it for gfortran procedures with character arguments that call implicitly prototyped procedures.
    • Heads up: Using this can cause problems, including crashes from not having enough stack space ๐Ÿ˜ญ
    • It is very strongly recommended that you fix the code in question instead. The -fc-prototypes-external option can generate prototypes that follow gfortran's ABI, so you can drop them into your source code ๐ŸŒธ
    • Support for this option will probably be withdrawn in a future gfortran release, so don't get too attached ๐Ÿฅ€
    • To turn it off, use -fno-tail-call-workaround or -ftail-call-workaround=0 ๐Ÿ™…โ€โ™€๏ธ
    • Right now the default is -ftail-call-workaround, but that's going to change in future releases โญ
  • -fcheck-array-temporaries โ€” Deprecated alias for -fcheck=array-temps. Same vibe, shorter spelling is the new move ๐Ÿ’…
  • -fmax-array-constructor=n โ€” Lets you raise the upper limit allowed in array constructors. The code below needs this option so it can expand the array at compile time ๐Ÿงฎ
program test
implicit none
integer j
integer, parameter :: n = 100000
integer, parameter :: i(n) = (/ (2*j, j = 1, n) /)
print '(10(I0,1X))', i
end program test
Caution: This option can lead to really long compile times and excessively large object files. Proceed with love but also with snacks ๐Ÿฟ

The default value for n is 65535.

  • -fmax-stack-var-size=n โ€” Sets the size in bytes of the biggest array that can go on the stack. If an array is bigger than that, it uses static memory instead (except in procedures marked RECURSIVE) ๐Ÿงฑ
    • Use -frecursive to allow recursive procedures that don't have a RECURSIVE attribute, or for parallel programs ๐Ÿ”
    • Use -fno-automatic if you want to never use the stack ๐Ÿšซ
    • Note: This only affects local arrays declared with constant bounds, and it may not apply to every character variable. Future versions of GNU Fortran might improve this ๐ŸŒฑ
    • The default value for n is 65536.
  • -fstack-arrays โ€” Puts all arrays of unknown size, plus array temporaries, onto stack memory. If your program uses super large local arrays, you might have to raise 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 together as possible. Code built with this is likely to be incompatible with code built 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 ๐Ÿ“ฆ
    • Result: faster array accesses, but it can add a lot of overhead to the function call, especially when the passed data is noncontiguous ๐Ÿ˜ฌ
  • -fshort-enums โ€” Here for interop with C code that was compiled using -fshort-enums. GNU Fortran will pick the smallest INTEGER kind that an enumerator set fits into, then give all of its enumerators that kind ๐Ÿงธ
  • -finline-arg-packing โ€” When you pass an assumed-shape argument of a procedure as an actual argument to an assumed-size or explicit size procedure (or to one without an explicit interface), the argument might have to be packed, meaning put into contiguous memory. Here's the classic example, a call to foo in:
subroutine foo(a)
   real, dimension(*) :: a
end subroutine foo
subroutine bar(b)
   real, dimension(:) :: b
   call foo(b)
end subroutine bar

With -finline-arg-packing in effect, that packing is done by inline code. That means more optimization, but also a bigger code size ๐Ÿ“ˆ

  • It's implied by any of the -O options, except when you're optimizing for size with -Os ๐Ÿง 
  • If your code has a very large number of arguments that need packing, code size and compile time can get out of hand. In that case, disabling this option might be the better move ๐Ÿ™ƒ
  • To find instances of packing, use -Warray-temporaries ๐Ÿ”
  • -fexternal-blas โ€” Makes gfortran call BLAS functions for some matrix operations, like MATMUL, instead of using its own algorithms. This only happens when the matrices are bigger than a given limit (see -fblas-matmul-limit) ๐Ÿงฎ This can pay off if you have an optimized vendor BLAS library available. The BLAS library has to be specified at link time ๐Ÿ”—
  • -fblas-matmul-limit=n โ€” Only matters when -fexternal-blas is in effect. Matrix multiplications with a size larger than, or equal to, n get handled by BLAS calls, while smaller ones use 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 figured out at compile time, since code for both cases gets generated ๐Ÿซ 
    • -finline-matmul-limit=0 turns inlining off in all cases ๐Ÿ›‘
    • Setting it to some value n produces inline code for matrices with a size up to n. If the matrices aren't square, the size comparison uses the geometric mean of the dimensions of the argument and result matrices ๐Ÿ“
    • The default value for n is 30. The -fblas-matmul-limit option can be used to change this value ๐Ÿ”„
  • -frecursive โ€” Allows indirect recursion by forcing all local arrays to be allocated on the stack ๐ŸŒ€ This flag can't be used together with -fmax-stack-var-size= or -fno-automatic ๐Ÿ™…โ€โ™€๏ธ
  • -finit-local-zero, -finit-derived, -finit-integer=n, -finit-real=<zero|inf|-inf|nan|snan>, -finit-logical=<true|false>, -finit-character=n โ€” The initialization squad ๐ŸงŠ
    • -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 0๏ธโƒฃ
    • For finer 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. Here n is an ASCII character value for the character one ๐Ÿ”ก
    • 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 ๐Ÿงฌ
    • Not included: These options do not initialize:
      • objects with the POINTER attribute ๐Ÿšซ
      • allocatable arrays ๐Ÿšซ
      • variables that show up in an EQUIVALENCE statement ๐Ÿšซ

(psst, these limitations might get removed in future releases ๐ŸŒธ)

okay so -finit-real=nan sets up REAL and COMPLEX variables with a quiet NaN โœจ. If you want a signalling NaN, use -finit-real=snan instead. ngl though, compile-time optimizations can quietly turn those into quiet NaNs ๐Ÿ’€, and trapping has to be enabled too (for example with -ffpe-trap) ๐Ÿซถ

The -finit-integer option parses the value into an integer of type INTEGER(kind=C_LONG) on the host. That value then gets assigned to the integer variables in your Fortran code, and if it's too big for the kind, it can wrap around ๐Ÿ˜ฌ

And last but not least, turning on any of the -finit-* options silences the warnings -Wuninitialized would've thrown for the affected local variables. main character energy ๐Ÿ‘‘

  • -falign-commons โ€” By default, gfortran pads every variable in a COMMON block so it's properly aligned ๐Ÿ’…. On some platforms this is mandatory, and on others it just boosts performance. If a COMMON block isn't declared with consistent data types everywhere, that padding can cause trouble, so -fno-align-commons lets you turn automatic alignment off. Use the same form of this option for every file that shares 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, parentheses in expressions are honored at every optimization level, so the compiler doesn't re-associate anything ๐Ÿฆ‹. Using -fno-protect-parens lets it reorder REAL and COMPLEX expressions for faster code. For that re-association to happen, -fno-signed-zeros and -fno-trapping-math need to be in effect. Parentheses protection is on by default, unless -Ofast is given.
  • -frealloc-lhs โ€” An allocatable left-hand side of an intrinsic assignment gets automatically (re)allocated if it's either unallocated or has a different shape โœจ. This is enabled by default, except when -std=f95 is given. See also -Wrealloc-lhs.
  • -faggressive-function-elimination โ€” Functions with identical argument lists get eliminated within statements, whether or not they're marked PURE ๐Ÿงš. For example, in:
a = f(b,c) + f(b,c)

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

  • -ffrontend-optimize โ€” This option does 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 it turns on include:
    • inlining calls to MATMUL,
    • elimination of identical function calls within expressions,
    • removing unnecessary calls to TRIM in comparisons and assignments,
    • replacing TRIM(a) with a(1:LEN_TRIM(a)), and
    • short-circuiting of logical operators (.AND. and .OR.).

If you don't vibe with it, you can deselect it by specifying -fno-frontend-optimize.

  • -ffrontend-loop-interchange โ€” This one attempts to interchange loops in the Fortran front end wherever it's profitable ๐Ÿ”ฅ. It's enabled by default by any -O option. Right now it only affects FORALL and DO CONCURRENT statements that have several forall triplets.

๐ŸŒ ENVIRONMENT ๐ŸŒ

The gfortran compiler doesn't use any environment variables to control its operation beyond the ones that affect the operation of gcc. So just gcc's vibes ๐Ÿ’œ


๐Ÿ› BUGS ๐Ÿ›

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


๐Ÿ”— SEE ALSO ๐Ÿ”—

Here's your reading list, bestie ๐Ÿ“š: gpl(7), gfdl(7), fsf-funding(7), cpp(1), gcov(1), gcc(1), as(1), ld(1), gdb(1), dbx(1), plus the Info entries for gcc, cpp, gfortran, as, ld, binutils and gdb ๐Ÿงš


โœ๏ธ AUTHOR โœ๏ธ

Check the Info entry for gfortran to see the contributors to GCC and GNU Fortran ๐Ÿซถ


Copyright (c) 2004-2022 Free Software Foundation, Inc. ๐Ÿ’–

Okay, legal stuff, but it's actually cute: you're allowed to copy, distribute and/or modify this document under the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation. The Invariant Sections are "Funding Free Software", the Front-Cover Texts are (a) (see below), and the Back-Cover Texts are (b) (see below). A copy of the license is included in the gfdl(7) man page ๐Ÿ“œ

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

A GNU Manual

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

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

That's the whole page, bestie! ๐ŸŽ€ Source gcc-12, dated 2022-02-22, and you literally made it through the entire gfortran manual. You're so iconic ๐Ÿ’…๐ŸŒธ

๐Ÿ“ฆ where this page comes from