mkdtemp: Provide declaration in <stdlib.h>

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <3051244.e9J7NaK4W3@cagnes>
Building a testdir of the modules
  acl areadlinkat areadlinkat-with-size argmatch argp argp-version-etc array-map array-omap array-oset array-set backup-rename backupfile bitset bitsetv c-nstrftime c-xvasprintf canonicalize chdir-long chmodat chown chownat classpath clean-temp clean-temp-simple closein closeout copy-file csharpcomp csharpexec cygpath dfa dirname error exclude execute faccessat fchmodat fchownat fdopendir fdutimensat file-remote file-set filenamecat findprog fstatat fstrcmp fts futimens getaddrinfo getcwd getlogin getumask getusershell glob hamt hash-map hash-set hash-triple hashcode-file-inode human idcache issymlinkat javacomp javaexec javaversion jit/cache kwset lchmod lchown linebuffer linkat linkedhash-map linkedhash-set list-c++ map-c++ mem-hash-map mkancesdirs mkdir-p mkdirat mkfifoat modechange mountlist nonblocking-pipe-tests nonblocking-socket-tests nonblocking nstrftime obstack-printf-gnu obstack-printf-posix obstack-printf obstack-zprintf omap-c++ openat openat-die openat-safer openat openat2 opendirat oset-c++ pagealign_alloc parse-duration perror pipe-filter-gi pipe-filter-ii posix_spawn_file_actions_addchdir posix_spawn_file_actions_addfchdir propername quote quotearg quotearg-simple quotearg random readlinkat readtokens readutmp regex-quote relocatable-lib relocatable-prog renameat renameatu same save-cwd savedir savewd selinux-at set-c++ sh-quote sigpipe-die spawn-pipe stack stat-time statat strerror strerror_l string-desc-quotearg supersede symlinkat system-quote term-style-control trim unicodeio uniname/uniname uninorm/nfc uninorm/nfd uninorm/nfkc uninorm/nfkd unistdio/u16-vsnprintf unistdio/u16-vsprintf unistdio/u32-vsnprintf unistdio/u32-vsprintf unistdio/u8-vsnprintf unistdio/u8-vsprintf unistdio/ulc-vsnprintf unistdio/ulc-vsprintf unlinkat userspec utime utimecmp utimens utimensat vc-mtime verror wait-process windows-cygpath xalignalloc xalloc xalloc-die xbinary-io xconcat-filename xfreopen xgetcwd xgetcwd-lgpl xgetdomainname xgetgroups xgethostname xhash xlist xmalloca xmap xmemcoll xmemdup0 xomap xoset xprintf xprintf-gnu xprintf-posix xreadlink xreadlinkat xset xsetenv xstdopen xstrerror xstriconv xstriconveh xstring-buffer xstring-buffer-reversed xstring-desc xstrndup xstrtol-error xstrtol xsublist xvasprintf xvasprintf-gnu xvasprintf-posix xvasprintf yesno
with option --with-c++-tests on macOS 26, I see this compilation error:

