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引起的问题,可以通过如上设定尝试解决。
本文介绍了解决使用asprintf函数时出现的警告问题的方法。asprintf是GNU C库的一部分,在使用时可能会遇到未声明的警告。文章提供了两种解决方案:通过makefile添加编译选项-D_GNU_SOURCE或在CMake中加入add_definitions(-D_GNU_SOURCE)。

2308

被折叠的 条评论
为什么被折叠?



