printf()包装器参数将由GCC检查

发布于 2025-01-25 11:06:57 字数 650 浏览 4 评论 0 原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2025-02-01 11:06:57

您可以使用GCC 格式函数属性,以检查与格式字符串的参数。

extern int my_printf (void *my_object, const char *my_format, ...)
           __attribute__ ((format (printf, 2, 3)));

检查GCC手册

You can use gcc format function attribute in order to check the parameters against the format string.

extern int my_printf (void *my_object, const char *my_format, ...)
           __attribute__ ((format (printf, 2, 3)));

Check the gcc manual "6.31.1 Common Function Attributes"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文