_CRT_SECURE_NO_DEPRECATE has no effect

MSDN clearly states that if you define _CRT_SECURE_NO_DEPRECATE it "will disable deprecation warnings".

I tried adding _CRT_SECURE_NO_DEPRECATE to the Preprocessor Definitions and #define _CRT_SECURE_NO_DEPRECATE to the code. In both cases I still get a C4996 warning from every single strcpy call. The only way to disable the warning, as far as I can tell, is to use #pragma warning (disable:4996) or add the warning to the C/C++ settings (Advanced > Disable Specific Warnings). But this will disable all deprecated warnings and I only want to disable insecure CRT warnings (str... functions).

Is the MSDN documentation false or am I doing something wrong


Answer this question

_CRT_SECURE_NO_DEPRECATE has no effect

  • Dhericean

    Make sure you define both _CRT_SECURE_NO_DEPRECATE and _CRT_NONSTDC_NO_DEPRECATE. I actually define them in the project settings (C++ > Preprocessor > Preprocessor Definitions). Also, make sure that you select Rebuild all. This will ensure that precompiled headers are compiled again.
  • IcemanNCalifornia

    Strange! For test with your code I just used the define in the command line and it worked.

    Any how: to your question about: _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES
    I have nothing else than this link.

    http://msdn2.microsoft.com/en-us/library/ms175759

  • Roona

    Can't reproduce it on a test project. The projects I'm having problems with are MFC DLLs created with VC6. The projects are rather large and it's difficult to set up the build environment, so posting one of them probably wouldn't be useful. I can send it to you directly if you're willing to have a closer look.

    The Preprocessor Definition I have are:

    _CRT_SECURE_NO_DEPRECATE
    _DEBUG
    WIN32
    _WINDOWS
    _AFXEXT
    WINVER=0x0500

  • falcon22

    Yep, that works. For some reason adding the two defines in the Preprocessor Definitions doesn't work. Adding them to Stdafx.h does. Also, MSDN has no mention of _CRT_NONSTDC_NO_DEPRECATE, so I think it's safe to say that MSDN docs are faulty on this matter.

    Anyways, problem solved. Thanks very much for your help, Martin.


  • XPolter

    Adding the definitions to the Preprocessor works but adding them on top of Stdafx.h, before any #include still doesn't.

    I am trying to make keep my projects backward compatible with VS 6.0, that is why I don't want to update my .dsp file. The following is on the top of Stdafx.h. It works well for one MFC GUI project, but not for a Win32 Dll:

    #if
    _MSC_VER > 1200
       #define _CRT_SECURE_NO_DEPRECATE
       #define _CRT_NONSTDC_NO_DEPRECATE
       #if defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
          #undef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
          #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
       #else
         #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
       #endif
    #endif

    Mo


  • hABi2aL

    I expect thats to large for our mail server. Place it on a secret link on one of your domains. Mail me the link. Than I can download it.

  • tomservo291

    Very strange! If defined in this way it should work. Send me a project (as small as possible) to my email address (look into my profile).
    Please only use a ZIP file.

  • Paul Cavanagh

    Martin,

    I can send a sample project, but the smallest one I can come up with right now is 10MB (zipped). I could strip the project a little but I'm a little pressed for time at the moment. Do you still want it

  • Kiet Quach

    So if the first line in my CPP file is #define _CRT_SECURE_NO_DEPRECATE, it should work Well, it doesn't! Nor does adding /D_CRT_SECURE_NO_DEPRECATE to the Preprocessor Definitions (with or without the /D switch).
  • luke1234564759

    You must declare _CRT_SECURE_NO_DEPRECATE before including any CRT files (like stdio.h or string.h) in order to disable the deprecation warnings.  For instance:


    #include <stdio.h>
    #include <string.h>

    #define _CRT_SECURE_NO_DEPRECATE

    int main()
    {
      printf("hello\n");
      char buf[30], buf2[30];
      buf2[0] = 'h';
      buf2[1] = 0;

      strcpy (buf,buf2);
    }

    Will give you  a deprecation warning while:

    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    int main()
    {
      printf("hello\n");
      char buf[30], buf2[30];
      buf2[0] = 'h';
      buf2[1] = 0;

      strcpy (buf,buf2);
    }

     

    Will not.  The easiest way to do this is simply to throw /D_CRT_SECURE_NO_DEPRECATE onto the command line for compilation.  (after cl.exe if you're compiling on the command line or in the project settings if your using the ide).

     

    Ben Anderson,

    VC++ Team



  • Pandit

    I put _CRT_NONSTDC_NO_DEPRECATE in Stdafx.h on the top before anything else but I still get the warning. Any clue

    Thanks,

    Mo

  • JoshTheAspirant

    The problem is that there is another define for the CRT functions.
    I am sorry that I didn't asked for the detailed warnings you got.

    Define _CRT_NONSTDC_NO_DEPRECATE too!
    Your code compiles in the Debug build with 1 warning and this warning is absolutely correct. There are link errors but I think they occur because I didn't

    I would recommend that you set those defines in your stdafx.h! Nobody looks into the project settings but the stdafx.h is a good place to document this!

    PS: I will delete your files if you mark this comment as "Answer to your question".

  • rtelford

     Rubio wrote:

    Yep, that works. For some reason adding the two defines in the Preprocessor Definitions doesn't work. Adding them to Stdafx.h does. Also, MSDN has no mention of _CRT_NONSTDC_NO_DEPRECATE, so I think it's safe to say that MSDN docs are faulty on this matter.



    I just hit the exact situation as above in which adding:
    /D _CRT_SECURE_NO_DEPRECATE  /D _CRT_NONSTDC_NO_DEPRECATE \
    to the command line did not solve the problem. The reason for me turned out to be that I needed to recompile my precompiled header to ensure that it was using the new defines.

    Normally I'd have thought that the compiler would have complained that I was using an out of date precompiled header, but it didn't in this case. Seems like a bug to me.

  • Kendor_bsc

    Could you please post a small sample reproducing the warning even after definning _CRT_SECURE_NO_DEPRECATE

    Thanks,
      Ayman Shoukry
      VC++ Team

  • _CRT_SECURE_NO_DEPRECATE has no effect