When I build testdirs of modules fcntl-h or unistd-h, with C++ tests included,
on MSVC, I see these errors:

.\fcntl.h(676): error C2039: '_creat': is not a member of '`global namespace''
.\fcntl.h(676): error C2065: '_creat': undeclared identifier
.\fcntl.h(749): error C2039: '_open': is not a member of '`global namespace''
.\fcntl.h(749): error C2065: '_open': undeclared identifier
.\fcntl.h(749): error C2440: 'return': cannot convert from 'int (__cdecl 
*)(const char *const ,const int,const int)' to 'gnulib::_gl_open_wrapper::type'

This patch fixes them.


2025-05-25  Bruno Haible  <br...@clisp.org>

        fcntl-h, unistd-h: Fix errors in C++ mode with MSVC.
        * lib/fcntl.in.h (open): Use _GL_CXXALIAS_MDA_CAST instead of
        _GL_CXXALIAS_MDA.
        * lib/unistd.in.h: Include <io.h>, <direct.h> before <fcntl.h>.

diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h
index d7f551b30f..0e4c04ea9b 100644
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -209,7 +209,9 @@ _GL_WARN_ON_USE (open, "open is not always POSIX compliant 
- "
 #   undef open
 #   define open _open
 #  endif
-_GL_CXXALIAS_MDA (open, int, (const char *filename, int flags, ...));
+/* Need to cast, because in MSVC the parameter list of _open as a C++ function
+   is (const char *, int, int = 0).  */
+_GL_CXXALIAS_MDA_CAST (open, int, (const char *filename, int flags, ...));
 # else
 _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
 # endif
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index c135a770dc..9f057d30cd 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -95,6 +95,15 @@
 # include <stdio.h>
 #endif
 
+/* Native Windows platforms declare _chdir, _getcwd, _rmdir in
+   <io.h> and/or <direct.h>, not in <unistd.h>.
+   They also declare _access(), _chmod(), _close(), _dup(), _dup2(), _isatty(),
+   _lseek(), _read(), _unlink(), _write() in <io.h>.  */
+#if defined _WIN32 && !defined __CYGWIN__
+# include <io.h>
+# include <direct.h>
+#endif
+
 /* FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Solaris 11.4, and glibc 2.41
    do not define O_CLOEXEC in <unistd.h>.  */
 /* Cygwin 1.7.1 and Android 4.3 declare unlinkat in <fcntl.h>, not in
@@ -120,15 +129,6 @@
 # undef __need_system_stdlib_h
 #endif
 
-/* Native Windows platforms declare _chdir, _getcwd, _rmdir in
-   <io.h> and/or <direct.h>, not in <unistd.h>.
-   They also declare _access(), _chmod(), _close(), _dup(), _dup2(), _isatty(),
-   _lseek(), _read(), _unlink(), _write() in <io.h>.  */
-#if defined _WIN32 && !defined __CYGWIN__
-# include <io.h>
-# include <direct.h>
-#endif
-
 /* Native Windows platforms declare _execl*, _execv* in <process.h>.  */
 #if defined _WIN32 && !defined __CYGWIN__
 # include <process.h>




Reply via email to