Hi Paul,

Paul Eggert wrote:
> Would the attached work for MSVC? I am still trying to avoid fiddling with
> compilation flags

This is an excellent idea! We have similar (tested) code already in
gnulib/lib/at-init.h.

Your patch has no immediate success: it still leads to
  error C2168: 'memcpy': too few actual parameters for intrinsic function

But with a small tweak, it does the job!

I'm attaching:
  - 0001 your patch,
  - 0002 my tweaks,
  - 0001+0002 both patches merged together.

Tested with these 3 symbols:
  fpurge                         no
  _set_invalid_parameter_handler yes
  memcpy                         yes

on three platforms:
  - MSVC 32-bit
  - MSVC 64-bit
  - clang masquerading as MSVC 64-bit

Once you commit this in Autoconf, I'll push a corresponding patch to Gnulib
(that reverts my previous commit that augments AC_PROG_CC).

Bruno

From 29a82dd02ecc951d6df50669bda0ca6fd7cf3115 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sun, 18 Oct 2026 18:24:02 -0700
Subject: [PATCH 1/2] Fix AC_CHECK_FUNC for MSVC with a linker pragma
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/autoconf/c.m4 (AC_LANG_FUNC_LINK_TRY(C)): For MSVC, use
‘#pragma comment (linker, "/include:_foo")’, not ‘char
foo(void);’, to check whether foo exists
---
 lib/autoconf/c.m4 | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4
index 8d9cfe64..15f09720 100644
--- a/lib/autoconf/c.m4
+++ b/lib/autoconf/c.m4
@@ -169,13 +169,26 @@ m4_define([AC_LANG_FUNC_LINK_TRY(C)],
 # pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
 #endif
 
+#ifdef _MSC_VER
+
+/* Tell MSVC to link the function without checking its type.  */
+# ifdef _M_IX86
+#  pragma comment (linker, "/include:_$1")
+# else
+#  pragma comment (linker, "/include:$1")
+# endif
+
+#else
+
 /* Override any GCC internal prototype to avoid an error.
    Use char because int might match the return type of a GCC
    builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C"
-#endif
+# endif
 char $1 (void);
+#endif
+
 /* The GNU C library defines this for functions which it implements
     to always fail with ENOSYS.  Some functions are actually named
     something starting with __ and the normal name is an alias.  */
-- 
2.53.0

>From 2e7b46fc3b0671a359029765de66116eb98c7a8d Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Mon, 19 Oct 2026 16:34:19 +0200
Subject: [PATCH 2/2] Make the previous commit actually work, and document the
 change.

---
 NEWS              |  4 +++-
 lib/autoconf/c.m4 | 16 ++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index 023880b3..9ffba6f7 100644
--- a/NEWS
+++ b/NEWS
@@ -27,10 +27,12 @@ GNU Autoconf NEWS - User visible changes.
 
 *** AC_C_RESTRICT no longer triggers -Wkeyword-macro warnings from GCC 16.
 
-*** AC_CHECK_FUNC and related macros now work better with Clang.
+*** AC_CHECK_FUNC and related macros now work better with Clang and MSVC.
 
   Arguments like CFLAGS="-Wno-incompatible-library-redeclaration" are
   no longer needed when configuring with Clang in pedantic mode.
+  Similarly, arguments like CFLAGS="-Oi-" are no longer needed when
+  configuring with MSVC with enabled optimizations.
 
 * Noteworthy changes in release 2.73 (2026-03-20) [release]
 
diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4
index 15f09720..f3a1caa5 100644
--- a/lib/autoconf/c.m4
+++ b/lib/autoconf/c.m4
@@ -172,9 +172,9 @@ m4_define([AC_LANG_FUNC_LINK_TRY(C)],
 #ifdef _MSC_VER
 
 /* Tell MSVC to link the function without checking its type.  */
-# ifdef _M_IX86
+# ifdef _M_IX86 /* x86 */
 #  pragma comment (linker, "/include:_$1")
-# else
+# else /* x86_64, arm, arm64 */
 #  pragma comment (linker, "/include:$1")
 # endif
 
@@ -187,15 +187,19 @@ m4_define([AC_LANG_FUNC_LINK_TRY(C)],
 extern "C"
 # endif
 char $1 (void);
-#endif
-
 /* The GNU C library defines this for functions which it implements
     to always fail with ENOSYS.  Some functions are actually named
     something starting with __ and the normal name is an alias.  */
-#if defined __stub_$1 || defined __stub___$1
+# if defined __stub_$1 || defined __stub___$1
 choke me
+# endif
+
+#endif
+], [
+#ifndef _MSC_VER
+return $1 ();
 #endif
-], [return $1 ();])])
+])])
 
 
 # AC_LANG_BOOL_COMPILE_TRY(C)(PROLOGUE, EXPRESSION)
-- 
2.53.0

From 68132168584176836538afef962730415c93cacf Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sun, 18 Oct 2026 18:24:02 -0700
Subject: [PATCH] Fix AC_CHECK_FUNC for MSVC with a linker pragma
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/autoconf/c.m4 (AC_LANG_FUNC_LINK_TRY(C)): For MSVC, use
‘#pragma comment (linker, "/include:_foo")’, not ‘char
foo(void);’, to check whether foo exists
---
 NEWS              |  4 +++-
 lib/autoconf/c.m4 | 25 +++++++++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index 023880b3..9ffba6f7 100644
--- a/NEWS
+++ b/NEWS
@@ -27,10 +27,12 @@ GNU Autoconf NEWS - User visible changes.
 
 *** AC_C_RESTRICT no longer triggers -Wkeyword-macro warnings from GCC 16.
 
-*** AC_CHECK_FUNC and related macros now work better with Clang.
+*** AC_CHECK_FUNC and related macros now work better with Clang and MSVC.
 
   Arguments like CFLAGS="-Wno-incompatible-library-redeclaration" are
   no longer needed when configuring with Clang in pedantic mode.
+  Similarly, arguments like CFLAGS="-Oi-" are no longer needed when
+  configuring with MSVC with enabled optimizations.
 
 * Noteworthy changes in release 2.73 (2026-03-20) [release]
 
diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4
index 8d9cfe64..f3a1caa5 100644
--- a/lib/autoconf/c.m4
+++ b/lib/autoconf/c.m4
@@ -169,20 +169,37 @@ m4_define([AC_LANG_FUNC_LINK_TRY(C)],
 # pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
 #endif
 
+#ifdef _MSC_VER
+
+/* Tell MSVC to link the function without checking its type.  */
+# ifdef _M_IX86 /* x86 */
+#  pragma comment (linker, "/include:_$1")
+# else /* x86_64, arm, arm64 */
+#  pragma comment (linker, "/include:$1")
+# endif
+
+#else
+
 /* Override any GCC internal prototype to avoid an error.
    Use char because int might match the return type of a GCC
    builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
+# ifdef __cplusplus
 extern "C"
-#endif
+# endif
 char $1 (void);
 /* The GNU C library defines this for functions which it implements
     to always fail with ENOSYS.  Some functions are actually named
     something starting with __ and the normal name is an alias.  */
-#if defined __stub_$1 || defined __stub___$1
+# if defined __stub_$1 || defined __stub___$1
 choke me
+# endif
+
+#endif
+], [
+#ifndef _MSC_VER
+return $1 ();
 #endif
-], [return $1 ();])])
+])])
 
 
 # AC_LANG_BOOL_COMPILE_TRY(C)(PROLOGUE, EXPRESSION)
-- 
2.53.0

Reply via email to