]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
added AC_F77_{DUMMY_}MAIN
authorSteven G. Johnson <stevenj@alum.mit.edu>
Sun, 1 Jul 2001 19:41:20 +0000 (19:41 +0000)
committerSteven G. Johnson <stevenj@alum.mit.edu>
Sun, 1 Jul 2001 19:41:20 +0000 (19:41 +0000)
ChangeLog
NEWS
aclang.m4
doc/autoconf.texi
lib/autoconf/c.m4
lib/autoconf/fortran.m4
lib/autoconf/lang.m4

index 67b619409fda79f5eb7d5aa167acfb42e2d6010d..8d904ebb5fc50d1f3b25abcb5576d09921374fbd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2001-07-01  Steven G. Johnson  <stevenj@alum.mit.edu>
+
+       Add alternate 'main' routine detection for linking C/C++ with Fortran,
+       fixing link failures for e.g. AC_F77_WRAPPERS on NetBSD.
+
+       * aclang.m4 (AC_F77_DUMMY_MAIN): New macro to detect whether a
+       dummy alternate main is required even if the user provides her own
+       'main'.
+       (AC_F77_MAIN): New macro to detect whether it is possible to
+       provide an alternate 'main' function name, using the 'main' from
+       the Fortran libraries.
+       (AC_LANG_PROGRAM(C)): Use F77_DUMMY_MAIN, if it is defined, so that
+       cross-language link tests can be performed successfully.
+       (_AC_F77_NAME_MANGLING): Require AC_F77_DUMMY_MAIN.  Also put $FLIBS
+       after $LIBS, for consistency; this should be the general rule since
+       the user may want to link to Fortran libraries that require $FLIBS.
+       * autoconf.texi: Document AC_F77_DUMMY_MAIN and AC_F77_MAIN.
+
 2001-06-29  Pavel Roskin  <proski@gnu.org>
 
        * atgeneral.m4 (AT_CHECK): Add a newline to the end of
diff --git a/NEWS b/NEWS
index 52013f5935206b2ec7522e878535673dd10571de..574fc1c65c57aeef27ae3987b55f1b79b6c6d1af 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,8 @@
 - `config.status foo' works properly when `foo' depends on variables
   set in an AC_CONFIG_THING INIT-CMD.
 - autoheader is more robust to broken input.
+- Fixed Fortran name-mangling and link tests on a number of systems,
+  e.g. NetBSD; see AC_F77_DUMMY_MAIN, below.
 
 ** Generic macros
 - AC_CHECK_HEADER and AC_CHECK_HEADERS support a fourth argument to
@@ -45,7 +47,9 @@
   AM_FUNC_OBSTACK, and AM_FUNC_STRTOD are now activated.
   Be sure to read `Upgrading from Version 2.13' to understand why
   running `autoupdate' is needed.
-[2.50a]
+- AC_F77_DUMMY_MAIN, AC_F77_MAIN: new macros to detect whether
+  a main-like routine is required/possible when linking C/C++ with
+  Fortran.  Users of e.g. AC_F77_WRAPPERS should be aware of these.
 \f
 * Major changes in Autoconf 2.50
 
