bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] Getopt fix installed


From: Paul Eggert
Subject: Re: [Bug-gnulib] Getopt fix installed
Date: Thu, 11 Nov 2004 12:31:25 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Simon Josefsson <address@hidden> writes:

> It seems that the mapping of opterr was forgotten.
> Or was that intentional somehow?

No, it was an error.

> internal.h:58: warning: could not use precompiled header 
> '/usr/include/unistd.p', because:
> internal.h:58: warning: macro 'optarg' defined by ../config.h conflicts with 
> precomp

That's annoying, and it's a symptom of a deeper problem: currently
gnulib-using code #defines getopt before #including <unistd.h>, and
this can cause problems if <unistd.h> attempts to #define getopt.

This sort of thing is a problem with the entire rpl_* strategy, of
course; but it seems to be biting getopt harder than other modules,
possibly because getopt defines external variables.

I installed the following patch to attempt to work around this problem.

2004-11-11  Paul Eggert  <address@hidden>

        * lib/getopt_.h [defined __GETOPT_PREFIX && !defined __need_getopt]:
        Include <unistd.h> if available.
        Then rename getopt to __GETOPT_PREFIX##getopt, and so forth for
        the other external symbols.
        (getopt) [!defined __GNU_LIBRARY]: Use prototype, not old-style
        declaration, since the above renaming now works around collisions.

        * m4/getopt.m4 (gl_GETOPT_SUBSTITUTE): Define __GETOPT_PREFIX instead
        of replacing opterr, getopt, etc.  This should handle the
        powerpc-apple-darwin5.5 problem recently noted by Simon Josefsson.

Index: lib/getopt_.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getopt_.h,v
retrieving revision 1.1
diff -p -u -r1.1 getopt_.h
--- lib/getopt_.h       12 Aug 2004 08:05:22 -0000      1.1
+++ lib/getopt_.h       11 Nov 2004 20:12:23 -0000
@@ -23,6 +23,34 @@
 # define _GETOPT_H 1
 #endif
 
+/* Standalone applications should #define __GETOPT_PREFIX to an
+   identifier that prefixes the external functions and variables
+   defined in this header.  When this happens, include <unistd.h> (the
+   standard header declaring getopt), so that it will not cause
+   confusion if it is included later.  Then systematically rename
+   identifiers so that they do not collide with the system functions
+   and variables.  Renaming avoids problems with some compilers and
+   linkers.  */
+#if defined __GETOPT_PREFIX && !defined __need_getopt
+# if HAVE_UNISTD_H
+#  include <unistd.h>
+# endif
+# undef getopt
+# undef getopt_long
+# undef getopt_long_only
+# undef optarg
+# undef opterr
+# undef optind
+# undef optopt
+# define getopt __GETOPT_PREFIX##getopt
+# define getopt_long __GETOPT_PREFIX##getopt_long
+# define getopt_long_only __GETOPT_PREFIX##getopt_long_only
+# define optarg __GETOPT_PREFIX##optarg
+# define opterr __GETOPT_PREFIX##opterr
+# define optind __GETOPT_PREFIX##optind
+# define optopt __GETOPT_PREFIX##optopt
+#endif
+
 /* If __GNU_LIBRARY__ is not already defined, either we are being used
    standalone, or this is the first header included in the source file.
    If we are being used with glibc, we need to include <features.h>, but
@@ -144,15 +172,8 @@ struct option
    arguments to the option '\0'.  This behavior is specific to the GNU
    `getopt'.  */
 
-#ifdef __GNU_LIBRARY__
-/* Many other libraries have conflicting prototypes for getopt, with
-   differences in the consts, in stdlib.h.  To avoid compilation
-   errors, only prototype getopt for the GNU C library.  */
 extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
        __THROW;
-#else /* not __GNU_LIBRARY__ */
-extern int getopt ();
-#endif /* __GNU_LIBRARY__ */
 
 #ifndef __need_getopt
 extern int getopt_long (int ___argc, char *const *___argv,
Index: m4/getopt.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/getopt.m4,v
retrieving revision 1.5
diff -p -u -r1.5 getopt.m4
--- m4/getopt.m4        11 Nov 2004 15:32:19 -0000      1.5
+++ m4/getopt.m4        11 Nov 2004 20:12:23 -0000
@@ -1,4 +1,4 @@
-# getopt.m4 serial 5
+# getopt.m4 serial 6
 dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
@@ -15,20 +15,9 @@ AC_DEFUN([gl_GETOPT_SUBSTITUTE],
   GETOPT_H=getopt.h
   AC_LIBOBJ([getopt])
   AC_LIBOBJ([getopt1])
-  AC_DEFINE([optarg], [rpl_optarg],
-    [Define to rpl_optarg if the replacement variable should be used.])
-  AC_DEFINE([optind], [rpl_optind],
-    [Define to rpl_optind if the replacement variable should be used.])
-  AC_DEFINE([opterr], [rpl_opterr],
-    [Define to rpl_opterr if the replacement variable should be used.])
-  AC_DEFINE([optopt], [rpl_optopt],
-    [Define to rpl_optopt if the replacement variable should be used.])
-  AC_DEFINE([getopt], [rpl_getopt],
-    [Define to rpl_getopt if the replacement function should be used.])
-  AC_DEFINE([getopt_long], [rpl_getopt_long],
-    [Define to rpl_getopt_long if the replacement function should be used.])
-  AC_DEFINE([getopt_long_only], [rpl_getopt_long_only],
-    [Define to rpl_getopt_long_only if the replacement function should be 
used.])
+  AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
+    [Define to rpl_ if the getopt replacement functions and variables
+     should be used.])
   AC_SUBST([GETOPT_H])
 ])
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]