depbase=`echo test-assert-h-c++.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
        c++ -DHAVE_CONFIG_H -DEXEEXT=\"\" -DEXEEXT=\"\" -I. -I../../gltests -I..  -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -I/Users/haible/include -Wall -Wno-error -Wno-error -g -O2 -MT test-assert-h-c++.o -MD -MP -MF $depbase.Tpo -c -o test-assert-h-c++.o ../../gltests/test-assert-h-c++.cc &&\
        mv -f $depbase.Tpo $depbase.Po
In file included from ../../gltests/test-assert-h-c++.cc:27:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:45:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:228:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:17:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale_dir/locale_base_api.h:115:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale_dir/support/apple.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale_dir/support/bsd_like.h:19:
../gllib/stdlib.h:1435:19: error: no member named 'mkdtemp' in the global namespace
 1435 | _GL_CXXALIAS_SYS (mkdtemp, char *, (char * /*template*/));
      |                   ^~~~~~~
../gllib/stdlib.h:1436:19: error: use of undeclared identifier 'mkdtemp'
 1436 | _GL_CXXALIASWARN (mkdtemp);
      |                   ^~~~~~~
In file included from ../../gltests/test-assert-h-c++.cc:27:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:45:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:228:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:28:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string:654:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string_view:951:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:2057:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:607:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__atomic/atomic_flag.h:19:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__thread/support.h:112:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__thread/support/pthread.h:198:10: warning: The symbol ::nanosleep refers to the system function. Use gnulib::nanosleep instead. [-Wuser-defined-warnings]
  198 |   while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
      |          ^
../gllib/time.h:788:1: note: from 'diagnose_if' attribute on 'nanosleep':
  788 | _GL_CXXALIASWARN (nanosleep);
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/locale.h:381:4: note: expanded from macro '_GL_CXXALIASWARN'
  381 |    _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/locale.h:383:4: note: expanded from macro '_GL_CXXALIASWARN_1'
  383 |    _GL_CXXALIASWARN_2 (func, namespace)
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/locale.h:388:5: note: expanded from macro '_GL_CXXALIASWARN_2'
  388 |     _GL_WARN_ON_USE (func, \
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
  389 |                      "The symbol ::" #func " refers to the system function. " \
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  390 |                      "Use " #namespace "::" #func " instead.")
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/locale.h:556:19: note: expanded from macro '_GL_WARN_ON_USE'
  556 |   __attribute__ ((__diagnose_if__ (1, message, "warning")))
      |                   ^                ~
../../gltests/test-assert-h-c++.cc:32:26: warning: 'static_assert' with no message is a C++17 extension [-Wc++17-extensions]
   32 | static_assert (2 + 2 == 4);
      |                          ^
      |                          , ""
2 warnings and 2 errors generated.
make[3]: *** [test-assert-h-c++.o] Error 1


This patch fixes it.


2026-06-26  Bruno Haible  <[email protected]>

	mkdtemp: Provide declaration in <stdlib.h>.
	* lib/stdlib.in.h: Include <unistd.h> when needed for mkdtemp.
	* doc/posix-functions/mkdtemp.texi: Document macOS bug.

diff --git a/doc/posix-functions/mkdtemp.texi b/doc/posix-functions/mkdtemp.texi
index 75780faaa2..aa50da2046 100644
--- a/doc/posix-functions/mkdtemp.texi
+++ b/doc/posix-functions/mkdtemp.texi
@@ -12,6 +12,10 @@
 @item
 This function is missing on some platforms:
 AIX 5.1, HP-UX 11, Solaris 10, mingw, MSVC 14.
+@item
+This function is declared in @code{<unistd.h>} instead of @code{<stdlib.h>}
+on some platforms:
+macOS 26.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index 3c2004611f..6f63c87969 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -103,7 +103,8 @@ struct random_data
 # endif
 #endif
 
-#if (@GNULIB_MKSTEMP@ || @GNULIB_MKSTEMPS@ || @GNULIB_MKOSTEMP@ || @GNULIB_MKOSTEMPS@ || @GNULIB_GETSUBOPT@ || defined GNULIB_POSIXCHECK) && ! defined __GLIBC__ && !(defined _WIN32 && ! defined __CYGWIN__)
+#if (@GNULIB_MKDTEMP@ || @GNULIB_MKSTEMP@ || @GNULIB_MKSTEMPS@ || @GNULIB_MKOSTEMP@ || @GNULIB_MKOSTEMPS@ || @GNULIB_GETSUBOPT@ || defined GNULIB_POSIXCHECK) && ! defined __GLIBC__ && !(defined _WIN32 && ! defined __CYGWIN__)
+/* On macOS 26, only <unistd.h> declares mkdtemp.  */
 /* On Mac OS X 10.3, only <unistd.h> declares mkstemp.  */
 /* On Mac OS X 10.5, only <unistd.h> declares mkstemps.  */
 /* On Mac OS X 10.13, only <unistd.h> declares mkostemp and mkostemps.  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.