GCC no longer implements <varargs.h>

家住魔仙堡 提交于 2021-02-07 06:28:08

问题


I have to change this code fragment from varargs.h to stdarg.h, but I do not know exactly how to:

#ifndef lint
int ll_log (va_alist)
va_dcl
{
    int event, result;
    LLog   *lp;
    va_list ap;

    va_start (ap);

    lp = va_arg (ap, LLog *);
    event = va_arg (ap, int);

    result = _ll_log (lp, event, ap);

    va_end (ap);

    return result;
}

When I try build this, compiler says:

error "GCC no longer implements <varargs.h>."
error "Revise your code to use <stdarg.h>."

The program, which I need to compile and run, has a few similar fragments and I need to know how to change them. If you can write some example, I'll be content.


回答1:


<varargs.h> is a pre-standard C header; use <stdarg.h> instead. The differences:

  1. The function must take at least one named argument.
  2. The function must be prototyped (using the ellipsis terminator).
  3. The va_start macro works differently: it takes two arguments, the first being the va_list to be initialized and the second the name of the last named argument.

Example:

int ll_log (LLog *llog, ...) {
    int event, result;
    LLog   *lp;
    va_list ap;

    va_start (ap, llog);

    lp = llog;
    event = va_arg (ap, int);

    result = _ll_log (lp, event, ap);

    va_end (ap);
    return result;
}

Regarding va_start: gcc ignores the second argument, but not giving the correct one is not portable.




回答2:


You have to include

#include <stdarg.h>

va_ Macro syntax stays the same.



来源:https://stackoverflow.com/questions/24950362/gcc-no-longer-implements-varargs-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!