Bug 60498 - error: 'snprintf' was not declared in this scope
Summary: error: 'snprintf' was not declared in this scope
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-11 06:59 UTC by zosrothko
Modified: 2014-03-16 19:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zosrothko 2014-03-11 06:59:02 UTC
Hi

On a Cygwin/win7/x86_64 platform with , one gets this error message when compiling the following source with -std=c++11 while it compiles fine with -std=gnu++11

$ cat foo.c
#include <stdio.h>

int main(void) {
        char buf[10];
        snprintf(buf, 10, "%i", 0);
        printf("buf=%s\n", buf);
        return 0;
}

$ gcc -xc -std=c90 -c foo.c
$ gcc -xc -std=c89 -c foo.c
$
$ g++ -xc++ -std=c++11  -c foo.c
foo.c: In function 'int main()':
foo.c:5:27: error: 'snprintf' was not declared in this scope
  snprintf(buf, 10, "%i", 0);

$
$ g++ -xc++ -std=gnu++11  -c foo.c





Context:
CYGWIN_NT-6.1 idefix 1.7.28(0.271/5/3) 2014-02-09 21:06 x86_64 Cygwin

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.2-3/src/gcc-4.8.2/configure --srcdir=/cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.2-3/src/gcc-4.8.2 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --disable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers --with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --libexecdir=/usr/lib
Thread model: posix
gcc version 4.8.2 (GCC)
Comment 1 zosrothko 2014-03-11 17:03:08 UTC
This issue applies also to other C functions like strdup, realpath, strerror_r, strsep, etc..
Comment 2 Jakub Jelinek 2014-03-11 17:06:09 UTC
stdio.h is provided by cygwin, not GCC, so IMHO you should report it there instead.
Comment 3 zosrothko 2014-03-11 17:22:40 UTC
(In reply to Jakub Jelinek from comment #2)
> stdio.h is provided by cygwin, not GCC, so IMHO you should report it there
> instead.

on the cygwin mailling list, cygwin states that it is a bug in gcc... see this thread: http://cygwin.com/ml/cygwin/2012-04/msg00140.html

Thus I am a little bit puzzled...
Comment 4 zosrothko 2014-03-11 17:25:39 UTC
(In reply to Jakub Jelinek from comment #2)
> stdio.h is provided by cygwin, not GCC, so IMHO you should report it there
> instead.

and by the way, it compiles fine when using -std=gnu++11
Comment 5 zosrothko 2014-03-11 17:27:13 UTC
This is a snipet from usr/include/stdio.h

#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L)
#ifndef _REENT_ONLY
int     _EXFUN(asiprintf, (char **, const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 2, 3))));
char *  _EXFUN(asniprintf, (char *, size_t *, const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 3, 4))));
char *  _EXFUN(asnprintf, (char *__restrict, size_t *__restrict, const char *__restrict, ...)
               _ATTRIBUTE ((__format__ (__printf__, 3, 4))));
int     _EXFUN(asprintf, (char **__restrict, const char *__restrict, ...)
               _ATTRIBUTE ((__format__ (__printf__, 2, 3))));
#ifndef diprintf
int     _EXFUN(diprintf, (int, const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 2, 3))));
#endif
int     _EXFUN(fcloseall, (_VOID));
int     _EXFUN(fiprintf, (FILE *, const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 2, 3))));
int     _EXFUN(fiscanf, (FILE *, const char *, ...)
               _ATTRIBUTE ((__format__ (__scanf__, 2, 3))));
int     _EXFUN(iprintf, (const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 1, 2))));
int     _EXFUN(iscanf, (const char *, ...)
               _ATTRIBUTE ((__format__ (__scanf__, 1, 2))));
int     _EXFUN(siprintf, (char *, const char *, ...)
               _ATTRIBUTE ((__format__ (__printf__, 2, 3))));
int     _EXFUN(siscanf, (const char *, const char *, ...)
               _ATTRIBUTE ((__format__ (__scanf__, 2, 3))));
int     _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
               _ATTRIBUTE ((__format__ (__printf__, 3, 4))));
Comment 6 Jakub Jelinek 2014-03-11 17:32:39 UTC
As C++ isn't C, obviously it g++ -std=c++11 doesn't define __STDC_VERSION__ macro, but just defines
__STRICT_ANSI__ 1
__cplusplus 201103L
Should be more than enough for cygwin headers to do the right thing.
Comment 7 zosrothko 2014-03-11 17:50:32 UTC
(In reply to Jakub Jelinek from comment #6)
> As C++ isn't C, obviously it g++ -std=c++11 doesn't define __STDC_VERSION__
> macro, but just defines
> __STRICT_ANSI__ 1
> __cplusplus 201103L
> Should be more than enough for cygwin headers to do the right thing.

Ok but gnu++1 doesn't define __STDC_VERSION__ neither but it compiles fine...so??

$ g++ -xc++ -std=gnu++11 -dM -E - < /dev/null | sort | grep STDC
#define __GNUC_STDC_INLINE__ 1
#define __STDC_HOSTED__ 1
#define __STDC__ 1

$ g++ -xc++ -std=c++11 -dM -E - < /dev/null | sort | grep STDC
#define __GNUC_STDC_INLINE__ 1
#define __STDC_HOSTED__ 1
#define __STDC__ 1
Comment 8 Jakub Jelinek 2014-03-11 17:52:14 UTC
Sure, but with -std=gnu++11 __STRICT_ANSI__ isn't defined (as intended).
Comment 9 Jonathan Wakely 2014-03-11 18:47:43 UTC
Definitely a Cygwin bug, their header should test:

#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L) || (__cplusplus >= 201103L)

That way the functions will be defined for any -std=gnuNN mode, for C99, and for C++11, which would be correct.
Comment 10 Jakub Jelinek 2014-03-11 18:55:24 UTC
Note, you've listed also plenty of non-standard functions like asiprintf, I guess those certainly shouldn't be available for __STRICT_ANSI__, even for -std=c99, -std=c11 or -std=c++11.
Comment 11 Csaba Ráduly 2014-03-16 19:03:09 UTC
That is not going to work.

$ gcc -dumpversion
4.8.2

$ gcc -E -dM -x c /dev/null | egrep -i 'ansi|std'
#define _stdcall __attribute__((__stdcall__))
#define __STDC_HOSTED__ 1
#define __stdcall __attribute__((__stdcall__))
#define __STDC__ 1

$ g++ -dumpversion
4.8.2

$ g++ -E -dM -x c++ /dev/null | egrep -i 'ansi|std'
#define _stdcall __attribute__((__stdcall__))
#define __STDC_HOSTED__ 1
#define __stdcall __attribute__((__stdcall__))
#define __STDC__ 1
Comment 12 Csaba Ráduly 2014-03-16 19:07:04 UTC
Doh. Disregard me.

$ g++ -std=c++11 -E -dM -x c++ /dev/null | egrep -i 'ansi|std|plus'
#define __STDC_HOSTED__ 1
#define __STRICT_ANSI__ 1
#define __cplusplus 201103L
#define __stdcall __attribute__((__stdcall__))
#define __GNUC_STDC_INLINE__ 1
#define __STDC__ 1