views:

3619

answers:

6

What is the best way to disable the warnings generated via _CRT_SECURE_NO_DEPRECATE that allows them to be reinstated with ease and will work across Visual Studio versions?

A: 

You can define the _CRT_SECURE_NO_WARNINGS symbol to suppress them and undefine it to reinstate them back.

dennisV
+6  A: 

If you don't wont to pollute your source code (after all this warning presents only with Microsoft compiler) add _CRT_SECURE_NO_WARNINGS symbol to your project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".

Also you can define it just before you include a header file which generates this warning. You should add something like this

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

And just a small remark, make sure you understand what this warning stands for and maybe if you don't intend to use other compilers then MSVC, consider using safer version of functions i.e. strcpy_s instead of strcpy.

Serge
+1  A: 

I'd like to point out that the warnings are actually there for a reason.

I'm not absolutely sure, but I believe the new, safer versions of the functions in question (CRT) exist in the new Windows SDK for older compilers as well. Moving to the new versions of functions is generally a good idea, since they're designed to better prevent overflows and other exploits.

psoul
They happen to serve quite well as part of an "Embrace, Extend, Extinguish" policy, something that Microsoft has been accused of before.
MSalters
Extinguish the moldy old unsecure standard functions? Wouldn't that be a shame. Also, since we're talking about Windows dev here, it shouldn't matter two shits if the proto differs from posix.
psoul
@MSalters: I guess those secure functions were written to make sure no MS developer would use them, at a time MS was struggling to remove unsecure code from its codebase. Perhaps they should be added as a standard to C and (most importantly) C++. Until them, better to have them than to have every developper write their own secure version (the last "strtok_safe" I saw was such a joke I can explain how much I wanted to bang my head on the table).
paercebal
MSalters
+2  A: 

You can also use the Secure Template Overloads, they will help you replace the unsecure calls with secure ones anywhere it is possible to easily deduce buffer size (static arrays).

Just add the following:

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

Then fix the remaining warnings by hand, by using the _s functions.

Drealmer
+1  A: 

hello,
i work on a multi platform project, so i can't use _s function and i don't want pollute my code with visual studio specific code.
my solution is disable the warning 4996 on the visual studio project. go to Project -> Properties -> Configuration properties -> C/C++ -> Advanced -> Disable specific warning add the value 4996.
if you use also the mfc and/or atl library (not my case) define before include mfc _AFX_SECURE_NO_DEPRECATE and before include atl _ATL_SECURE_NO_DEPRECATE.
i use this solution across visual studio 2003 and 2005.

p.s. if you use only visual studio the secure template overloads could be a good solution.

A: 

You could disable the warnings temporarily in places where they appear by using

#pragma warning(push)
#pragma warning(disable: warning-code)
// deprecated code here
#pragma warning(pop)

so you don't disable all warnings, which can be harmful at times.

macbirdie
-1 not relevant here.
Tomas