Building a gnulib testdir with non-optimizing MSVC works fine. However,
when I add an optimizing option ("-O2") to the CFLAGS, I see compilation
errors:

log2.c(32): error C2169: 'log2': intrinsic function, cannot be defined
log2f.c(42): error C2169: 'log2f': intrinsic function, cannot be defined
memcpy.c(28): error C2169: 'memcpy': intrinsic function, cannot be defined
memmove.c(13): error C2169: 'memmove': intrinsic function, cannot be defined
memset.c(22): error C2169: 'memset': intrinsic function, cannot be defined
powf.c(23): error C2169: 'powf': intrinsic function, cannot be defined
wcscat-impl.h(19): error C2169: 'wcscat': intrinsic function, cannot be defined
wcscmp-impl.h(19): error C2169: 'wcscmp': intrinsic function, cannot be defined
wcscpy-impl.h(19): error C2169: 'wcscpy': intrinsic function, cannot be defined
wcslen-impl.h(19): error C2169: 'wcslen': intrinsic function, cannot be defined
wcsncmp-impl.h(19): error C2169: 'wcsncmp': intrinsic function, cannot be 
defined
wcsncpy-impl.h(19): error C2169: 'wcsncpy': intrinsic function, cannot be 
defined

Gnulib attempts to define these functions, including memcpy, memmove, and 
memset,
because the configure test produced the wrong result:

  checking for memcpy... no
  checking for memmove... no
  checking for memset... no

This, in turn, comes from the fact that optimizing MSVC "knows" about these
functions in particular. This is documented here:
https://learn.microsoft.com/en-us/cpp/intrinsics/intrinsics-available-on-all-architectures

This was already reported in 2007:
<https://lists.gnu.org/archive/html/bug-autoconf/2007-08/msg00008.html>
but no fix was added to Autoconf at that time.

Two possible fixes come to mind:
  (a) AC_CHECK_FUNC could special-case each of these functions, using a
      correct prototype instead of a dummy (no arguments, return type 'char').
  (b) Add a compiler option to AC_CHECK_FUNC so that the intrinsics get turned
      off. This option is '-Oi-'.

Since (a) would be a lot of code, I'm going for (b). I'm applying this fix to
Gnulib. It would be nice if Autoconf could incorporate it.


2026-10-17  Bruno Haible  <[email protected]>

        Fix AC_CHECK_FUNC results with optimizing MSVC.
        Reported by Jerker Bäck in
        <https://lists.gnu.org/archive/html/bug-autoconf/2007-08/msg00008.html>.
        * m4/00gnulib.m4 (gl_COMPILER_PREPARE_CHECK_FUNC): New macro.
        (_AC_CHECK_FUNC_BODY): New override.
        * m4/zzgnulib.m4 (AC_PROG_CC): Invoke gl_COMPILER_PREPARE_CHECK_FUNC.

diff --git a/m4/00gnulib.m4 b/m4/00gnulib.m4
index a4b3240300..c922273a70 100644
--- a/m4/00gnulib.m4
+++ b/m4/00gnulib.m4
@@ -1,5 +1,5 @@
 # 00gnulib.m4
-# serial 9
+# serial 10
 dnl Copyright (C) 2009-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -80,6 +80,68 @@ m4_define([_AC_CHECK_DECL_BODY]
 m4_defn([_AC_CHECK_DECL_BODY])[  ac_compile="$ac_saved_ac_compile"
 ])
 
