In a testdir created through
  ./gnulib-tool --create-testdir --dir=../testdir1 --single-configure 
--with-c++-tests stdbit
on MSVC 14 I see a compilation error:

/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -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-unistd-c++.obj `cygpath -w '../../gltests/test-unistd-c++.cc'`
test-unistd-c++.cc
.\unistd.h(2457): error C2440: 'return': cannot convert from 'int (__cdecl 
*)(int,void *,unsigned int)' to 'gnulib::_gl_read_wrapper::type'
.\unistd.h(2457): note: This conversion requires a reinterpret_cast, a C-style 
cast or function-style cast
.\unistd.h(2925): error C2440: 'return': cannot convert from 'int (__cdecl 
*)(int,const void *,unsigned int)' to 'gnulib::_gl_write_wrapper::type'
.\unistd.h(2925): note: This conversion requires a reinterpret_cast, a C-style 
cast or function-style cast
make[4]: *** [Makefile:2245: test-unistd-c++.obj] Error 2

The cause is that the _read and _write function are declared with 'int'
as return type. Whereas we want the functions to have the return type
ssize_t = 'long'. Both have the same size in native Windows, therefore
just casting the functions pointers is the solution.


2024-05-17  Bruno Haible  <br...@clisp.org>

        unistd: Fix compilation error with MSVC in C++ mode.
        * lib/unistd.in.h (read, write): Use _GL_CXXALIAS_MDA_CAST instead of
        _GL_CXXALIAS_MDA.

diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index fa99d7472f..7dbed38969 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -1934,11 +1934,7 @@ _GL_CXXALIASWARN (read);
 #   undef read
 #   define read _read
 #  endif
-#  ifdef __MINGW32__
-_GL_CXXALIAS_MDA (read, int, (int fd, void *buf, unsigned int count));
-#  else
-_GL_CXXALIAS_MDA (read, ssize_t, (int fd, void *buf, unsigned int count));
-#  endif
+_GL_CXXALIAS_MDA_CAST (read, ssize_t, (int fd, void *buf, unsigned int count));
 # else
 _GL_CXXALIAS_SYS (read, ssize_t, (int fd, void *buf, size_t count));
 # endif
@@ -2402,11 +2398,7 @@ _GL_CXXALIASWARN (write);
 #   undef write
 #   define write _write
 #  endif
-#  ifdef __MINGW32__
-_GL_CXXALIAS_MDA (write, int, (int fd, const void *buf, unsigned int count));
-#  else
-_GL_CXXALIAS_MDA (write, ssize_t, (int fd, const void *buf, unsigned int 
count));
-#  endif
+_GL_CXXALIAS_MDA_CAST (write, ssize_t, (int fd, const void *buf, unsigned int 
count));
 # else
 _GL_CXXALIAS_SYS (write, ssize_t, (int fd, const void *buf, size_t count));
 # endif




Reply via email to