积极答复者
怪事。。。 名称被标记为 #pragma deprecated

问题
-
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cstdio(53) : warning C4995: “sprintf”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cstdio(56) : warning C4995: “vsprintf”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cstring(22) : warning C4995: “strcat”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cstring(23) : warning C4995: “strcpy”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cwchar(36) : warning C4995: “swprintf”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cwchar(37) : warning C4995: “vswprintf”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cwchar(39) : warning C4995: “wcscat”: 名称被标记为 #pragma deprecated
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cwchar(41) : warning C4995: “wcscpy”: 名称被标记为 #pragma deprecated我另一个工程提示 : This function or variable may be unsafe 为什么现在这个工程提示我 名称被标记? 是不是工程选项的问题? 一个是MFC DLL。一个是WIN32 DLL
平常都是提示我 这个函数不安全啥的啥的。怎么现在提示我 名称被标记? 谁帮我解释下 谢谢,我不想关闭这个警告
…|▌'寔堅蔃の/「≯還寔╪.逞蔃﹖
答案
-
Hi dowflyon,
这个错误意思是说,某个函数已经被标记为过时了,最好不要用,在将来的版本中,该函数可能就没有了,不存在了。
对于编译器警告,当然可以用 #pragma warning(disable: xxxx ) 的语法将其禁止掉,但是关闭这个警告并不正常,因为这样一来,所有过时的函数都不会再警告了,而我们可能是需要这个警告的。
像是对于strcpy这种超常用的函数,考虑到安全性(应对缓冲区溢出攻击),我们的确应该使用其安全版本,例如strcpy就有对应的StringCchCopy/StringCbCopy这样的函数,如果关闭了此警告,我们就可能在代码中不小心写下strcpy,而不是其对应的安全版本(当然,strcpy等函数是特例,关闭C4995警告后,仍然会有其他警告,下面有说明)。
所以,考察上述几个函数,我们知道其函数声明所在的头文件,这些头文件中的函数应该都不会用到,所以可以用另一种方式来避免引入这些头文件:
在你的工程的预编译头文件(一般来说,就是stdafx.h)中,在 #pragma once 一行后面加上下列三行:
1.#define _CSTDIO_
2.#define _CSTRING_
3.#define _CWCHAR_
这样,编译器就不会再加载 cstdio / cstring / cwchar 这几个头文件了。
注意:
使用 #pragma warning(disable: xxxx) 这种方式关闭警告后,如果代码里面用到了 strcpy 这样的函数,编译器会报另一个警告:
1.1>e:\work\ncksoft\test\main.cpp(126) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
2.1> d:\program\msvs2008\vc\include\string.h(74) : see declaration of 'strcpy'
所以我们仍然可以得到想要的警告信息。
如果您的问题解决了,请把有用的回答标记为答案!
谢谢,
Lucy
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 dowflyon 2011年1月19日 12:06
全部回复
-
Hi dowflyon,
这个错误意思是说,某个函数已经被标记为过时了,最好不要用,在将来的版本中,该函数可能就没有了,不存在了。
对于编译器警告,当然可以用 #pragma warning(disable: xxxx ) 的语法将其禁止掉,但是关闭这个警告并不正常,因为这样一来,所有过时的函数都不会再警告了,而我们可能是需要这个警告的。
像是对于strcpy这种超常用的函数,考虑到安全性(应对缓冲区溢出攻击),我们的确应该使用其安全版本,例如strcpy就有对应的StringCchCopy/StringCbCopy这样的函数,如果关闭了此警告,我们就可能在代码中不小心写下strcpy,而不是其对应的安全版本(当然,strcpy等函数是特例,关闭C4995警告后,仍然会有其他警告,下面有说明)。
所以,考察上述几个函数,我们知道其函数声明所在的头文件,这些头文件中的函数应该都不会用到,所以可以用另一种方式来避免引入这些头文件:
在你的工程的预编译头文件(一般来说,就是stdafx.h)中,在 #pragma once 一行后面加上下列三行:
1.#define _CSTDIO_
2.#define _CSTRING_
3.#define _CWCHAR_
这样,编译器就不会再加载 cstdio / cstring / cwchar 这几个头文件了。
注意:
使用 #pragma warning(disable: xxxx) 这种方式关闭警告后,如果代码里面用到了 strcpy 这样的函数,编译器会报另一个警告:
1.1>e:\work\ncksoft\test\main.cpp(126) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
2.1> d:\program\msvs2008\vc\include\string.h(74) : see declaration of 'strcpy'
所以我们仍然可以得到想要的警告信息。
如果您的问题解决了,请把有用的回答标记为答案!
谢谢,
Lucy
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 dowflyon 2011年1月19日 12:06