将Visual c++ Studio/Express设置为严格的ANSI模式

时间:2021-01-04 00:14:53

I generally program & compile under Linux with gcc and -ansi flag; but I've been forced with doing a job in Visual C++ and whenever I compile my C code I get all the Microsoft warnings like

我一般用gcc和-ansi标志在Linux下编程和编译;但是我不得不在Visual c++中工作,每当我编译我的C代码时,我都会得到微软的所有警告,比如

'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead.

fscanf:这个函数或变量可能是不安全的。考虑使用fscanf_s代替。

I get these despite following some steps on MSDN Developer's site for setting up an ANSI C project, but all the _s ("secure") calls are not ANSI C!

尽管我在MSDN开发人员的站点上执行了一些设置ANSI C项目的步骤,但是所有的_s(“secure”)调用都不是ANSI C!

Any suggestions on putting Visual C++ Studio in a strict ANSI-only mode?

对于将Visual c++ Studio置于严格的ansi模式有什么建议吗?

Thanks.

谢谢。

3 个解决方案

#1


2  

One way to suppress specific warnings is to add something like the following to the source.

抑制特定警告的一种方法是向源添加如下内容。

#if defined( _WIN32 )
#pragma warning(disable:4996)
#endif

#2


6  

As mentioned in another answer, #define'ing _CRT_SECURE_NO_WARNING will address the specific warnings you mentioned in your question.

正如在另一个答案中提到的,#define'ing _CRT_SECURE_NO_WARNING将解决您在问题中提到的特定警告。

If you're really looking for an ANSI-only mode, the closest thing is the /Za compiler switch. Inside the Visual Studio IDE, you can find it in the project's Properties dialog (under Configuration Properties | C/C++ | Language | Disable Language Extensions).

如果你真的在寻找一个只有ansi的模式,最接近的是/Za编译器开关。在Visual Studio IDE中,您可以在项目的属性对话框中找到它(在配置属性| C/ c++ |语言|下禁用语言扩展)。

Note that virtually all Windows apps build with Microsoft's compiler extensions enabled; e.g., I don't think you'd even be able to consume Windows SDK headers with /Za set. If your code truly is strict ANSI, you should be OK. If you have a few Windows-specific pieces in a project that is mostly strict ANSI, you could probably isolate those sources and only build those indivudal source files with /Za unset.

注意,几乎所有Windows应用程序都支持微软的编译器扩展;例如,我认为你甚至不能使用带有/Za集的Windows SDK标头。如果你的代码真的是严格的ANSI,你应该没问题。如果在一个大多数严格的ANSI项目中有一些特定于windows的片段,那么您可能会隔离这些源,并且只构建那些带有/Za unset的独立源文件。

#3


4  

These warnings can be suppressed by defining _CRT_SECURE_NO_WARNING

可以通过定义_CRT_SECURE_NO_WARNING来抑制这些警告

Go to Procect Settings -> Preprocessor and add _CRT_SECURE_NO_WARNING

进入Procect Settings ->预处理器,添加_CRT_SECURE_NO_WARNING

This isn't forcing compiler to comply with ANSI. Just suppresses use ..._s warnings

这并不是强迫编译器遵从ANSI。只是抑制使用…_警告

#1


2  

One way to suppress specific warnings is to add something like the following to the source.

抑制特定警告的一种方法是向源添加如下内容。

#if defined( _WIN32 )
#pragma warning(disable:4996)
#endif

#2


6  

As mentioned in another answer, #define'ing _CRT_SECURE_NO_WARNING will address the specific warnings you mentioned in your question.

正如在另一个答案中提到的,#define'ing _CRT_SECURE_NO_WARNING将解决您在问题中提到的特定警告。

If you're really looking for an ANSI-only mode, the closest thing is the /Za compiler switch. Inside the Visual Studio IDE, you can find it in the project's Properties dialog (under Configuration Properties | C/C++ | Language | Disable Language Extensions).

如果你真的在寻找一个只有ansi的模式,最接近的是/Za编译器开关。在Visual Studio IDE中,您可以在项目的属性对话框中找到它(在配置属性| C/ c++ |语言|下禁用语言扩展)。

Note that virtually all Windows apps build with Microsoft's compiler extensions enabled; e.g., I don't think you'd even be able to consume Windows SDK headers with /Za set. If your code truly is strict ANSI, you should be OK. If you have a few Windows-specific pieces in a project that is mostly strict ANSI, you could probably isolate those sources and only build those indivudal source files with /Za unset.

注意,几乎所有Windows应用程序都支持微软的编译器扩展;例如,我认为你甚至不能使用带有/Za集的Windows SDK标头。如果你的代码真的是严格的ANSI,你应该没问题。如果在一个大多数严格的ANSI项目中有一些特定于windows的片段,那么您可能会隔离这些源,并且只构建那些带有/Za unset的独立源文件。

#3


4  

These warnings can be suppressed by defining _CRT_SECURE_NO_WARNING

可以通过定义_CRT_SECURE_NO_WARNING来抑制这些警告

Go to Procect Settings -> Preprocessor and add _CRT_SECURE_NO_WARNING

进入Procect Settings ->预处理器,添加_CRT_SECURE_NO_WARNING

This isn't forcing compiler to comply with ANSI. Just suppresses use ..._s warnings

这并不是强迫编译器遵从ANSI。只是抑制使用…_警告