environ: fix link error on 64-bit Cygwin
(too old to reply)
Bruno Haible
2018-01-31 10:42:15 UTC
Raw Message
On 64-bit Cygwin, a libunistring build fails like this:

$ /bin/sh ../libtool --tag=CC --preserve-dup-deps --mode=link x86_64-pc-cygwin-gcc -g -O2 -L/usr/local/cygwin64/lib -Wl,--disable-auto-import -o test-environ.exe test-environ.o libtests.a ../lib/libunistring.la libtests.a
libtool: link: x86_64-pc-cygwin-gcc -g -O2 -Wl,--disable-auto-import -o .libs/test-environ.exe test-environ.o -L/usr/local/cygwin64/lib libtests.a ../lib/.libs/libunistring.dll.a -liconv libtests.a -L/usr/local/cygwin64/lib
test-environ.o:test-environ.c:(.rdata$.refptr.environ[.refptr.environ]+0x0): undefined reference to `environ'
collect2: error: ld returned 1 exit status

This fixes it.

2018-01-31 Bruno Haible <***@clisp.org>

environ: Fix link error on 64-bit Cygwin.
* lib/unistd.in.h (environ): On Cygwin, redeclare with the
__declspec(dllimport) attribute.
* doc/posix-functions/environ.texi: Mention the Cygwin problem.

diff --git a/doc/posix-functions/environ.texi b/doc/posix-functions/environ.texi
index 34ac25a..a6c0095 100644
--- a/doc/posix-functions/environ.texi
+++ b/doc/posix-functions/environ.texi
@@ -24,6 +24,9 @@ shared libraries on Mac OS X 10.5. Here is a workaround: Instead, one can use
#define environ (*_NSGetEnviron())
@end smallexample
This works at all versions of Mac OS X.
+On Cygwin in 64-bit mode, references to this variable cause a link error when
+the option @code{-Wl,--disable-auto-import} is in use.
@end itemize

Portability problems not fixed by Gnulib:
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 6802e3b..4ef0ffa 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -400,6 +400,13 @@ _GL_WARN_ON_USE (dup3, "dup3 is unportable - "

+# if defined __CYGWIN__
+/* The 'environ' variable is defined in a DLL. Therefore its declaration needs
+ the '__declspec(dllimport)' attribute, but the system's <unistd.h> lacks it.
+ This leads to a link error on 64-bit Cygwin when the option
+ -Wl,--disable-auto-import is in use. */
+_GL_EXTERN_C __declspec(dllimport) char **environ;
+# endif
/* Set of environment variables and values. An array of strings of the form
"VARIABLE=VALUE", terminated with a NULL. */