238

Is there a way to set by default for all projects removing the precompiler secure warnings that come up when using functions like scanf(). I found that you can do it by adding a line in the project option or a #define _CRT_SECURE_NO_WARNINGS in the beginning of the code.

I find myself repeatedly creating new projects for solving programming contests and it is really annoying (and takes valuable time) to add:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

In the beginning of the code, or to set it in the precompiler options every time I start a new project.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Juan Martinez
  • 2,520
  • 2
  • 16
  • 8

7 Answers7

332

Mark all the desired projects in solution explorer.

  • Press Alt-F7 or right click in solution explorer and select "Properties"
  • Configurations: All Configurations
  • Click on the Preprocessor Definitions line to invoke its editor
  • Choose Edit
  • Copy _CRT_SECURE_NO_WARNINGS into the Preprocessor Definitions white box on the top

Copy "_CRT_SECURE_NO_WARNINGS" into the Preprocessor Definitions white box on the top.

user2548100
  • 4,571
  • 1
  • 18
  • 18
  • 11
    This describes how to add it for one project which I think the OP already knows (although it's not 100% clear). The key question is how to add it so that it appears in all projects. Ideally, how can one add it to the %(PreprocessorDefinitions) macro so that it gets included everywhere? – Rob Gilliam Jan 30 '14 at 11:58
  • Fixed as of Jan 13th, 2015. –  Apr 26 '17 at 02:10
  • This only describes the first step. Once you have configured everything the way you need it, you will want to export a project template as well (see [How to: Create project templates](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-create-project-templates?view=vs-2019) for instructions). – IInspectable May 24 '19 at 19:19
  • 2
    I don't have Preprocessor Definitions tab. What can I do ? – Jorje12 Nov 28 '20 at 15:06
  • @Jorje12 add this to the top:#define _CRT_SECURE_NO_WARNINGS – chess_lover_6 Jun 08 '21 at 22:37
131

It may have been because I am still new to VS and definitely new to C, but the only thing that allowed me to build was adding

#pragma warning(disable:4996)

At the top of my file, this suppressed the C4996 error I was getting with sprintf

A bit annoying but perfect for my tiny bit of code and by far the easiest.

I read about it here: https://msdn.microsoft.com/en-us/library/2c8f766e.aspx

Shaun314
  • 3,191
  • 4
  • 22
  • 27
  • 7
    I tried every variation of #define shown on this page (with and without the 1 on the end) and only the #pragma worked for me. (VS2013 Community edition) I'm sure I'm missing something, but at some point, you just need it to work so you can get on with it. – Spike0xff Dec 01 '15 at 02:08
  • Had the exact same thing - it feels shitty but at the end of the day, well f*** it, it works @Spike0xff – Shaun314 Dec 01 '15 at 09:23
  • 2
    I can confirm _CRT_SECURE_NO_WARNINGS doesn't work in VC++ 2015 but above works. Thanks! – Shital Shah Sep 22 '16 at 07:00
  • @ShitalShah Is your confirmation based on personal experiments or some official Microsoft documentation? – qqqqq Jan 10 '17 at 18:30
  • @qqqqq personal experiments – Shital Shah Jan 10 '17 at 22:26
  • I used Visual Studio 2017 (version 15.9.1) and it only worked if I put it in the preprocessor command line in the configuration, not if I put it in code... strange. – karsten Nov 21 '18 at 15:56
  • And add `#pragma warning(default:4996)` after the block of code whose warnings you deliberately want to silence. Otherwise the pragma will apply until the end of the file, which you may not want. – Brian THOMAS Feb 20 '19 at 17:38
  • 1
    @bri: Unconditionally setting the default can have unwanted effects. You really meant to restore the behavior to what it was before. To do that, use [`#pragma warning(push)`/`#pragma warning(pop)`](https://learn.microsoft.com/en-us/cpp/preprocessor/warning#push-and-pop) instead. – IInspectable May 24 '19 at 19:30
31

Not automatically, no. You can create a project template as BlueWandered suggested or create a custom property sheet that you can use for your current and all future projects.

  1. Open up the Property Manager (View->Property Manager)
  2. In the Property Manager Right click on your project and select "Add New Project Property Sheet"
  3. Give it a name and create it in a common directory. The property sheet will be added to all build targets.
  4. Right click on the new property sheet and select "Properties". This will open up the properties and allow you to change the settings just like you would if you were editing them for a project.
  5. Go into "Common Properties->C/C++->Preprocessor"
  6. Edit the setting "Preprocessor Definitions" and add _CRT_SECURE_NO_WARNINGS.
  7. Save and you're done.

Now any time you create a new project, add this property sheet like so...

  1. Open up the Property Manager (View->Property Manager)
  2. In the Property Manager Right click on your project and select "Add Existing Project Property Sheet"

The benefit here is that not only do you get a single place to manage common settings but anytime you change the settings they get propagated to ALL projects that use it. This is handy if you have a lot of settings like _CRT_SECURE_NO_WARNINGS or libraries like Boost that you want to use in your projects.

Community
  • 1
  • 1
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
20

All the solutions here failed to work on my VS2013, however, I put the #define _CRT_SECURE_NO_WARNINGS in the stdafx.h just before the #pragma once and all warnings were suppressed.

Note: I only code for prototyping purposes to support my research so please make sure you understand the implications of this method when writing your code.

PDF417
  • 391
  • 3
  • 12
13
  • Copy _CRT_SECURE_NO_WARNINGS
  • Paste it on projects->properties->c/c++->preprocessor->preprocessor definitions
  • Click OK

It will work

Leninkumar
  • 314
  • 3
  • 3
12

For VS 2017:

I can confirm it works in stdafx.h both in these styles:

a)

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1 
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 

b)

#define _CRT_SECURE_NO_WARNINGS 1 
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 
#pragma once

(I have added another define for MSDN network calls... Of course, I do prefer a).

I can confirm that:

#define _CRT_SECURE_NO_WARNINGS

(without a value) DOES NOT WORK.

The real point is to put these defines BEFORE declarations of functions, i.e. before *.h

ingconti
  • 10,876
  • 3
  • 61
  • 48
0

If your project does not use stdafx.h, you can put the following lines as the first lines in your .cpp file and the compiler warning should go away -- at least it did for me in Visual Studio C++ 2008.

#ifdef _CRT_SECURE_NO_WARNINGS
#undef _CRT_SECURE_NO_WARNINGS
#endif
#define _CRT_SECURE_NO_WARNINGS 1

It's ok to have comments and blank lines before them.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • It's true. Worked for me on VS2010. Putting this after includes has no effect but as very first line it working... So what to do if we want to disable warning for part of code (for example inside a function)? – Mohammad Jun 26 '22 at 09:25
  • 1
    @Mohammad, it's my undestanding that you cannot disable it for part of a file and disable it for part of the file. One option is to divide the contents of a file into two files, enable it for one file and disable it for the other. – R Sahu Jun 27 '22 at 20:01