当C printf()
及其家族由 gcc
和 -wall
在命令行上使用时,编译器警告说明错误的参数到使用的格式字符串。例如,下面的代码将收到一条错误消息,说明格式指定的3个参数,但实际上您只通过了两个参数。
printf("%d%d%d", 1, 2);
在将包装器编写到 printf()
上时,您如何保持此功能?我能想到的一种功能或宏将是一种形式。但是简单的解析器也可以接受。
可以在Stackoverflow上找到一些编写printf包装器的方法。两种常见方法是使用vprintf with varrintf 使用__builtin_apply 。我尝试了这两种方法,没有用。
When the C printf()
and its family is compiled by gcc
and -Wall
is used on command line, the compiler warns about misplaced arguments according to the format string that is being used. As an example the code below would get an error message saying the format specified 3 arguments but actually you have only passed in two.
printf("%d%d%d", 1, 2);
When writing a wrapper to the printf()
, how do you keep this capability? A form of function or a macro would be what I can think about. But simple parsers could be acceptable too.
A few ways of writing a printf wrapper can be found on the stackoverflow. Two common approaches are using vprintf with varargs, and using __builtin_apply. I've tried these two approaches, none worked.
发布评论
评论(1)
您可以使用GCC 格式函数属性,以检查与格式字符串的参数。
检查GCC手册
You can use gcc format function attribute in order to check the parameters against the format string.
Check the gcc manual "6.31.1 Common Function Attributes"