2013-11-22 176 views
5

我試圖編譯使用g ++的版本4.8.2中的snprintf和-std = C++ 11的代碼,但沒有成功。 爲什麼g ++不識別snprintf?我仍然可以在使用snprintf和C++ 11的時候克服這個問題嗎?如何在g ++中使用snprintf()-std = C++ 11版本4.8.2

我有以下幾點:

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 **** 
+3

[此cygwin郵件列表歸檔線程](http://cygwin.com/ml/cygwin/2012-04/msg00140.html)是關於導致'snprintf'不被聲明爲' -std = C++ 0x'。建議的解決方法是使用'-std = gnu ++ 0x'代替。僅僅爲了這個,試試'-std = gnu ++ 11',看看會發生什麼。 – Casey

+0

工作正常!我也有#include vjktm

+0

我現在發現矢量有同樣的問題。 vector <>很好-std = C++ 11 – vjktm

回答

6

比模式切換到-std=gnu++11大概少管閒事的解決辦法是-U__STRICT_ANSI__。這將在stdio.h:234(GCC 4.8.3)中啓用#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L)

+2

這可以在Cygwin上使用'-std = C++ 11'。 –

+0

當在ubuntu/linux和windows上用cygwin構建時,那麼這個解決方案將是最好的解決方案 - 它爲我工作 - 謝謝 – serup

相關問題