C++ VS 2015 编译cocos2d-x 3.3 错误“致命错误C1189:#error:snprintf 的宏定义与标准库函数声明冲突”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27754492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VS 2015 compiling cocos2d-x 3.3 error "fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration"
提问by Jared
When I compile cocos2d-x (version 3.3) using visual studio 2015, an error occured, saying:
当我使用visual studio 2015编译cocos2d-x(3.3版)时,出现错误,说:
fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration (编译源文件 ..\base\s3tc.cpp)
致命错误 C1189:#error:snprintf 的宏定义与标准库函数声明冲突(编译源文件 ..\base\s3tc.cpp)
The source code is:
源代码是:
#ifdef snprintf
#error Macro definition of snprintf conflicts with Standard Library
function declaration
#endif
Anybody can tell me what's wrong?
任何人都可以告诉我出了什么问题?
回答by user1
Until now, Many libraries & programs used snprintf()
function by defining it as _snprintf()
, since _snprintf()
was supported.
到目前为止,许多库和程序snprintf()
通过将函数定义为 来使用函数_snprintf()
,因为它_snprintf()
被支持。
#define snprintf _snprintf
Finally, Visual Studio 14 defines snprintf()
!
最后,Visual Studio 14 定义了snprintf()
!
Since, snprintf()
is now officially supported. We should never #define it.
因为,snprintf()
现在得到官方支持。我们永远不应该#define它。
Doing it will overshadow new snprintf()
function defined in stdio.h.
这样做会使snprintf()
stdio.h 中定义的新函数黯然失色。
To restrict that, this is added in stdio.h
为了限制这一点,这是在 stdio.h 中添加的
#ifdef snprintf
#error: Macro definition of snprintf conflicts with Standard Library function declaration”
#endif
Hence, your code doesn't compile.
因此,您的代码无法编译。
It is true that on all previous versions of Visual Studio, you must use _snprintf()
function. But VS 2014 onwards you should not #define it with _snprintf()
.
确实,在所有以前版本的 Visual Studio 上,您必须使用_snprintf()
函数。但是从 VS 2014 开始,你不应该用_snprintf()
.
Somewhere in your code or most likely in cocos
headers, this is done and hence the error.
在您的代码中或最有可能在cocos
标题中的某处,已完成此操作,因此会出现错误。
Check that and remove that #define.
检查并删除该#define。
snprintf()
is part of C99 specifications.
snprintf()
是 C99 规范的一部分。
To enable C99 support
启用 C99 支持
add this in your program
将此添加到您的程序中
#if _MSC_VER>=1900
# define STDC99
#endif
In case you don't know what _MSC_VER macro values are
如果您不知道 _MSC_VER 宏值是什么
...
MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003)
MSVC++ 7.0 _MSC_VER == 1300
MSVC++ 6.0 _MSC_VER == 1200
MSVC++ 5.0 _MSC_VER == 1100
MSVC++ 4.0 _MSC_VER == 1000
MSVC++ 2.0 _MSC_VER == 900
MSVC++ 1.0 _MSC_VER == 800
C/C++ 7.0 _MSC_VER == 700
C 6.0 _MSC_VER == 600
回答by Jie
Just find the definition of snprintf
in your code and undefine it while on VS2015
.
Something like:
只需snprintf
在代码中找到 的定义并在 on 时取消定义即可VS2015
。
就像是:
#if _MSC_VER < 1900 //vs2015 already have this function
#define snprintf _snprintf_s
#endif
回答by user1185287
user1 is right
用户 1 是对的
But even if you fix the problem this way, you'll probably face linker problems with prebuilt libraries.
但即使您以这种方式解决问题,您也可能会面临预建库的链接器问题。
The way to avoid this is to change platform toolset on all projects to Visual Studio 2013 (v120)
避免这种情况的方法是将所有项目上的平台工具集更改为Visual Studio 2013 (v120)
And in linker/input propry page add libcmt.lib to Ignore Specific Default libraries: libcmt.lib;libcmtd.lib;...
并在链接器/输入属性页面中将 libcmt.lib 添加到忽略特定默认库:libcmt.lib;libcmtd.lib;...