+# The following definitions arrange to use a compiler option -Oi- in
+# AC_CHECK_FUNC, when the compiler is MSVC with optimization enabled.
+# Without it, this compiler implicitly declares "known" library functions,
+# which would cause the AC_CHECK_FUNC test to fail.
+# The list of such library functions is documented at
+# 
<https://learn.microsoft.com/en-us/cpp/intrinsics/intrinsics-available-on-all-architectures>.
+# It's not possible to AC_REQUIRE the extra tests from AC_CHECK_FUNC,
+# because AC_CHECK_FUNC, like other Autoconf built-ins, is not supposed
+# to AC_REQUIRE anything: some configure.ac files have their first
+# AC_CHECK_FUNC executed conditionally.  Therefore append the extra tests
+# to AC_PROG_CC.
+AC_DEFUN([gl_COMPILER_PREPARE_CHECK_FUNC],
+[
+dnl AC_REQUIRE([AC_PROG_CC])
+  AC_CACHE_CHECK([for compiler option needed when checking for functions],
+    [gl_cv_compiler_check_func_option],
+    [dnl Use _AC_COMPILE_IFELSE instead of AC_COMPILE_IFELSE, to avoid a
+     dnl warning "AC_COMPILE_IFELSE was called before 
AC_USE_SYSTEM_EXTENSIONS".
+     _AC_COMPILE_IFELSE(
+       [AC_LANG_PROGRAM([[
+          #ifdef __cplusplus
+          extern "C"
+          #endif
+          char memcpy (void);
+          ]],[[
+          return memcpy ();
+          ]])],
+       [gl_cv_compiler_check_func_option=none],
+       [dnl Test whether the compiler supports the option '-Oi-'.
+        saved_ac_compile="$ac_compile"
+        ac_compile="$ac_compile -Oi-"
+        _AC_COMPILE_IFELSE(
+          [AC_LANG_PROGRAM([[
+             #ifdef __cplusplus
+             extern "C"
+             #endif
+             char memcpy (void);
+             ]],[[
+             return memcpy ();
+             ]])],
+          [gl_cv_compiler_check_func_option='-Oi-'],
+          [gl_cv_compiler_check_func_option=none])
+        ac_compile="$saved_ac_compile"
+       ])
+    ])
+  if test "x$gl_cv_compiler_check_func_option" != xnone; then
+    ac_link_for_check_func="$ac_link $gl_cv_compiler_check_func_option"
+  else
+    ac_link_for_check_func="$ac_link"
+  fi
+])
+dnl Redefine _AC_CHECK_FUNC_BODY so that it references ac_link_for_check_func
+dnl instead of ac_link.  If, for whatever reason, the override of AC_PROG_CC
+dnl in zzgnulib.m4 is inactive, use the original ac_link.
+m4_define([_AC_CHECK_FUNC_BODY],
+[  ac_saved_ac_link="$ac_link"
+  if test -n "$ac_link_for_check_func"; then
+    ac_link="$ac_link_for_check_func"
+  fi]
+m4_defn([_AC_CHECK_FUNC_BODY])[  ac_link="$ac_saved_ac_link"
+])
+
 # gl_00GNULIB
 # -----------
 # Witness macro that this file has been included.  Needed to force
diff --git a/m4/zzgnulib.m4 b/m4/zzgnulib.m4
index fed091cabd..edce69192c 100644
--- a/m4/zzgnulib.m4
+++ b/m4/zzgnulib.m4
@@ -1,5 +1,5 @@
 # zzgnulib.m4
-# serial 1
+# serial 2
 dnl Copyright (C) 2020-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,12 +10,13 @@
 dnl package- or gnulib-provided .m4 files - at least for those packages
 dnl that redefine AC_PROG_CC.
 
-dnl Redefine AC_PROG_CC so that it ends with invocations of gl_COMPILER_CLANG
-dnl and gl_COMPILER_PREPARE_CHECK_DECL.
+dnl Redefine AC_PROG_CC so that it ends with invocations of gl_COMPILER_CLANG,
+dnl gl_COMPILER_PREPARE_CHECK_DECL, and gl_COMPILER_PREPARE_CHECK_FUNC.
 m4_define([AC_PROG_CC],
   m4_defn([AC_PROG_CC])[
 gl_COMPILER_CLANG
 gl_COMPILER_PREPARE_CHECK_DECL
+gl_COMPILER_PREPARE_CHECK_FUNC
 ])
 
 # gl_ZZGNULIB




Reply via email to