g++: -ansi flag makes snprintf() unavailable?

Steven Monai steve+cygwin@monai.ca
Wed Jan 20 04:06:00 GMT 2010


Hi folks,

Here is a very simple C++ test program that uses snprintf() and then
prints the result.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
  char buf[10];
  char c = 10;
  snprintf( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) );
  cout << buf << '\n';  // printf( "%s\n", buf );
  return 0;
}

I compile and run this program as follows:

$ g++ -ansi -o mytest mytest.cc && ./mytest

In Debian Lenny, using g++ version 4.3.2, the program compiles and runs
successfully, producing the output I expect.

However in Cygwin 1.7, using g++ version 4.3.4, I get the following
compilation error:

mytest.cc: In function 'int main()':
mytest.cc:9: error: 'snprintf' was not declared in this scope

When I remove the -ansi flag from the compilation line, the program
builds and runs successfully in both Cygwin and Debian.

Furthermore, when I replace the cout line with a printf(), the compiler
never complains about printf(), regardless of the presence or absence of
the -ansi flag. I find that puzzling, since I'd expect printf() and
snprintf(), being related functions, to be affected in the same way by
the compilation flags.

Can anyone shed some light on this behaviour? Did the interpretation of
the -ansi flag change between gcc version 4.3.2 (Debian's) and 4.3.4
(Cygwin's)? Or is someone's compiler misbehaving?

Best regards,
-SM
--

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list