[ 用户登陆 ] [ 免费注册 

Class >> 学习资料 > 程序技术 > VC >
warning C4996: 'sprintf': This function or variable may be unsafe
文 . 2011/10/5 13:44:50
警告信息:
warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

简单解决方法:
在程序最前加一行命令
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1



Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104

一、WINVER
  Compile result:
  WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)

  windows   server   2003  
  winver>=0x0502  
    
  windows   xp    
  winver>=0x0501  
    
  windows   2000  
  winver>=0x0500  
    
  windows   nt   4.0  
  winver>=0x0400  
    
  windows   me  
  winver>=0x0500  
    
  windows   98  
  winver>=0x0410  
    
  windows   95  
  winver>=0x0400  

二、编译警告:warning C4996 与 Security Enhancements in the CRT

将过去的工程用VS2005打开的时候。你有可能会遇到一大堆的警告:warning C4996。
比如:
warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use

_CRT_SECURE_NO_WARNINGS. See online help for details.
warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation,

use _CRT_SECURE_NO_WARNINGS.

原因是Visual C++ 2005使用了更加安全的run-time library routines。
新的Security CRT functions(就是那些带有“_s”后缀的函数):
http://msdn2.microsoft.com/en-us/library/wd3wzwts(VS.80).aspx

那么如何搞定这些警告呢:
方法一:将原来的旧函数替换成新的Security CRT functions。
方法二:用以下方法屏蔽这个警告。
1.在预编译头文件stdafx.h里(注意:一定要在没有include任何头文件之前)定义下面的宏:
  #define _CRT_SECURE_NO_DEPRECATE
2.#param warning(disable:4996)
3.更改预处理定义:
  项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加_CRT_SECURE_NO_DEPRECATE
方法三:方法二没有使用新的更安全的CRT函数,显然不是一个值得推荐的方法,可是你又不想一个一个地改,那么还有一个更方便的方法:
  在预编译头文件stdafx.h里(同样要在没有include任何头文件之前)定义下面的宏:
  #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
  在链接的时候便会自动将旧函数替换成Security CRT functions。
注意:这个方法虽然使用了新的函数,但是不能消除警告(原因见红字),你还得同时使用方法二。。。

三、link error 1104
原因:当从vc6移植到.net时,会导致这个链接错误!
解决:项目属性->配置属性->链接器->输入->忽略特定库,加入libcd.lib;或直接在命令行中加入: /nodefaultlib:"libcd.lib"
注意:是否是libcd.lib,与C/C++属性中的“代码生成”选项相关

参考:
Security Enhancements in the CRT :
http://msdn2.microsoft.com/en-us/library/8ef0s5kh(VS.80).aspx
Secure Template Overloads :
http://msdn2.microsoft.com/en-us/library/ms175759(VS.80).aspx

 URL: http://blog.csdn.net/hylaking/article/details/1700475 


最近后的内容
VS2003 中配置Microsoft PlatformSDK2003
vc++实现U盘介质加密解密保障存储安全
error C2440: “static_cast”: 无法从“UINT (__thiscall CCoolControlBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)”
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
最近前的内容
BOOL和bool的区别
wsprintf用法
C++命名书写规范/MFC、句柄、控件及结构的命名规范
C语言类型范围
error C2440: "static_cast": 无法从"void (__thiscall CChat::* )(WPARAM,LPARAM)"转换为"LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)"
最受欢迎
C语言类型范围
error C2440: "static_cast": 无法从"void (__thiscall CChat::* )(WPARAM,LPARAM)"转换为"LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)"
C++命名书写规范/MFC、句柄、控件及结构的命名规范
VS2003 中配置Microsoft PlatformSDK2003
error C2440: “static_cast”: 无法从“UINT (__thiscall CCoolControlBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)”
warning C4996: 'sprintf': This function or variable may be unsafe
BOOL和bool的区别
vc++实现U盘介质加密解密保障存储安全
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
wsprintf用法
 ©2005 - 2008 粤ICP备12037054号-1 用户登陆  |  互联说明  |  联系我们  |  网站地图