5

I am trying compile code that uses snprintf in version 4.8.2 of g++ with -std=c++11 without success. Why doesn't g++ recognize snprintf? Can I overcome this while still using snprintf and c++11?

I got the following:

make all  Building file: ../src/cppHWtest.cpp Invoking: Cygwin C++
Compiler g++ -std=c++11 -D"hash_map=unordered_map" -O0 -g3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"src/cppHWtest.d" -MT"src/cppHWtest.d" -o "src/cppHWtest.o" "../src/cppHWtest.cpp" cygwin warning:   MS-DOS style path detected: C:\Users\poudyal\workspace\cppHWtest\Debug
Preferred POSIX equivalent is:
/cygdrive/c/Users/poudyal/workspace/cppHWtest/Debug   CYGWIN
environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames ../src/cppHWtest.cpp: In function 'int main()':
../src/cppHWtest.cpp:20:35: error: 'snprintf' was not declared in this
scope   snprintf(buff, 10, "%s", "Hell O");
                                   ^ make: *** [src/cppHWtest.o] Error 1 src/subdir.mk:18: recipe for target 'src/cppHWtest.o' failed

**** Build Finished ****
5
  • 3
    This cygwin mailing list archive thread is about a supposedly-fixed bug that caused snprintf not to be declared for -std=c++0x. Suggested workaround was to use -std=gnu++0x instead. Just for the heck of it, try -std=gnu++11 and see what happens. Commented Nov 22, 2013 at 17:43
  • That worked! I also had #include <cstdio> Commented Nov 22, 2013 at 21:22
  • I find now that vector<double> has the same problem. vector<> is fine with -std=c++11 Commented Nov 22, 2013 at 21:39
  • #include <vector> fixed that. This was a bad oversight, sorry. Commented Nov 22, 2013 at 22:11
  • 1
    On Cygwin, I can verify that -std=c++11 does not work either. But -std=gnu++11 does work. On real Linux (e.g. CentOS), -std=c++11 works fine. Commented Jun 11, 2014 at 17:10

1 Answer 1

6

Probably less intrusive solution than switching the mode to -std=gnu++11 is -U__STRICT_ANSI__. This will enable #if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L) at stdio.h:234 (GCC 4.8.3).

Sign up to request clarification or add additional context in comments.

1 Comment

when building both on ubuntu/linux and on windows with cygwin, then this solution will be the best solution - it worked for me - thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.