Discussion:
snprintf functions errors on MSVC
(too old to reply)
Bruno Haible
2016-12-17 22:11:06 UTC
Permalink
Raw Message
Compiling a gnulib testdir with MSVC 14, I get these errors:

C:\cygwin64\home\bruno\testdir-posix\gllib\snprintf.c(38): error C2084: function 'int snprintf(char *const ,const std::size_t,const char *const ,...)' already has a body
C:\cygwin64\home\bruno\testdir-posix\gllib\vsnprintf.c(40): error C2084: function 'int vsnprintf(char *const ,const std::size_t,const char *const ,va_list)' already has a body

The reason is that these functions are defined as inline functions in <stdio.h>
(unless _NO_CRT_STDIO_INLINE is defined):
vfprintf vprintf fprintf printf vsnprintf sprintf snprintf
vfscanf vscanf fscanf vsscanf

configure finds:
checking whether vsnprintf is declared... yes
checking whether vsnprintf is declared without a macro... yes
checking for vsnprintf... no
and as a result, sets REPLACE_VSNPRINTF=0. This fixes it:


2016-12-17 Bruno Haible <***@clisp.org>

Avoid redefinition errors on MSVC.
* m4/snprintf.m4 (gl_REPLACE_SNPRINTF): Set REPLACE_SNPRINTF to 1 if
the function may be defined as an inline function.
* m4/vsnprintf.m4 (gl_REPLACE_VSNPRINTF): Set REPLACE_VSNPRINTF to 1 if
the function may be defined as an inline function.

diff --git a/m4/snprintf.m4 b/m4/snprintf.m4
index f876b55..30aa25c 100644
--- a/m4/snprintf.m4
+++ b/m4/snprintf.m4
@@ -1,4 +1,4 @@
-# snprintf.m4 serial 6
+# snprintf.m4 serial 7
dnl Copyright (C) 2002-2004, 2007-2016 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -46,6 +46,14 @@ AC_DEFUN([gl_REPLACE_SNPRINTF],
AC_LIBOBJ([snprintf])
if test $ac_cv_func_snprintf = yes; then
REPLACE_SNPRINTF=1
+ else
+ AC_CHECK_DECLS_ONCE([snprintf])
+ if test $ac_cv_have_decl_snprintf = yes; then
+ dnl If the function is declared but does not appear to exist, it may be
+ dnl defined as an inline function. In order to avoid a conflict, we have
+ dnl to define rpl_snprintf, not snprintf.
+ REPLACE_SNPRINTF=1
+ fi
fi
gl_PREREQ_SNPRINTF
])
diff --git a/m4/vsnprintf.m4 b/m4/vsnprintf.m4
index e056f05..02da308 100644
--- a/m4/vsnprintf.m4
+++ b/m4/vsnprintf.m4
@@ -1,4 +1,4 @@
-# vsnprintf.m4 serial 6
+# vsnprintf.m4 serial 7
dnl Copyright (C) 2002-2004, 2007-2016 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -46,6 +46,14 @@ AC_DEFUN([gl_REPLACE_VSNPRINTF],
AC_LIBOBJ([vsnprintf])
if test $ac_cv_func_vsnprintf = yes; then
REPLACE_VSNPRINTF=1
+ else
+ AC_CHECK_DECLS_ONCE([vsnprintf])
+ if test $ac_cv_have_decl_vsnprintf = yes; then
+ dnl If the function is declared but does not appear to exist, it may be
+ dnl defined as an inline function. In order to avoid a conflict, we have
+ dnl to define rpl_vsnprintf, not vsnprintf.
+ REPLACE_VSNPRINTF=1
+ fi
fi
gl_PREREQ_VSNPRINTF
])

Loading...