How to use _CRT_SECURE_NO_WARNINGS

C++Visual C++Visual Studio-2012Warnings

C++ Problem Overview


I have compile error in my simple MFC window application generated from wizard with several lines of code:

> error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

I set Configuration Properties>>C/C++>>Preporocessor>>Preprocessor Definitions>> _CRT_NONSTDC_NO_WARNINGS

But this does't helped. I have another very close project that generates only warning in this place and it has no _CRT_NONSTDC_NO_WARNINGS definition.

Only difference between projects is several different options in wizard.

Why _CRT_NONSTDC_NO_WARNINGS does not helps in first project and why second project compiles without problems without this definition?

C++ Solutions


Solution 1 - C++

Add by

> Configuration Properties>>C/C++>>Preporocessor>>Preprocessor > Definitions>> _CRT_SECURE_NO_WARNINGS

screenshot of the relevant config interface

Solution 2 - C++

Under "Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add _CRT_SECURE_NO_WARNINGS

Solution 3 - C++

If your are in Visual Studio 2012 or later this has an additional setting 'SDL checks' Under Property Pages -> C/C++ -> General

> Additional Security Development Lifecycle (SDL) recommended checks; includes enabling additional secure code generation features and extra security-relevant warnings as errors.

It defaults to YES - For a reason, I.E you should use the secure version of the strncpy. If you change this to NO you will not get a error when using the insecure version.

SDL checks in vs2012 and later

Solution 4 - C++

For a quick fix or test, I find it handy just adding #define _CRT_SECURE_NO_WARNINGS to the top of the file before all #include

#define _CRT_SECURE_NO_WARNINGS
#include ...
int main(){
    //...
}

Solution 5 - C++

Adding _CRT_SECURE_NO_WARNINGS to Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions didn't work for me, don't know why.

The following hint works: In stdafx.h file, please add

#define _CRT_SECURE_NO_DEPRECATE

before include other header files.

Solution 6 - C++

Visual Studio 2019 with CMake

Add the following to CMakeLists.txt:

add_definitions(-D_CRT_SECURE_NO_WARNINGS)

Solution 7 - C++

I was getting the same error in Visual Studio 2017 and to fix it just added #define _CRT_SECURE_NO_WARNINGS after #include "pch.h"

#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
....

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvicoView Question on Stackoverflow
Solution 1 - C++BaluView Answer on Stackoverflow
Solution 2 - C++nexusclarumView Answer on Stackoverflow
Solution 3 - C++kmcnameeView Answer on Stackoverflow
Solution 4 - C++CarlosioView Answer on Stackoverflow
Solution 5 - C++user2703790View Answer on Stackoverflow
Solution 6 - C++rbentoView Answer on Stackoverflow
Solution 7 - C++molecoderView Answer on Stackoverflow