编译一些遗留代码A.c,它具有函数原型
void somefun(...)
gcc 4.1.2总是说出错误
error: ISO C requires a named argument before ...
但是我无法修改代码,所以我应该用什么C dialet选项让GCC编译这段代码?
gcc -c A.c ????
答案 0 :(得分:4)
我认为不再可能了。请参阅此处的评论(3.4.0 - 已经很老)GCC来源c-parse.in:
/* This is what appears inside the parens in a function declarator.
Is value is represented in the format that grokdeclarator expects. */
parmlist_2: /* empty */
{ $$ = get_parm_info (0); }
| ELLIPSIS
{ $$ = get_parm_info (0);
/* Gcc used to allow this as an extension. However, it does
not work for all targets, and thus has been disabled.
Also, since func (...) and func () are indistinguishable,
it caused problems with the code in expand_builtin which
tries to verify that BUILT_IN_NEXT_ARG is being used
correctly. */
error ("ISO C requires a named argument before `...'");
GCC 2.95.3有相同的评论。
较新版本的GCC(4.6.1)也没有选择接受该代码(来自gcc / c-parse.c):
static struct c_arg_info *
c_parser_parms_list_declarator (c_parser *parser, tree attrs)
{
...
if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
{
struct c_arg_info *ret = build_arg_info ();
/* Suppress -Wold-style-definition for this case. */
ret->types = error_mark_node;
error_at (c_parser_peek_token (parser)->location,
"ISO C requires a named argument before %<...%>");
c_parser_consume_token (parser);
答案 1 :(得分:3)
我不认为GCC中的任何C方言都接受这个,但G ++确实如此。你可以做的是将函数定义放在extern "C" {}
块中,并用g++
编译包含它的模块(假设它也是一个有效的C ++函数)。
然后,您必须使用void somefun()
(K&amp; R样式)在C中声明它。
但这也需要与g++
进行关联。
如果C ++链接不是您想要的,那么您应该将该函数更改为不带参数并以K&amp; R样式声明它。