简体   繁体   中英

Warning: implicit declaration of function ‘vasprintf’?

#define _GNU_SOURCE

#include <errno.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>

static int mylog(const char *format, ...)
{
    va_list args;
    char *str = NULL;

    va_start(args, format);
    vasprintf(&str, format, args);
    va_end(args);

    fprintf(stdout, "Test >> %s", str);

    free(str);
    return 0;
}

int main(int argc, char *argv[])
{
    //...
}

warning: implicit declaration of function 'vasprintf'; did you mean 'vsprintf'? [-Wimplicit-function-declaration]

Why is the compiler issuing a warning for vasprintf ?

gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

To enable the Dynamic Allocation Functions, features from ISO/IEC TR 24731-2:2010, you have to define the macro __STDC_WANT_LIB_EXT2__ to 1 before to include the stdio.h header:

#define __STDC_WANT_LIB_EXT2__ 1  //Define you want TR 24731-2:2010 extensions
#define _GNU_SOURCE

#include <errno.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>

static int mylog(const char *format, ...)
{
    va_list args;
    char *str = NULL;

    va_start(args, format);
    vasprintf(&str, format, args);
    va_end(args);

    fprintf(stdout, "Test >> %s", str);

    free(str);
    return 0;
}

int main(int argc, char *argv[])
{
    //...
}

Note that these extensions are optional, GCC doesn't implements all of them, and other compilers can implement different set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM