gpt4 book ai didi

c - “ISO C99 要求可变参数宏中的 "..."至少有一个参数”专门使用 c11 和 -Wno-variadic-macros 时

转载 作者:行者123 更新时间:2023-12-02 01:20:36 39 4
gpt4 key购买 nike

我有一个简单的:#define log(text, ...) fprintf(stderr, "stuff before" text "stuff after", ## __VA_ARGS__);正在触发:error: ISO C99 requires at least one argument for the "..." in a variadic macro [-Werror]

应该使用-std=c11-Wno-variadic-macros不修复此错误/警告?

throw #pragma GCC system_header在日志定义之前的头文件中修复了这个问题(不一定测试输出的二进制文件是否有效...)但这似乎有点老套,我不完全确定它的后果。

以下是预期行为的示例:https://stackoverflow.com/a/31327708/5698848

From GNU

-Wvariadic-macros

Warn if variadic macros are used in ISO C90 mode, or if the GNU alternate syntax is used in ISO C99 mode.
This is enabled by either -Wpedantic or -Wtraditional.
To inhibit the warning messages, use -Wno-variadic-macros.

有什么好的解决方案可以阻止来自合法 GNU GCC C 代码的警告/错误吗?为什么它说我正在使用 C99,为什么禁用 C99 警告的标志不起作用?线路看起来像:

gcc -c src/file.c -Wall -Werror -Wextra -pedantic -Wfloat-equal -Wwrite-strings -Wcast-qual -Wunreachable-code -Wcast-align -Wstrict-prototypes -Wundef -Wshadow -Wstrict-aliasing -Wstrict-overflow -Wno-variadic-macros -g3 -std=c11 -O2 -flto -Iinclude/ -MMD -MF depend/file.d -o bin/file.o

请注意-pedantic确实是罪魁祸首。

<小时/>

MCVE

抄送

#include <stdio.h>
#include <stdlib.h>

#define log(text, ...) fprintf(stderr, "stuff before" text "stuff after", ## __VA_ARGS__);

int main(void)
{

log("should work, but doesn't");
log("works fine: %s", "yep");

return EXIT_SUCCESS;
}

生成文件

all:
gcc c.c -Wall -Werror -Wextra -pedantic -Wfloat-equal -Wwrite-strings -Wcast-qual -Wunreachable-code -Wcast-align -Wstrict-prototypes -Wundef -Wshadow -Wstrict-aliasing -Wstrict-overflow -Wno-variadic-macros -g3 -std=c11 -O2

注意:删除迂腐编译很好 - gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

最佳答案

在 ISO C99 和 C11 中,当您定义如下宏时:

#define log(text, ...)   something

那么宏的任何调用都必须至少采用 2 个参数。您的代码在 ISO C(所有版本)中格式不正确。

GCcflags -pedantic,根据 the documentation ,意味着:

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

GCC 开发人员决定将使用此扩展的代码包含在“一些不遵循 ISO C 的其他程序”下。如果您想在代码中使用此非标准扩展,则不应使用 -pedantic 标志。

如果您请求符合 C11,GCC 开发人员也不会费心修改错误消息的文本以显示“ISO C11 禁止...”。如果您担心这个问题,那么也许您可以提交补丁。

<小时/>

关于-Wno-variadic-macros,文档是:

Warn if variadic macros are used in ISO C90 mode, or if the GNU alternate syntax is used in ISO C99 mode.

“GNU 备用语法”似乎是指在 C99 之前首先启用可变参数宏的 GNU 语法,如 GCC documentation 中所述。 (而不是提供少于最小值的参数的扩展):

GCC has long supported variadic macros, and used a different syntax that allowed you to give a name to the variable arguments just like any other argument. Here is an example:

#define debug(format, args...) fprintf (stderr, format, args)

关于c - “ISO C99 要求可变参数宏中的 "..."至少有一个参数”专门使用 c11 和 -Wno-variadic-macros 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991106/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com