1. 程式人生 > >vs的【warning C4996:'fopen': This function or variable may be unsafe】解決方案

vs的【warning C4996:'fopen': This function or variable may be unsafe】解決方案

二、編譯警告:warning C4996 與 Security Enhancements in the CRT

將過去的工程用VS2005開啟的時候。你有可能會遇到一大堆的警告:warning C4996。
比如:
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) : 參見“strcpy”的宣告


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) : 參見“fopen”的宣告

原因是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時,會導致這個連結錯誤!