vico
vico

Reputation: 18171

How to use _CRT_SECURE_NO_WARNINGS

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?

Upvotes: 118

Views: 524275

Answers (10)

Paulo Guaruja
Paulo Guaruja

Reputation: 11

adding #define _CRT_SECURE_NO_WARNINGS before any other heading worked for me.

Upvotes: 1

Amr Wael
Amr Wael

Reputation: 1

this code works with me (I am using Visual Studio 2022 IDE):

// put me on your main.cpp
#define _CRT_SECURE_NO_WARNINGS

Upvotes: 0

rs4734
rs4734

Reputation: 1

the _CRT_SECURE_NO_WARNINGS addition to the preprocessor settings worked great for "gets" warnings and this is on an ancient version of Visual C++ 6.0 before Visual Studio.

Upvotes: 0

rbento
rbento

Reputation: 11608

Visual Studio 2019 with CMake

Add the following to CMakeLists.txt:

add_definitions(-D_CRT_SECURE_NO_WARNINGS)

Upvotes: 7

molecoder
molecoder

Reputation: 473

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
....

Upvotes: 0

user2703790
user2703790

Reputation: 153

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.

Upvotes: 12

Carlosio
Carlosio

Reputation: 479

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(){
    //...
}

Upvotes: 33

Balu
Balu

Reputation: 2447

Add by

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

screenshot of the relevant config interface

Upvotes: 176

kmcnamee
kmcnamee

Reputation: 5255

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

Upvotes: 35

nexusclarum
nexusclarum

Reputation: 991

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

Upvotes: 99

Related Questions