Is there any way to make visual C++ (9.0) generate warnings about printf format strings not matching type of printf's args?
Gcc很好地提供了-Wformat来帮助查找与printf相关的错误。 有什么办法可以在MSVC中获得相同的行为? 具体来说,我希望编译器对参数进行某种级别的类型检查。 由于各种原因,我明确地不想使用C ++的iostream库。 (而且我也不想使用增强格式)。
引用上面的源代码,-WFormat基本提供以下功能
Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense.
到目前为止,我能找到的最接近Microsoft的警告是与使用
不幸的是,没有办法在编译时生成这样的警告,但是VC ++代码分析工具将为带有不匹配参数的类printf函数生成警告消息。
有关更多详细信息,请参见VC ++中的
附带说明一下,人们一直在抱怨这一点,所以也许微软将来会做一些事情:
https://connect.microsoft.com/VisualStudio/feedback/details/799869/detection-of-format-string-errors-should-be-part-of-the-regular-c-compile-instead-of-analyze
我相信这不是Visual Studio中受支持的功能(我将尝试为此提供引用)。我知道的最接近的方法是使用
Specifically I'd like the compiler to do some level of type checking
on the arguments.
编译器喜欢在C ++代码中默认进行类型检查。不幸的是,您试图使用不提供该功能的C工具。
仅使用IO流,当类型不匹配时,编译器会做的不只是发出警告:它会发出错误并且无法完全编译您的代码!