Index: common/getopt.cc
===================================================================
--- common/getopt.cc	(revision 14330)
+++ common/getopt.cc	(revision 14331)
@@ -82,7 +82,7 @@
    Also, when `ordering' is RETURN_IN_ORDER,
    each non-option ARGV-element is returned here.  */
 
-char *Xapian::Internal::optarg;
+char *optarg;
 
 /* Index in ARGV of the next element to be scanned.
    This is used for communication to and from the caller
@@ -97,7 +97,7 @@
    how much of ARGV has been scanned so far.  */
 
 /* 1003.2 says this must be 1 before any call.  */
-int Xapian::Internal::optind = 1;
+int optind = 1;
 
 /* Formerly, initialization of getopt depended on optind==0, which
    causes problems with re-calling getopt as programs generally don't
@@ -117,13 +117,13 @@
 /* Callers store zero here to inhibit the error message
    for unrecognized options.  */
 
-int Xapian::Internal::opterr = 1;
+int opterr = 1;
 
 /* Set to an option character which was unrecognized.
    This must be initialized on some systems to avoid linking in the
    system's own getopt implementation.  */
 
-int Xapian::Internal::optopt = '?';
+int optopt = '?';
 
 /* Describe how to deal with options that follow non-option ARGV-elements.
 
Index: common/gnu_getopt.h
===================================================================
--- common/gnu_getopt.h	(revision 14330)
+++ common/gnu_getopt.h	(revision 14331)
@@ -58,8 +58,14 @@
 
 #else
 
-// Put these variables in a namespace and then pull in so we don't end up
-// linking to versions in the C library.
+// POSIX says getopt() and optarg, etc are defined by <unistd.h>.  Some older
+// implementations have them in <stdio.h>.
+#include "safeunistd.h"
+#include <cstdio>
+
+// Put these variables in a namespace and then use macros to divert the
+// unqualified versions to the qualified ones, so we don't end up linking to
+// versions in the C library.
 namespace Xapian {
 namespace Internal {
 extern char *optarg;
@@ -68,10 +74,10 @@
 extern int optopt;
 }
 }
-using Xapian::Internal::optarg;
-using Xapian::Internal::optind;
-using Xapian::Internal::opterr;
-using Xapian::Internal::optopt;
+#define optarg Xapian::Internal::optarg
+#define optind Xapian::Internal::optind
+#define opterr Xapian::Internal::opterr
+#define optopt Xapian::Internal::optopt
 
 struct option {
     const char *name;

