]> Savannah Git Hosting - gnulib.git/commitdiff
getopt: Fix link error for users of getopt() in <unistd.h>.
authorBruno Haible <bruno@clisp.org>
Sun, 18 Dec 2016 12:07:43 +0000 (13:07 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 18 Dec 2016 12:08:25 +0000 (13:08 +0100)
* lib/getopt.in.h (getopt etc.): Do the macro definitions also when
__need_getopt is defined. Undefine all macros before defining them.
* modules/getopt (Include): Clarify that including <unistd.h> is also
OK.
* tests/test-getopt.c: Add comment.

ChangeLog
lib/getopt.in.h
modules/getopt
tests/test-getopt.c

index fdf61ab0ffbdc355e759bfd8f6ec6687006db6ea..d9834b5b1e4c762c8e532c43dff6fc63520fc9aa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2016-12-18  Bruno Haible  <bruno@clisp.org>
+
+       getopt: Fix link error for users of getopt() in <unistd.h>.
+       * lib/getopt.in.h (getopt etc.): Do the macro definitions also when
+       __need_getopt is defined. Undefine all macros before defining them.
+       * modules/getopt (Include): Clarify that including <unistd.h> is also
+       OK.
+       * tests/test-getopt.c: Add comment.
+
 2016-12-17  Bruno Haible  <bruno@clisp.org>
 
        getaddrinfo tests: Avoid compilation error on MSVC.
index 0f7218278f004ed9b454d2f9f6f5d633b1c23906..64469b7c6bd3bb5217c66b3fe1906c23ca128e3d 100644 (file)
    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_GETOPT_H@
-#  define __need_system_stdlib_h
-#  include <stdlib.h>
-#  undef __need_system_stdlib_h
-#  include <stdio.h>
-#  include <unistd.h>
+#if defined __GETOPT_PREFIX
+# if !defined __need_getopt
+#  if !@HAVE_GETOPT_H@
+#   define __need_system_stdlib_h
+#   include <stdlib.h>
+#   undef __need_system_stdlib_h
+#   include <stdio.h>
+#   include <unistd.h>
+#  endif
+#  undef __need_getopt
 # endif
-# undef __need_getopt
+# undef __GETOPT_CONCAT
+# undef __GETOPT_XCONCAT
+# undef __GETOPT_ID
 # undef getopt
 # undef getopt_long
 # undef getopt_long_only
@@ -64,6 +69,7 @@
 # undef optind
 # undef optopt
 # undef option
+# undef _getopt_internal
 # define __GETOPT_CONCAT(x, y) x ## y
 # define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
 # define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
index 5d4f3c4989092e3b14d8cf6060db3526c6a2ebcd..53c844d8b0c6b01e7b4a70d778d67fc14dc65401 100644 (file)
@@ -17,7 +17,7 @@ configure.ac:
 Makefile.am:
 
 Include:
-<getopt.h>
+<getopt.h> or <unistd.h>
 
 License:
 LGPL
index 505cb2a994f4475b4ecfb25d37eb85401797ae78..04e1ae674b92cfb91998669f9c2bb7646990cd6f 100644 (file)
@@ -38,7 +38,17 @@ SIGNATURE_CHECK (getopt_long_only, int, (int, char *__getopt_argv_const *,
 
 #endif
 
-#include <unistd.h>
+/* POSIX and glibc provide the getopt() function in <unistd.h>, see
+   http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html
+   https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html
+   But gnulib provides the getopt() function in <getopt.h>, not in <unistd.h>.
+   Nevertheless the getopt() function should also be found in <unistd.h>.
+   We can test it either way.  */
+#if 0
+# include <getopt.h>
+#else
+# include <unistd.h>
+#endif
 
 #include "signature.h"
 SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *));