Discussion:
redefinition errors on MSVC
(too old to reply)
Bruno Haible
2016-12-17 22:10:18 UTC
Permalink
Raw Message
Compiling a gnulib testdir with MSVC 14, I get these errors:

/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../gltests -I.. -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o test-binary-io.obj `cygpath -w '../../gltests/test-binary-io.c'`
test-binary-io.c
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdio.h(380): error C2375: 'rpl_unlink': redefinition; different linkage
../gllib\unistd.h(1849): note: see declaration of 'rpl_unlink'
make[4]: *** [Makefile:8027: test-binary-io.obj] Error 2

/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../gltests -I.. -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o test-dprintf-posix2.obj `cygpath -w '../../gltests/test-dprintf-posix2.c'`
test-dprintf-posix2.c
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdlib.h(230): error C2375: 'rpl_perror': redefinition; different linkage
../gllib\stdio.h(1160): note: see declaration of 'rpl_perror'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_io.h(254): error C2375: 'rpl_remove': redefinition; different linkage
../gllib\stdio.h(1296): note: see declaration of 'rpl_remove'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_io.h(259): error C2375: 'rpl_rename': redefinition; different linkage
../gllib\stdio.h(1317): note: see declaration of 'rpl_rename'
make[4]: *** [Makefile:8027: test-dprintf-posix2.obj] Error 2

This fixes it.


2016-12-17 Bruno Haible <***@clisp.org>

Avoid redefinition errors on MSVC.
* lib/stdio.in.h: Include <stdlib.h> and <io.h> when necessary.
* lib/unistd.in.h: Include <stdio.h> when necessary.

diff --git a/lib/stdio.in.h b/lib/stdio.in.h
index 4a6e9ff..fff16a5 100644
--- a/lib/stdio.in.h
+++ b/lib/stdio.in.h
@@ -118,6 +118,26 @@
# include <unistd.h>
#endif

+/* MSVC declares 'perror' in <stdlib.h>, not in <stdio.h>. We must include
+ it before we #define perror rpl_perror. */
+/* But in any case avoid namespace pollution on glibc systems. */
+#if (@GNULIB_PERROR@ || defined GNULIB_POSIXCHECK) \
+ && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__) \
+ && ! defined __GLIBC__
+# include <stdlib.h>
+#endif
+
+/* MSVC declares 'remove' in <io.h>, not in <stdio.h>. We must include
+ it before we #define remove rpl_remove. */
+/* MSVC declares 'rename' in <io.h>, not in <stdio.h>. We must include
+ it before we #define rename rpl_rename. */
+/* But in any case avoid namespace pollution on glibc systems. */
+#if (@GNULIB_REMOVE@ || @GNULIB_RENAME@ || defined GNULIB_POSIXCHECK) \
+ && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__) \
+ && ! defined __GLIBC__
+# include <io.h>
+#endif
+

/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */

diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 686eb81..e7e977c 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -55,9 +55,13 @@
#include <stddef.h>

/* mingw doesn't define the SEEK_* or *_FILENO macros in <unistd.h>. */
+/* MSVC declares 'unlink' in <stdio.h>, not in <unistd.h>. We must include
+ it before we #define unlink rpl_unlink. */
/* Cygwin 1.7.1 declares symlinkat in <stdio.h>, not in <unistd.h>. */
/* But avoid namespace pollution on glibc systems. */
#if (!(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET) \
+ || ((@GNULIB_UNLINK@ || defined GNULIB_POSIXCHECK) \
+ && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)) \
|| ((@GNULIB_SYMLINKAT@ || defined GNULIB_POSIXCHECK) \
&& defined __CYGWIN__)) \
&& ! defined __GLIBC__
--
2.6.4
Loading...