C++ 默认情况下,在 Visual Studio 中从项目中删除安全警告 (_CRT_SECURE_NO_WARNINGS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16883037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:44:02  来源:igfitidea点击:

Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio

c++visual-studioprecompiler

提问by Juan Martinez

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_WARNINGSin the beginning of the code.

有没有办法在默认情况下为所有项目设置删除使用 scanf() 之类的函数时出现的预编译器安全警告。我发现您可以通过在项目选项中添加一行或#define _CRT_SECURE_NO_WARNINGS在代码开头添加一行来实现。

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.

在代码的开头,或者每次启动新项目时在预编译器选项中设置它。

回答by user2548100

Mark all the desired projects in solution explorer.

在解决方案资源管理器中标记所有所需的项目。

Press Alt-F7 or right click in solution explorer and select "Properties"

按 Alt-F7 或右键单击解决方案资源管理器并选择“属性”

Configurations:All Configurations

配置:所有配置

Click on the Preprocessor Definitions line to invoke its editor

单击 Preprocessor Definitions 行以调用其编辑器

Choose Edit...

选择编辑...

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

将“_CRT_SECURE_NO_WARNINGS”复制到顶部的预处理器定义白框中。

enter image description here

在此处输入图片说明

回答by Shaun314

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

可能是因为我还是 VS 的新手,而且绝对是 C 的新手,但唯一允许我构建的是添加

#pragma warning(disable:4996)

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

在我的文件顶部,这抑制了我在 sprintf 中遇到的 C4996 错误

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

我在这里读到它:https: //msdn.microsoft.com/en-us/library/2c8f766e.aspx

回答by Captain Obvlious

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

不是自动的,没有。你可以创建一个项目模板作为BlueWandered建议创建一个自定义属性表,您可以使用您当前和将来的所有项目。

  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.
  1. 打开物业管理器(查看->物业管理器)
  2. 在属性管理器中右键单击您的项目并选择“添加新项目属性表”
  3. 为其命名并在公共目录中创建它。属性表将添加到所有构建目标。
  4. 右键单击新的属性表并选择“属性”。这将打开属性并允许您更改设置,就像您为项目编辑它们一样。
  5. 进入“通用属性->C/C++->预处理器”
  6. 编辑设置“预处理器定义”并添加_CRT_SECURE_NO_WARNINGS.
  7. 保存,你就完成了。

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"
  1. 打开物业管理器(查看->物业管理器)
  2. 在 Property Manager 中右键单击您的项目并选择“添加现有项目属性表”

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_WARNINGSor libraries like Boost that you want to use in your projects.

这样做的好处是,您不仅可以在一个地方管理常用设置,而且可以在任何时候更改设置,它们都会传播到所有使用它的项目。如果您_CRT_SECURE_NO_WARNINGS想在项目中使用很多设置或类似 Boost 的库,这会很方便。

回答by PDF417

All the solutions here failed to work on my VS2013, however I put the #define _CRT_SECURE_NO_WARNINGSin the stdafx.h just before the #pragma onceand 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.

此处的所有解决方案都无法在我的 VS2013 上运行,但是我将它#define _CRT_SECURE_NO_WARNINGS放在了 stdafx.h 中,#pragma once并且所有警告都被抑制了。注意:我仅出于原型设计目的编写代码以支持我的研究,因此请确保您在编写代码时了解此方法的含义。

Hope this helps

希望这可以帮助

回答by ingconti

my two cents for VS 2017:

我 VS 2017 的两分钱:

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

我可以确认它在 stdafx.h 中以这些样式工作:

a)

一种)

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1 
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 

b)

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

(我为 MSDN 网络调用添加了另一个定义。)当然我更喜欢 a)。

I can confirm that: #define _CRT_SECURE_NO_WARNINGS (without a value) DOES NOTWORK.

我可以确认:#定义_CRT_SECURE_NO_WARNINGS(无值)不NOTWORK。

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

PS真正的重点是将这些定义放在函数声明之前,即在*.h之前

回答by Leninkumar

just copy " _CRT_SECURE_NO_WARNINGS" paste it on projects->properties->c/c++->preprocessor->preprocessor definitionsclick ok.it will work

只需复制“ _CRT_SECURE_NO_WARNINGS”将其粘贴到项目-> 属性-> c/c++-> 预处理器-> 预处理器定义单击确定即可。

回答by R Sahu

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.

如果您的项目不使用 stdafx.h,您可以将以下几行作为 .cpp 文件的第一行,编译器警告应该会消失——至少它在 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 comment and blank lines before them.

在它们之前有注释和空行是可以的。

回答by Michael Haephrati

Even though you can add _CRT_SECURE_NO_WARNINGSto your project's Properties, the easiest way is to use stdafx.h for any kind of warning and error disabling, just make sure you place the following line (and similar) BEFORE

即使您可以将_CRT_SECURE_NO_WARNINGS添加到项目的属性中,最简单的方法是使用 stdafx.h 进行任何类型的警告和错误禁用,只需确保在之前放置以下行(和类似行)

#pragma once

Also there is no need to define a value (i.e "1") for _CRT_SECURE_NO_WARNINGS as long as it is defined. So it should look like this:

此外,只要定义了 _CRT_SECURE_NO_WARNINGS 就不需要定义值(即“1”)。所以它应该是这样的:

#define _CRT_SECURE_NO_WARNINGS 

#pragma once