Previous: Erlang Compiler and Interpreter, Up: Compilers and Preprocessors


5.10.7 Fortran Compiler Characteristics

The Autoconf Fortran support is divided into two categories: legacy Fortran 77 macros (F77), and modern Fortran macros (FC). The former are intended for traditional Fortran 77 code, and have output variables like F77, FFLAGS, and FLIBS. The latter are for newer programs that can (or must) compile under the newer Fortran standards, and have output variables like FC, FCFLAGS, and FCLIBS.

Except for two new macros AC_FC_SRCEXT and AC_FC_FREEFORM (see below), the FC and F77 macros behave almost identically, and so they are documented together in this section.

— Macro: AC_PROG_F77 ([compiler-search-list])

Determine a Fortran 77 compiler to use. If F77 is not already set in the environment, then check for g77 and f77, and then some other names. Set the output variable F77 to the name of the compiler found.

This macro may, however, be invoked with an optional first argument which, if specified, must be a blank-separated list of Fortran 77 compilers to search for. This just gives the user an opportunity to specify an alternative search list for the Fortran 77 compiler. For example, if you didn't like the default order, then you could invoke AC_PROG_F77 like this:

          AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
     

If using g77 (the GNU Fortran 77 compiler), then set the shell variable G77 to `yes'. If the output variable FFLAGS was not already set in the environment, then set it to -g -02 for g77 (or -O2 where g77 does not accept -g). Otherwise, set FFLAGS to -g for all other Fortran 77 compilers.

— Macro: AC_PROG_FC ([compiler-search-list], [dialect])

Determine a Fortran compiler to use. If FC is not already set in the environment, then dialect is a hint to indicate what Fortran dialect to search for; the default is to search for the newest available dialect. Set the output variable FC to the name of the compiler found.

By default, newer dialects are preferred over older dialects, but if dialect is specified then older dialects are preferred starting with the specified dialect. dialect can currently be one of Fortran 77, Fortran 90, or Fortran 95. However, this is only a hint of which compiler name to prefer (e.g., f90 or f95), and no attempt is made to guarantee that a particular language standard is actually supported. Thus, it is preferable that you avoid the dialect option, and use AC_PROG_FC only for code compatible with the latest Fortran standard.

This macro may, alternatively, be invoked with an optional first argument which, if specified, must be a blank-separated list of Fortran compilers to search for, just as in AC_PROG_F77.

If the output variable FCFLAGS was not already set in the environment, then set it to -g -02 for GNU g77 (or -O2 where g77 does not accept -g). Otherwise, set FCFLAGS to -g for all other Fortran compilers.

— Macro: AC_PROG_F77_C_O
— Macro: AC_PROG_FC_C_O

Test whether the Fortran compiler accepts the options -c and -o simultaneously, and define F77_NO_MINUS_C_MINUS_O or FC_NO_MINUS_C_MINUS_O, respectively, if it does not.

The following macros check for Fortran compiler characteristics. To check for characteristics not listed here, use AC_COMPILE_IFELSE (see Running the Compiler) or AC_RUN_IFELSE (see Runtime), making sure to first set the current language to Fortran 77 or Fortran via AC_LANG([Fortran 77]) or AC_LANG(Fortran) (see Language Choice).

— Macro: AC_F77_LIBRARY_LDFLAGS
— Macro: AC_FC_LIBRARY_LDFLAGS

Determine the linker flags (e.g., -L and -l) for the Fortran intrinsic and runtime libraries that are required to successfully link a Fortran program or shared library. The output variable FLIBS or FCLIBS is set to these flags (which should be included after LIBS when linking).

This macro is intended to be used in those situations when it is necessary to mix, e.g., C++ and Fortran source code in a single program or shared library (see Mixing Fortran 77 With C and C++).

For example, if object files from a C++ and Fortran compiler must be linked together, then the C++ compiler/linker must be used for linking (since special C++-ish things need to happen at link time like calling global constructors, instantiating templates, enabling exception support, etc.).

However, the Fortran intrinsic and runtime libraries must be linked in as well, but the C++ compiler/linker doesn't know by default how to add these Fortran 77 libraries. Hence, this macro was created to determine these Fortran libraries.

The macros AC_F77_DUMMY_MAIN and AC_FC_DUMMY_MAIN or AC_F77_MAIN and AC_FC_MAIN are probably also necessary to link C/C++ with Fortran; see below.

— Macro: AC_F77_DUMMY_MAIN ([action-if-found], [action-if-not-found])
— Macro: AC_FC_DUMMY_MAIN ([action-if-found], [action-if-not-found])

With many compilers, the Fortran libraries detected by AC_F77_LIBRARY_LDFLAGS or AC_FC_LIBRARY_LDFLAGS provide their own main entry function that initializes things like Fortran I/O, and which then calls a user-provided entry function named (say) MAIN__ to run the user's program. The AC_F77_DUMMY_MAIN and AC_FC_DUMMY_MAIN or AC_F77_MAIN and AC_FC_MAIN macros figure out how to deal with this interaction.

When using Fortran for purely numerical functions (no I/O, etc.) often one prefers to provide one's own main and skip the Fortran library initializations. In this case, however, one may still need to provide a dummy MAIN__ routine in order to prevent linking errors on some systems. AC_F77_DUMMY_MAIN or AC_FC_DUMMY_MAIN detects whether any such routine is required for linking, and what its name is; the shell variable F77_DUMMY_MAIN or FC_DUMMY_MAIN holds this name, unknown when no solution was found, and none when no such dummy main is needed.

By default, action-if-found defines F77_DUMMY_MAIN or FC_DUMMY_MAIN to the name of this routine (e.g., MAIN__) if it is required. action-if-not-found defaults to exiting with an error.

In order to link with Fortran routines, the user's C/C++ program should then include the following code to define the dummy main if it is needed:

          #ifdef F77_DUMMY_MAIN
          #  ifdef __cplusplus
               extern "C"
          #  endif
             int F77_DUMMY_MAIN() { return 1; }
          #endif
     

(Replace F77 with FC for Fortran instead of Fortran 77.)

Note that this macro is called automatically from AC_F77_WRAPPERS or AC_FC_WRAPPERS; there is generally no need to call it explicitly unless one wants to change the default actions.

— Macro: AC_F77_MAIN
— Macro: AC_FC_MAIN

As discussed above, many Fortran libraries allow you to provide an entry point called (say) MAIN__ instead of the usual main, which is then called by a main function in the Fortran libraries that initializes things like Fortran I/O. The AC_F77_MAIN and AC_FC_MAIN macros detect whether it is possible to utilize such an alternate main function, and defines F77_MAIN and FC_MAIN to the name of the function. (If no alternate main function name is found, F77_MAIN and FC_MAIN are simply defined to main.)

Thus, when calling Fortran routines from C that perform things like I/O, one should use this macro and declare the "main" function like so:

          #ifdef __cplusplus
            extern "C"
          #endif
          int F77_MAIN(int argc, char *argv[]);
     

(Again, replace F77 with FC for Fortran instead of Fortran 77.)

— Macro: AC_F77_WRAPPERS
— Macro: AC_FC_WRAPPERS

Defines C macros F77_FUNC (name, NAME), FC_FUNC (name, NAME), F77_FUNC_(name, NAME), and FC_FUNC_(name, NAME) to properly mangle the names of C/C++ identifiers, and identifiers with underscores, respectively, so that they match the name-mangling scheme used by the Fortran compiler.

Fortran is case-insensitive, and in order to achieve this the Fortran compiler converts all identifiers into a canonical case and format. To call a Fortran subroutine from C or to write a C function that is callable from Fortran, the C program must explicitly use identifiers in the format expected by the Fortran compiler. In order to do this, one simply wraps all C identifiers in one of the macros provided by AC_F77_WRAPPERS or AC_FC_WRAPPERS. For example, suppose you have the following Fortran 77 subroutine:

                subroutine foobar (x, y)
                double precision x, y
                y = 3.14159 * x
                return
                end
     

You would then declare its prototype in C or C++ as:

          #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
          #ifdef __cplusplus
          extern "C"  /* prevent C++ name mangling */
          #endif
          void FOOBAR_F77(double *x, double *y);
     

Note that we pass both the lowercase and uppercase versions of the function name to F77_FUNC so that it can select the right one. Note also that all parameters to Fortran 77 routines are passed as pointers (see Mixing Fortran 77 With C and C++).

(Replace F77 with FC for Fortran instead of Fortran 77.)

Although Autoconf tries to be intelligent about detecting the name-mangling scheme of the Fortran compiler, there may be Fortran compilers that it doesn't support yet. In this case, the above code generates a compile-time error, but some other behavior (e.g., disabling Fortran-related features) can be induced by checking whether F77_FUNC or FC_FUNC is defined.

Now, to call that routine from a C program, we would do something like:

          {
              double x = 2.7183, y;
              FOOBAR_F77 (&x, &y);
          }
     

If the Fortran identifier contains an underscore (e.g., foo_bar), you should use F77_FUNC_ or FC_FUNC_ instead of F77_FUNC or FC_FUNC (with the same arguments). This is because some Fortran compilers mangle names differently if they contain an underscore.

— Macro: AC_F77_FUNC (name, [shellvar])
— Macro: AC_FC_FUNC (name, [shellvar])

Given an identifier name, set the shell variable shellvar to hold the mangled version name according to the rules of the Fortran linker (see also AC_F77_WRAPPERS or AC_FC_WRAPPERS). shellvar is optional; if it is not supplied, the shell variable is simply name. The purpose of this macro is to give the caller a way to access the name-mangling information other than through the C preprocessor as above, for example, to call Fortran routines from some language other than C/C++.

— Macro: AC_FC_SRCEXT (ext, [action-if-success], [action-if-failure])

By default, the FC macros perform their tests using a .f extension for source-code files. Some compilers, however, only enable newer language features for appropriately named files, e.g., Fortran 90 features only for .f90 files. On the other hand, some other compilers expect all source files to end in .f and require special flags to support other file name extensions. The AC_FC_SRCEXT macro deals with both of these issues.

The AC_FC_SRCEXT tries to get the FC compiler to accept files ending with the extension .ext (i.e., ext does not contain the dot). If any special compiler flags are needed for this, it stores them in the output variable FCFLAGS_ext. This extension and these flags are then used for all subsequent FC tests (until AC_FC_SRCEXT is called again).

For example, you would use AC_FC_SRCEXT(f90) to employ the .f90 extension in future tests, and it would set a FCFLAGS_f90 output variable with any extra flags that are needed to compile such files.

The FCFLAGS_ext can not be simply absorbed into FCFLAGS, for two reasons based on the limitations of some compilers. First, only one FCFLAGS_ext can be used at a time, so files with different extensions must be compiled separately. Second, FCFLAGS_ext must appear immediately before the source-code file name when compiling. So, continuing the example above, you might compile a foo.f90 file in your makefile with the command:

          foo.o: foo.f90
               $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
     

If AC_FC_SRCEXT succeeds in compiling files with the ext extension, it calls action-if-success (defaults to nothing). If it fails, and cannot find a way to make the FC compiler accept such files, it calls action-if-failure (defaults to exiting with an error message).

— Macro: AC_FC_FREEFORM ([action-if-success], [action-if-failure])

The AC_FC_FREEFORM tries to ensure that the Fortran compiler ($FC) allows free-format source code (as opposed to the older fixed-format style from Fortran 77). If necessary, it may add some additional flags to FCFLAGS.

This macro is most important if you are using the default .f extension, since many compilers interpret this extension as indicating fixed-format source unless an additional flag is supplied. If you specify a different extension with AC_FC_SRCEXT, such as .f90 or .f95, then AC_FC_FREEFORM ordinarily succeeds without modifying FCFLAGS.

If AC_FC_FREEFORM succeeds in compiling free-form source, it calls action-if-success (defaults to nothing). If it fails, it calls action-if-failure (defaults to exiting with an error message).