简体   繁体   English

本地变量声明放置需要Microsoft C / C ++编译器开关

[英]Microsoft C/C++ compiler switch needed for local variable declaration placement

I'm compiling some C files using Microsoft C/C++ and it's complaining about declaring local variables inside a block. 我正在使用Microsoft C / C ++编译一些C文件,并且抱怨在块内声明局部变量。 Declaring them at the beginning of a block, of course, is fine. 当然,在块的开头声明它们是可以的。 What compiler switch can I use to suppress the errors that I'm getting? 我可以使用哪种编译器开关来抑制出现的错误?

Much appreciated, 非常感激,

kris 克里斯

In C89 and earlier, all block-scope variable declarations must come before any statements. 在C89和更早版本中,所有块范围变量声明必须在任何语句之前。 C99 changed this rule, so that declarations and statements may be intermixed as in C++. C99更改了此规则,因此可以像在C ++中那样将声明和语句混合在一起。

Unfortunately, Microsoft has chosen not to support C99 in Visual Studio and has no plans to AFAIK. 不幸的是,Microsoft选择了不支持Visual Studio中的C99,并且没有AFAIK的计划。

In standard C, you can't declare variables anywhere but at the beginning. 在标准C中,您只能在开头声明变量。 This is different from C++ where variables can be declared anywhere. 这不同于C ++,在C ++中,可以在任何地方声明变量。

So you must compile the files as if they were C++ via /TP . 因此,必须通过/TP将文件编译为C ++。

See this article for more details. 有关更多详细信息,请参见本文

CL.exe /Tpfilename

It means "compile as C++ regardless of the extension". 它的意思是“不管扩展名都是C ++编译”。

Or, to put it another way, what you're doing is illegal in C, so of course it complains. 或者,换句话说,您的操作在C语言中是非法的,因此当然会抱怨。 Either change it to C++ or force it to be read as C++. 将其更改为C ++或强制将其读取为C ++。

You can use /TP to force the compiler to compile them as C++ files. 您可以使用/ TP强制编译器将它们编译为C ++文件。 I'm not sure if this is what you want, however. 但是,我不确定这是否是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM