summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2016-12-18 12:07:43 (GMT)
committerBruno Haible <bruno@clisp.org>2016-12-18 12:08:25 (GMT)
commit571d60e6f9b53acd386d0cfe73e5c4fbf3a2c9da (patch)
treea72fbc3f7953731d5b705582d30e00e844497084
parent2ebd13d97452e41fbd2d1068b38f22a4b714fec3 (diff)
downloadgnulib-571d60e6f9b53acd386d0cfe73e5c4fbf3a2c9da.zip
gnulib-571d60e6f9b53acd386d0cfe73e5c4fbf3a2c9da.tar.gz
gnulib-571d60e6f9b53acd386d0cfe73e5c4fbf3a2c9da.tar.bz2
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.
-rw-r--r--ChangeLog9
-rw-r--r--lib/getopt.in.h22
-rw-r--r--modules/getopt2
-rw-r--r--tests/test-getopt.c12
4 files changed, 35 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index fdf61ab..d9834b5 100644
--- 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.
diff --git a/lib/getopt.in.h b/lib/getopt.in.h
index 0f72182..64469b7 100644
--- a/lib/getopt.in.h
+++ b/lib/getopt.in.h
@@ -47,15 +47,20 @@
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)
diff --git a/modules/getopt b/modules/getopt
index 5d4f3c4..53c844d 100644
--- a/modules/getopt
+++ b/modules/getopt
@@ -17,7 +17,7 @@ configure.ac:
Makefile.am:
Include:
-<getopt.h>
+<getopt.h> or <unistd.h>
License:
LGPL
diff --git a/tests/test-getopt.c b/tests/test-getopt.c
index 505cb2a..04e1ae6 100644
--- a/tests/test-getopt.c
+++ b/tests/test-getopt.c
@@ -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 *));