Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE

前端 未结 10 1543
说谎
说谎 2020-12-02 06:00

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 Stud

相关标签:
10条回答
  • 2020-12-02 06:18

    Combination of @[macbirdie] and @[Adrian Borchardt] answer. Which proves to be very useful in production environment (not messing up previously existing warning, especially during cross-platform compile)

    #if (_MSC_VER >= 1400)         // Check MSC version
    #pragma warning(push)
    #pragma warning(disable: 4996) // Disable deprecation
    #endif 
    //...                          // ...
    strcat(base, cat);             // Sample depreciated code
    //...                          // ...
    #if (_MSC_VER >= 1400)         // Check MSC version
    #pragma warning(pop)           // Renable previous depreciations
    #endif
    
    0 讨论(0)
  • 2020-12-02 06:18

    The best way to do this is by a simple check and assess. I usually do something like this:

    #ifndef _DEPRECATION_DISABLE   /* One time only */
    #define _DEPRECATION_DISABLE   /* Disable deprecation true */
    #if (_MSC_VER >= 1400)         /* Check version */
    #pragma warning(disable: 4996) /* Disable deprecation */
    #endif /* #if defined(NMEA_WIN) && (_MSC_VER >= 1400) */
    #endif /* #ifndef _DEPRECATION_DISABLE */
    

    All that is really required is the following:

    #pragma warning(disable: 4996)
    

    Hasn't failed me yet; Hope this helps

    0 讨论(0)
  • 2020-12-02 06:18

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

    0 讨论(0)
  • 2020-12-02 06:20

    For the warning by warning case, It's wise to restore it to default at some point, since you are doing it on a case by case basis.

    #pragma warning(disable: 4996) /* Disable deprecation */
    // Code that causes it goes here
    #pragma warning(default: 4996) /* Restore default */
    
    0 讨论(0)
  • 2020-12-02 06:22

    If you don't want 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 than MSVC, consider using safer version of functions i.e. strcpy_s instead of strcpy.

    0 讨论(0)
  • 2020-12-02 06:27

    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.

    0 讨论(0)
提交回复
热议问题