6

编译一些遗留代码 Ac ,它有一个函数原型

void somefun(...)

gcc 4.1.2 总是报错

 error: ISO C requires a named argument before ...

但是我不能修改代码,那么我应该使用什么 C dialet 选项让 GCC 编译这段代码?

gcc -c A.c ????
4

2 回答 2

4

我认为这不再可能了。请参阅此(3.4.0 - 已经很老了)GCC source 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);
于 2011-07-24T14:02:56.890 回答
3

我认为 GCC 中的任何 C 方言都不接受这一点,但 G++ 可以。您可以做的是将函数定义放在一个extern "C" {}块中并编译包含它的模块g++(假设它也是一个有效的 C++ 函数)。

然后,您必须使用void somefun()(K&R 样式)在 C 中声明它。

不过,这也需要链接g++

如果 C++ 链接不是您想要的,那么您应该将函数更改为不带参数并以 K&R 样式声明它。

于 2011-07-24T14:05:17.923 回答