index 2d25034e1ed523b97bafcbf564a21a2f02b8adc2..f0189d413d20de4215228ed6f7183f71ef5c35e2 100644 (file)
--- a/aclang.m4
+++ b/aclang.m4
@@ -356,6 +356,12 @@ $1])
 # --------------------------------------
 m4_define([AC_LANG_PROGRAM(C)],
 [$1
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
 int
 main ()
 {
@@ -1896,6 +1902,98 @@ AC_SUBST(FLIBS)
 AC_LANG_POP(Fortran 77)dnl
 ])# AC_F77_LIBRARY_LDFLAGS
 
+# AC_F77_DUMMY_MAIN([ACTION-IF-FAIL], [ACTION-IF-NONE], [ACTION-IF-FOUND])
+# ---------------------
+# Detect name of dummy main routine required by the Fortran libraries,
+# (if any) and define F77_DUMMY_MAIN to this name (which should be
+# used for a dummy declaration, if it is defined).  On some systems,
+# linking a C program to the Fortran library does not work unless you
+# supply a dummy function called something like MAIN__.
+#
+# Execute ACTION-IF-FAIL if no way of successfully linking a C program
+# with the F77 libs is found; default to exiting with an error message.
+# Execute ACTION-IF-NONE if no dummy main is required for linking.
+# Execute ACTION-IF-FOUND if a dummy routine name is found (default to
+# defining F77_DUMMY_MAIN).
+#
+# What is technically happening is that the Fortran libraries provide
+# their own main() function, which usually initializes Fortran I/O
+# and similar stuff, and then calls MAIN__, which is the entry point
+# of your program.  Usually, a C program will override this with its
+# own main() routine, but the linker sometimes complain if you don't
+# provide a dummy (never-called) MAIN__ routine anyway.
+#
+# Of course, programs that want to allow Fortran subroutines to do
+# I/O, etcetera, should call their main routine MAIN__() (or whatever)
+# instead of main().   A separate autoconf test (AC_F77_MAIN) checks
+# for the routine to use in this case (since the semantics of the
+# test are slightly different).  To link to e.g. purely numerical
+# libraries, this is normally not necessary, however, and most C/C++
+# programs are reluctant to turn over so much control to Fortran.  =)
+#
+# The name variants we check for are (in order):
+#   MAIN__ (g77, MAIN__ required on some systems; IRIX, MAIN__ optional)
+#   MAIN_, __main (SunOS)
+#   MAIN _MAIN __MAIN main_ main__ _main (we follow DDD and try these too)
+#
+AC_DEFUN([AC_F77_DUMMY_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for dummy main to link with Fortran 77 libraries],
+               ac_cv_f77_dummy_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_dm_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+
+ # First, try linking without a dummy main:
+ AC_TRY_LINK([],[],ac_cv_f77_dummy_main=none,ac_cv_f77_dummy_main=unknown)
+
+ if test $ac_cv_f77_dummy_main = unknown; then
+   for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+     AC_TRY_LINK_FUNC($ac_func, [ac_cv_f77_dummy_main=$ac_func; break])
+   done
+ fi
+ LIBS=$ac_f77_dm_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+if test $ac_cv_f77_dummy_main = unknown; then
+  m4_default([$1],[AC_MSG_ERROR([Linking to Fortran libraries from C fails.])])
+elif test $ac_cv_f77_dummy_main = none; then
+  m4_default([$2],[:])
+else
+  m4_default([$3],
+    [AC_DEFINE_UNQUOTED([F77_DUMMY_MAIN], $ac_cv_f77_dummy_main,
+       [Define to dummy "main" function (if any) required to link to
+        the Fortran 77 libraries.])])
+fi
+])# AC_F77_DUMMY_MAIN
+
+# AC_F77_MAIN
+# ---------------------
+# Define F77_MAIN to name of alternate main() function for use with
+# the Fortran libraries.  (Typically, the libraries may define their
+# own main() to initialize I/O, etcetera, that then call your own
+# routine called MAIN__ or whatever.)  See AC_F77_DUMMY_MAIN, above.
+# If no such alternate name is found, just define F77_MAIN to main.
+#
+AC_DEFUN([AC_F77_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for alternate main to link with Fortran 77 libraries],
+               ac_cv_f77_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_m_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+ ac_cv_f77_main="main" # default entry point name
+
+ for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+   AC_TRY_LINK([#undef F77_DUMMY_MAIN
+#define main $ac_func], [], [ac_cv_f77_main=$ac_func; break])
+ done
+ LIBS=$ac_f77_m_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+AC_DEFINE_UNQUOTED([F77_MAIN], $ac_cv_f77_main, [Define to alternate name for
+       "main" routine that is called from a "main" in the Fortran libraries.])
+])# AC_F77_MAIN
 
 # _AC_F77_NAME_MANGLING
 # ---------------------
@@ -1914,6 +2012,7 @@ AC_LANG_POP(Fortran 77)dnl
 #
 AC_DEFUN([_AC_F77_NAME_MANGLING],
 [AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_REQUIRE([AC_F77_DUMMY_MAIN])dnl
 AC_CACHE_CHECK([for Fortran 77 name-mangling scheme],
                ac_cv_f77_mangling,
 [AC_LANG_PUSH(Fortran 77)dnl
@@ -1929,7 +2028,7 @@ AC_COMPILE_IFELSE(
   AC_LANG_PUSH(C)dnl
 
   ac_save_LIBS=$LIBS
-  LIBS="cf77_test.$ac_objext $FLIBS $LIBS"
+  LIBS="cf77_test.$ac_objext $LIBS $FLIBS"
 
   ac_success=no
   for ac_foobar in foobar FOOBAR; do
index 78234cf458d6ecc3ea9d6193e192f4f133afb2d7..169cfa63dcc8b663f2fbd089454ef8d1cf0fc279 100644 (file)
@@ -4670,6 +4670,69 @@ in as well, but the C++ compiler/linker doesn't know by default how to
 add these Fortran 77 libraries.  Hence, the macro
 @code{AC_F77_LIBRARY_LDFLAGS} was created to determine these Fortran 77
 libraries.
+
+The macro @code{AC_F77_DUMMY_MAIN} or @code{AC_F77_MAIN} will probably
+also be necessary to link C/C++ with Fortran; see below.
+@end defmac
+
+@defmac AC_F77_DUMMY_MAIN(@ovar{ACTION-IF-FAIL}, @ovar{ACTION-IF-NONE}, @ovar{ACTION-IF-FOUND})
+@maindex F77_DUMMY_MAIN
+@cvindex F77_DUMMY_MAIN
+With many compilers, the Fortran libraries detected by
+@code{AC_F77_LIBRARY_LDFLAGS} provide their own @code{main} entry
+function that initializes things like Fortran I/O, and which then calls
+a user-provided entry function named e.g. @code{MAIN__} to run the
+user's program.  The @code{AC_F77_DUMMY_MAIN} or @code{AC_F77_MAIN}
+macros figure out how to deal with this interaction.
+
+When using Fortran only for purely numerical functions (no I/O,
+etcetera), users often prefer to provide their own @code{main} and skip
+the Fortran library initializations.  In this case, however, one may
+still need to provide a dummy @code{MAIN__} routine in order to prevent
+linking errors on some systems.  @code{AC_F77_DUMMY_MAIN} detects
+whether any such routine is @emph{required} for linking, and what its
+name is.
+
+If it cannot figure out how to link successfully, @code{ACTION-IF-FAIL}
+is executed, with the default action being to exit with an error
+message.  @code{ACTION-IF-NONE} is executed if no dummy main is needed
+(default: no action).  @code{ACTION-IF-FOUND} is executed if a dummy
+main is required; the default action is to define @code{F77_DUMMY_MAIN}
+to the name of this required routine (e.g. @code{MAIN__}).
+
+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:
+
+@example
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() @{ return 1; @}
+#endif
+@end example
+
+Note that @code{AC_F77_DUMMY_MAIN} is called automatically from
+@code{AC_F77_WRAPPERS}; there is generally no need to call it explicitly
+unless one wants to change the default actions.
+@end defmac
+
+@defmac AC_F77_MAIN
+@maindex F77_MAIN
+@cvindex F77_MAIN
+As discussed above for @code{AC_F77_DUMMY_MAIN}, many Fortran libraries
+allow you to provide an entry point called e.g. @code{MAIN__} instead of
+the usual @code{main}, which is then called by a @code{main} function in
+the Fortran libraries that initializes things like Fortran I/O.  The
+@code{AC_F77_MAIN} macro detects whether it is @emph{possible} to
+utilize such an alternate main function, and defines @code{F77_MAIN} to
+the name of the function.  (If no alternate main function name is found,
+@code{F77_MAIN} is simply defined to @code{main}.)
+
+Thus, when calling Fortran routines from C that perform things like I/O,
+one should use this macro and name the "main" function @code{F77_MAIN}
+instead of @code{main}.
 @end defmac
 
 @defmac AC_F77_WRAPPERS
index 2d25034e1ed523b97bafcbf564a21a2f02b8adc2..f0189d413d20de4215228ed6f7183f71ef5c35e2 100644 (file)
@@ -356,6 +356,12 @@ $1])
 # --------------------------------------
 m4_define([AC_LANG_PROGRAM(C)],
 [$1
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
 int
 main ()
 {
@@ -1896,6 +1902,98 @@ AC_SUBST(FLIBS)
 AC_LANG_POP(Fortran 77)dnl
 ])# AC_F77_LIBRARY_LDFLAGS
 
+# AC_F77_DUMMY_MAIN([ACTION-IF-FAIL], [ACTION-IF-NONE], [ACTION-IF-FOUND])
+# ---------------------
+# Detect name of dummy main routine required by the Fortran libraries,
+# (if any) and define F77_DUMMY_MAIN to this name (which should be
+# used for a dummy declaration, if it is defined).  On some systems,
+# linking a C program to the Fortran library does not work unless you
+# supply a dummy function called something like MAIN__.
+#
+# Execute ACTION-IF-FAIL if no way of successfully linking a C program
+# with the F77 libs is found; default to exiting with an error message.
+# Execute ACTION-IF-NONE if no dummy main is required for linking.
+# Execute ACTION-IF-FOUND if a dummy routine name is found (default to
+# defining F77_DUMMY_MAIN).
+#
+# What is technically happening is that the Fortran libraries provide
+# their own main() function, which usually initializes Fortran I/O
+# and similar stuff, and then calls MAIN__, which is the entry point
+# of your program.  Usually, a C program will override this with its
+# own main() routine, but the linker sometimes complain if you don't
+# provide a dummy (never-called) MAIN__ routine anyway.
+#
+# Of course, programs that want to allow Fortran subroutines to do
+# I/O, etcetera, should call their main routine MAIN__() (or whatever)
+# instead of main().   A separate autoconf test (AC_F77_MAIN) checks
+# for the routine to use in this case (since the semantics of the
+# test are slightly different).  To link to e.g. purely numerical
+# libraries, this is normally not necessary, however, and most C/C++
+# programs are reluctant to turn over so much control to Fortran.  =)
+#
+# The name variants we check for are (in order):
+#   MAIN__ (g77, MAIN__ required on some systems; IRIX, MAIN__ optional)
+#   MAIN_, __main (SunOS)
+#   MAIN _MAIN __MAIN main_ main__ _main (we follow DDD and try these too)
+#
+AC_DEFUN([AC_F77_DUMMY_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for dummy main to link with Fortran 77 libraries],
+               ac_cv_f77_dummy_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_dm_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+
+ # First, try linking without a dummy main:
+ AC_TRY_LINK([],[],ac_cv_f77_dummy_main=none,ac_cv_f77_dummy_main=unknown)
+
+ if test $ac_cv_f77_dummy_main = unknown; then
+   for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+     AC_TRY_LINK_FUNC($ac_func, [ac_cv_f77_dummy_main=$ac_func; break])
+   done
+ fi
+ LIBS=$ac_f77_dm_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+if test $ac_cv_f77_dummy_main = unknown; then
+  m4_default([$1],[AC_MSG_ERROR([Linking to Fortran libraries from C fails.])])
+elif test $ac_cv_f77_dummy_main = none; then
+  m4_default([$2],[:])
+else
+  m4_default([$3],
+    [AC_DEFINE_UNQUOTED([F77_DUMMY_MAIN], $ac_cv_f77_dummy_main,
+       [Define to dummy "main" function (if any) required to link to
+        the Fortran 77 libraries.])])
+fi
+])# AC_F77_DUMMY_MAIN
+
+# AC_F77_MAIN
+# ---------------------
+# Define F77_MAIN to name of alternate main() function for use with
+# the Fortran libraries.  (Typically, the libraries may define their
+# own main() to initialize I/O, etcetera, that then call your own
+# routine called MAIN__ or whatever.)  See AC_F77_DUMMY_MAIN, above.
+# If no such alternate name is found, just define F77_MAIN to main.
+#
+AC_DEFUN([AC_F77_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for alternate main to link with Fortran 77 libraries],
+               ac_cv_f77_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_m_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+ ac_cv_f77_main="main" # default entry point name
+
+ for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+   AC_TRY_LINK([#undef F77_DUMMY_MAIN
+#define main $ac_func], [], [ac_cv_f77_main=$ac_func; break])
+ done
+ LIBS=$ac_f77_m_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+AC_DEFINE_UNQUOTED([F77_MAIN], $ac_cv_f77_main, [Define to alternate name for
+       "main" routine that is called from a "main" in the Fortran libraries.])
+])# AC_F77_MAIN
 
 # _AC_F77_NAME_MANGLING
 # ---------------------
@@ -1914,6 +2012,7 @@ AC_LANG_POP(Fortran 77)dnl
 #
 AC_DEFUN([_AC_F77_NAME_MANGLING],
 [AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_REQUIRE([AC_F77_DUMMY_MAIN])dnl
 AC_CACHE_CHECK([for Fortran 77 name-mangling scheme],
                ac_cv_f77_mangling,
 [AC_LANG_PUSH(Fortran 77)dnl
@@ -1929,7 +2028,7 @@ AC_COMPILE_IFELSE(
   AC_LANG_PUSH(C)dnl
 
   ac_save_LIBS=$LIBS
-  LIBS="cf77_test.$ac_objext $FLIBS $LIBS"
+  LIBS="cf77_test.$ac_objext $LIBS $FLIBS"
 
   ac_success=no
   for ac_foobar in foobar FOOBAR; do
index 2d25034e1ed523b97bafcbf564a21a2f02b8adc2..f0189d413d20de4215228ed6f7183f71ef5c35e2 100644 (file)
@@ -356,6 +356,12 @@ $1])
 # --------------------------------------
 m4_define([AC_LANG_PROGRAM(C)],
 [$1
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
 int
 main ()
 {
@@ -1896,6 +1902,98 @@ AC_SUBST(FLIBS)
 AC_LANG_POP(Fortran 77)dnl
 ])# AC_F77_LIBRARY_LDFLAGS
 
+# AC_F77_DUMMY_MAIN([ACTION-IF-FAIL], [ACTION-IF-NONE], [ACTION-IF-FOUND])
+# ---------------------
+# Detect name of dummy main routine required by the Fortran libraries,
+# (if any) and define F77_DUMMY_MAIN to this name (which should be
+# used for a dummy declaration, if it is defined).  On some systems,
+# linking a C program to the Fortran library does not work unless you
+# supply a dummy function called something like MAIN__.
+#
+# Execute ACTION-IF-FAIL if no way of successfully linking a C program
+# with the F77 libs is found; default to exiting with an error message.
+# Execute ACTION-IF-NONE if no dummy main is required for linking.
+# Execute ACTION-IF-FOUND if a dummy routine name is found (default to
+# defining F77_DUMMY_MAIN).
+#
+# What is technically happening is that the Fortran libraries provide
+# their own main() function, which usually initializes Fortran I/O
+# and similar stuff, and then calls MAIN__, which is the entry point
+# of your program.  Usually, a C program will override this with its
+# own main() routine, but the linker sometimes complain if you don't
+# provide a dummy (never-called) MAIN__ routine anyway.
+#
+# Of course, programs that want to allow Fortran subroutines to do
+# I/O, etcetera, should call their main routine MAIN__() (or whatever)
+# instead of main().   A separate autoconf test (AC_F77_MAIN) checks
+# for the routine to use in this case (since the semantics of the
+# test are slightly different).  To link to e.g. purely numerical
+# libraries, this is normally not necessary, however, and most C/C++
+# programs are reluctant to turn over so much control to Fortran.  =)
+#
+# The name variants we check for are (in order):
+#   MAIN__ (g77, MAIN__ required on some systems; IRIX, MAIN__ optional)
+#   MAIN_, __main (SunOS)
+#   MAIN _MAIN __MAIN main_ main__ _main (we follow DDD and try these too)
+#
+AC_DEFUN([AC_F77_DUMMY_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for dummy main to link with Fortran 77 libraries],
+               ac_cv_f77_dummy_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_dm_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+
+ # First, try linking without a dummy main:
+ AC_TRY_LINK([],[],ac_cv_f77_dummy_main=none,ac_cv_f77_dummy_main=unknown)
+
+ if test $ac_cv_f77_dummy_main = unknown; then
+   for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+     AC_TRY_LINK_FUNC($ac_func, [ac_cv_f77_dummy_main=$ac_func; break])
+   done
+ fi
+ LIBS=$ac_f77_dm_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+if test $ac_cv_f77_dummy_main = unknown; then
+  m4_default([$1],[AC_MSG_ERROR([Linking to Fortran libraries from C fails.])])
+elif test $ac_cv_f77_dummy_main = none; then
+  m4_default([$2],[:])
+else
+  m4_default([$3],
+    [AC_DEFINE_UNQUOTED([F77_DUMMY_MAIN], $ac_cv_f77_dummy_main,
+       [Define to dummy "main" function (if any) required to link to
+        the Fortran 77 libraries.])])
+fi
+])# AC_F77_DUMMY_MAIN
+
+# AC_F77_MAIN
+# ---------------------
+# Define F77_MAIN to name of alternate main() function for use with
+# the Fortran libraries.  (Typically, the libraries may define their
+# own main() to initialize I/O, etcetera, that then call your own
+# routine called MAIN__ or whatever.)  See AC_F77_DUMMY_MAIN, above.
+# If no such alternate name is found, just define F77_MAIN to main.
+#
+AC_DEFUN([AC_F77_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for alternate main to link with Fortran 77 libraries],
+               ac_cv_f77_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_m_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+ ac_cv_f77_main="main" # default entry point name
+
+ for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+   AC_TRY_LINK([#undef F77_DUMMY_MAIN
+#define main $ac_func], [], [ac_cv_f77_main=$ac_func; break])
+ done
+ LIBS=$ac_f77_m_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+AC_DEFINE_UNQUOTED([F77_MAIN], $ac_cv_f77_main, [Define to alternate name for
+       "main" routine that is called from a "main" in the Fortran libraries.])
+])# AC_F77_MAIN
 
 # _AC_F77_NAME_MANGLING
 # ---------------------
@@ -1914,6 +2012,7 @@ AC_LANG_POP(Fortran 77)dnl
 #
 AC_DEFUN([_AC_F77_NAME_MANGLING],
 [AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_REQUIRE([AC_F77_DUMMY_MAIN])dnl
 AC_CACHE_CHECK([for Fortran 77 name-mangling scheme],
                ac_cv_f77_mangling,
 [AC_LANG_PUSH(Fortran 77)dnl
@@ -1929,7 +2028,7 @@ AC_COMPILE_IFELSE(
   AC_LANG_PUSH(C)dnl
 
   ac_save_LIBS=$LIBS
-  LIBS="cf77_test.$ac_objext $FLIBS $LIBS"
+  LIBS="cf77_test.$ac_objext $LIBS $FLIBS"
 
   ac_success=no
   for ac_foobar in foobar FOOBAR; do
index 2d25034e1ed523b97bafcbf564a21a2f02b8adc2..f0189d413d20de4215228ed6f7183f71ef5c35e2 100644 (file)
@@ -356,6 +356,12 @@ $1])
 # --------------------------------------
 m4_define([AC_LANG_PROGRAM(C)],
 [$1
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
 int
 main ()
 {
@@ -1896,6 +1902,98 @@ AC_SUBST(FLIBS)
 AC_LANG_POP(Fortran 77)dnl
 ])# AC_F77_LIBRARY_LDFLAGS
 
+# AC_F77_DUMMY_MAIN([ACTION-IF-FAIL], [ACTION-IF-NONE], [ACTION-IF-FOUND])
+# ---------------------
+# Detect name of dummy main routine required by the Fortran libraries,
+# (if any) and define F77_DUMMY_MAIN to this name (which should be
+# used for a dummy declaration, if it is defined).  On some systems,
+# linking a C program to the Fortran library does not work unless you
+# supply a dummy function called something like MAIN__.
+#
+# Execute ACTION-IF-FAIL if no way of successfully linking a C program
+# with the F77 libs is found; default to exiting with an error message.
+# Execute ACTION-IF-NONE if no dummy main is required for linking.
+# Execute ACTION-IF-FOUND if a dummy routine name is found (default to
+# defining F77_DUMMY_MAIN).
+#
+# What is technically happening is that the Fortran libraries provide
+# their own main() function, which usually initializes Fortran I/O
+# and similar stuff, and then calls MAIN__, which is the entry point
+# of your program.  Usually, a C program will override this with its
+# own main() routine, but the linker sometimes complain if you don't
+# provide a dummy (never-called) MAIN__ routine anyway.
+#
+# Of course, programs that want to allow Fortran subroutines to do
+# I/O, etcetera, should call their main routine MAIN__() (or whatever)
+# instead of main().   A separate autoconf test (AC_F77_MAIN) checks
+# for the routine to use in this case (since the semantics of the
+# test are slightly different).  To link to e.g. purely numerical
+# libraries, this is normally not necessary, however, and most C/C++
+# programs are reluctant to turn over so much control to Fortran.  =)
+#
+# The name variants we check for are (in order):
+#   MAIN__ (g77, MAIN__ required on some systems; IRIX, MAIN__ optional)
+#   MAIN_, __main (SunOS)
+#   MAIN _MAIN __MAIN main_ main__ _main (we follow DDD and try these too)
+#
+AC_DEFUN([AC_F77_DUMMY_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for dummy main to link with Fortran 77 libraries],
+               ac_cv_f77_dummy_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_dm_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+
+ # First, try linking without a dummy main:
+ AC_TRY_LINK([],[],ac_cv_f77_dummy_main=none,ac_cv_f77_dummy_main=unknown)
+
+ if test $ac_cv_f77_dummy_main = unknown; then
+   for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+     AC_TRY_LINK_FUNC($ac_func, [ac_cv_f77_dummy_main=$ac_func; break])
+   done
+ fi
+ LIBS=$ac_f77_dm_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+if test $ac_cv_f77_dummy_main = unknown; then
+  m4_default([$1],[AC_MSG_ERROR([Linking to Fortran libraries from C fails.])])
+elif test $ac_cv_f77_dummy_main = none; then
+  m4_default([$2],[:])
+else
+  m4_default([$3],
+    [AC_DEFINE_UNQUOTED([F77_DUMMY_MAIN], $ac_cv_f77_dummy_main,
+       [Define to dummy "main" function (if any) required to link to
+        the Fortran 77 libraries.])])
+fi
+])# AC_F77_DUMMY_MAIN
+
+# AC_F77_MAIN
+# ---------------------
+# Define F77_MAIN to name of alternate main() function for use with
+# the Fortran libraries.  (Typically, the libraries may define their
+# own main() to initialize I/O, etcetera, that then call your own
+# routine called MAIN__ or whatever.)  See AC_F77_DUMMY_MAIN, above.
+# If no such alternate name is found, just define F77_MAIN to main.
+#
+AC_DEFUN([AC_F77_MAIN],
+[AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_CACHE_CHECK([for alternate main to link with Fortran 77 libraries],
+               ac_cv_f77_main,
+[AC_LANG_PUSH(C)dnl
+ ac_f77_m_save_LIBS=$LIBS
+ LIBS="$LIBS $FLIBS"
+ ac_cv_f77_main="main" # default entry point name
+
+ for ac_func in MAIN__ MAIN_ __main MAIN _MAIN __MAIN main_ main__ _main; do
+   AC_TRY_LINK([#undef F77_DUMMY_MAIN
+#define main $ac_func], [], [ac_cv_f77_main=$ac_func; break])
+ done
+ LIBS=$ac_f77_m_save_LIBS
+ AC_LANG_POP(C)dnl
+])
+AC_DEFINE_UNQUOTED([F77_MAIN], $ac_cv_f77_main, [Define to alternate name for
+       "main" routine that is called from a "main" in the Fortran libraries.])
+])# AC_F77_MAIN
 
 # _AC_F77_NAME_MANGLING
 # ---------------------
@@ -1914,6 +2012,7 @@ AC_LANG_POP(Fortran 77)dnl
 #
 AC_DEFUN([_AC_F77_NAME_MANGLING],
 [AC_REQUIRE([AC_F77_LIBRARY_LDFLAGS])dnl
+AC_REQUIRE([AC_F77_DUMMY_MAIN])dnl
 AC_CACHE_CHECK([for Fortran 77 name-mangling scheme],
                ac_cv_f77_mangling,
 [AC_LANG_PUSH(Fortran 77)dnl
@@ -1929,7 +2028,7 @@ AC_COMPILE_IFELSE(
   AC_LANG_PUSH(C)dnl
 
   ac_save_LIBS=$LIBS
-  LIBS="cf77_test.$ac_objext $FLIBS $LIBS"
+  LIBS="cf77_test.$ac_objext $LIBS $FLIBS"
 
   ac_success=no
   for ac_foobar in foobar FOOBAR; do