6

To compile some legacy code A.c , which has a function prototype

void somefun(...)

gcc 4.1.2 always tell an error

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

But I can not modify the code, so what C dialet option should I use to let GCC compile this code?

gcc -c A.c ????
YourBestBet
  • 1,651
  • 1
  • 12
  • 17
  • 2
    Does that make any sense at all? If you don't have a named argument, you probably cannot access any of the arguments. So it should be the same as `void somefun()` (which is still different from `void somefun(void)`!!) – Szabolcs Jul 24 '11 at 13:54
  • I don't see any options of new version gcc can do this. – Stan Jul 24 '11 at 14:07
  • possible duplicate of [Is it possible to have a variadic function in C with no non-variadic parameter?](http://stackoverflow.com/questions/2622147/is-it-possible-to-have-a-variadic-function-in-c-with-no-non-variadic-parameter) – Ciro Santilli OurBigBook.com Jul 20 '15 at 13:23

2 Answers2

4

I don't think that possible anymore. See the comment in this (3.4.0 - quite old already) 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 has the same comment.

Newer versions of GCC (4.6.1) also don't see to have an option to accept that code (from 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);
Mat
  • 202,337
  • 40
  • 393
  • 406
3

I don't think any of the C dialects in GCC accept this, but G++ does. What you can do is put the function definition in an extern "C" {} block and compile the module containing it with g++ (assuming it's also a valid C++ function).

You must then declare it in C with void somefun() (K&R style).

This will require linking with g++ as well, though.

If C++ linkage is not what you want, then you should change the function to take no arguments and declare it in K&R style.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • This code really compiles on some old unix machine using cc , and even without any specific options. So, it seems I should check out that machine's CC version first. Thanks. – YourBestBet Jul 24 '11 at 14:27