implicit declaration of function 'asprintf' 警告的解决

来源:互联网 发布:grub windows引导丢失 编辑:程序博客网 时间:2024/06/14 13:35


asprintf使用起来非常方便,但是它是GNU扩展的C函数库,使用的时候经常会有如下警告:

warning: implicit declaration of function 'asprintf' [-Wimplicit-function-declaration]


调查发现asprintf的头文件是stdio.h,在该头文件中

#ifdef __USE_GNU/* Write formatted output to a string dynamically allocated with `malloc'.   Store the address of the string in *PTR.  */extern int vasprintf (char **__restrict __ptr, __const char *__restrict __f,      _G_va_list __arg)     __THROW __attribute__ ((__format__ (__printf__, 2, 0))) __wur;extern int __asprintf (char **__restrict __ptr,       __const char *__restrict __fmt, ...)     __THROW __attribute__ ((__format__ (__printf__, 2, 3))) __wur;extern int asprintf (char **__restrict __ptr,     __const char *__restrict __fmt, ...)     __THROW __attribute__ ((__format__ (__printf__, 2, 3))) __wur;#endif

但是__USE_GNU是glibc内容的宏定义,我们不能在自己的代码中定义,因此要怎么解决呢?


解决方法:

在makefile中加入编译选项 -D_GNU_SOURCE

如果是用CMake,需加入add_definitions (-D_GNU_SOURCE)


如果遇到类似因#ifdef __USE_GNU引起的问题,可以通过如上设定尝试解决。


0 0
原创粉丝点击