107

I have a 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++>>Preprocessor>>Preprocessor Definitions>> _CRT_NONSTDC_NO_WARNINGS

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

The only difference between the projects is several different options in wizard.

Why does _CRT_NONSTDC_NO_WARNINGS not help in the first project and why does the second project compile without problems without this definition?

AJM
  • 1,317
  • 2
  • 15
  • 30
vico
  • 17,051
  • 45
  • 159
  • 315
  • 13
    Did you try with _CRT_SECURE_NO_WARNINGS ? – Balu Mar 17 '14 at 09:26
  • 1
    may be your project in Visual Studio has a "treat warnings as errors" option enabled. – Balu Mar 17 '14 at 09:30
  • 3
    *Read the error message* – Hans Passant Mar 17 '14 at 09:41
  • 2
    "treat warnings as errors" is set to No (/WX-) – vico Mar 17 '14 at 09:52
  • yes, I could use strncpy_s, but why another project has no problems wit that? – vico Mar 17 '14 at 09:53
  • These type problems are a classic example of why it is important to learn to build from the command line to familiarize yourself with what options your code need to compile before jumping to the IDE where a bulk of the time you spend troubleshooting problems is simply where is that option buried in VSCode? – David C. Rankin Feb 04 '18 at 11:43

7 Answers7

158

Add by

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

screenshot of the relevant config interface

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
Balu
  • 2,247
  • 1
  • 18
  • 23
97

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

nexusclarum
  • 971
  • 1
  • 6
  • 3
33

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

kmcnamee
  • 5,097
  • 2
  • 25
  • 36
  • 4
    "I.E you should use the secure version of the strncpy." just FYI, the secure versions that the SDL checks are recommending seem to be [not portable to other OSes](https://stackoverflow.com/questions/858252/alternatives-to-ms-strncpy-s), which would make them not suitable for cross platform development. It looks like you would have to disable this check for programs that target more than just windows. – jrh Dec 19 '17 at 16:18
27

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(){
    //...
}
Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Carlosio
  • 409
  • 4
  • 6
  • 1
    why does `#define _CRT_SECURE_NO_WARNINGS` need to go above `#include?` – cal17_hogo Nov 10 '20 at 21:43
  • @cal17_hogo It depends on what's in the headers that are `#include`d. If the headers don't depend on anything that requires `_CRT_SECURE_NO_WARNINGS`, then the `#define` can go after the `#include`s. – Keith Thompson Apr 16 '21 at 01:17
  • 1
    @Keith The headers are what causes the compiler to emit the warnings. If you declare the function manually, there s no warning/error. So if you get the warning the define *must* go before the header. Here, it's `_Check_return_ _CRT_INSECURE_DEPRECATE(ctime_s)` in time.h, together with `#ifdef _CRT_SECURE_NO_WARNINGS`... earlier – Peter - Reinstate Monica Jan 24 '22 at 23:43
12

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.

Niall
  • 30,036
  • 10
  • 99
  • 142
user2703790
  • 153
  • 1
  • 7
5

Visual Studio 2019 with CMake

Add the following to CMakeLists.txt:

add_definitions(-D_CRT_SECURE_NO_WARNINGS)
rbento
  • 9,919
  • 3
  • 61
  • 61
0

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
....
molecoder
  • 423
  • 1
  • 7
  • 24