Vs [Warning c4996: 'fopen': This function or variable may be unsafe] Solution

Source: Internet
Author: User

Reprinted from: http://my.oschina.net/liujinofhome/blog/36287 and http://blog.sina.com.cn/s/blog_562f523f0100rezj.html

Http://blog.csdn.net/godenlove007/article/details/7794354

Ii. Compilation warning: Warning c4996 and security enhancements in the CRT


Open the previous project with vs2005. You may encounter a lot of Warnings: Warning c4996.

For example:
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.
1>         C: \ Program
Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ string. H (105): see the "strcpy" Statement

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.
1>         C: \ Program
Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ stdio. H (234): See the fopen statement.


The reason is that visual C ++ 2005 uses a safer run-time Library Routines.

New security CRT functions (functions with the suffix "_ s ):

Http://msdn2.microsoft.com/en-us/library/wd3wzwts (vs.80). aspx


So how can we handle these warnings:

Method 1: replace the old function with the new security CRT functions.

Method 2: Use the following method to block this warning.

1. Define the following macro in the pre-compiled header file stdafx. H (Note: Before you include any header files:

# DEFINE _ crt_secure_no_deprecate

2. # Param warning (Disable: 4996)

3. modify the definition of preprocessing:

Project-> properties-> Configuration properties-> C/C ++-> pre-processor definition, add _ crt_secure_no_deprecate

Method 3: method 2 does not use a new and safer CRT function. It is obviously not a recommended method, but you do not want to change it one by one. There is a more convenient method:

Define the following macro in the pre-compiled header file stdafx. H (before you include any header files:

# DEFINE _ crt_secure_cpp_overload_standard_names 1

The old function is automatically replaced with security CRT functions at the link.

Note: although this method uses a new function, it cannot eliminate the warning (for the reason, see the red letter). You must use method 2 at the same time...


3. Link error 1104

Cause: This Link error occurs when you port data from vc6 to. net!
Solution: choose Project Properties> Configuration Properties> linker> input> ignore a specified library and add libcd. LIB; or directly add:/nodefaultlib: "libcd. lib"

Note: whether it is libcd. Lib, which is related to the "Code Generation" option in the C/C ++ attribute.


Refer:

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

========================================================== ========================================

Cause explanation
This kind of Microsoft warning is mainly caused by the C-library functions. Many functions do not perform parameter detection (including out-of-bounds functions). Microsoft is worried that using these functions will cause memory exceptions, therefore, the same function is rewritten, and the modified function is used for parameter detection. Using these new functions is more secure and convenient. You don't need to remember these rewrite functions, because the compiler will tell you the corresponding security function when giving a warning to each function. You can view the warning information, for more information, see msdn. Example of database function Rewriting:
Modify mkdir to _ mkdir.
Fopen is rewritten to fopen_s
Rewrite stricmp to stricmp_s
Strcpy to strcpy_s

Solution:
1> follow the following warning prompt: see the "fopen" statement.
Message: "This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _ crt_secure_no_deprecate. See online help for details ."
So you can use the fopen_s function in the second sentence prompted by warning:
Example: file * pfile = fopen ("1.txt"," W ");
Changed:
File * pfile;
Fopen_s (& pfile, "1.txt"," W ");
2> Use _ crt_secure_no_deprecate
Project | attribute | Configuration Attribute | C/C ++ | Command Line | additional option, add [/D "_ crt_secure_no_deprecate"] (Note: add the complete content in brackets)
3> lower warning level: Project | attribute | Configuration Attribute | C/C ++ | standard, which lowers the warning level based on the situation (this method is not recommended)

Note: Warning is highly valued: the highest warning level for using the compiler. The build should be clean (no warning ). Understand all warnings. You can exclude warnings by modifying the code rather than lowering the warning level.
The compiler is your friend. If it sends a warning to a constructor, it often indicates a potential problem in your code. Successful building should be silent (with no warning ). [C ++ programming specifications]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.