聘我网

新概念招聘3.0

为啥总是报implicit declaration of function ‘strndup’

vote up0vote downstar

按照man strndup中的要求:

   ...
   #define _GNU_SOURCE
   #include <string.h>

   char *strndup(const char *s, size_t n);
   ...

已经加了:

   #define _GNU_SOURCE
   #include <string.h>

但为何还是报:

japanese.l: In function ‘yylex’:
japanese.l:12: warning: implicit declaration of function ‘strndup’
japanese.l:12: warning: incompatible implicit declaration of built-in function ‘strndup’
japanese.l:17: warning: incompatible implicit declaration of built-in function ‘strndup’

有人碰到过没?

 

2 个答复

vote up0vote downcheck

碰到过,是因为man和头文件不一致(可能是版本导致也可能就是mistake),

最后结果当然以实际的头文件为准。

vim /usr/include/string.h:

#if defined __USE_GNU
extern char *strndup (__const char *__string, size_t __n)
     __THROW __attribute_malloc__ __nonnull ((1));
#endif

所以显然这时候应该#define __USE_GNU而不是#define _GNU_SOURCE..

更新

关于_GNU_SOURCE宏,

它同时也是features.h中用于特性控制的一个功能测试宏

</user/include/features.h>

/* If _GNU_SOURCE was defined by the user, turn on all the other features.  */

#ifdef _GNU_SOURCE

# undef  _ISOC99_SOURCE

# define _ISOC99_SOURCE 1

# undef  _POSIX_SOURCE

# define _POSIX_SOURCE  1

# undef  _POSIX_C_SOURCE

# define _POSIX_C_SOURCE    200809L

# undef  _XOPEN_SOURCE

# define _XOPEN_SOURCE  700

# undef  _XOPEN_SOURCE_EXTENDED

# define _XOPEN_SOURCE_EXTENDED 1

# undef  _LARGEFILE64_SOURCE

# define _LARGEFILE64_SOURCE    1

# undef  _BSD_SOURCE

# define _BSD_SOURCE    1

# undef  _SVID_SOURCE

# define _SVID_SOURCE   1

# undef  _ATFILE_SOURCE

# define _ATFILE_SOURCE 1

#endif

其中_ISOC99_SOURCE, _POSIX_SOURCE, _XOPEN_SOURCE都是功能测试宏,

用于指示是否包含对应标准的特性,

而这些不同的特性源于过去20多年来进行的各种标准化工作(ANSI, ISO, POSIX, FIPS等),

不同的标准支持实现了不同的特性,

如系统时间的获取,stat结构是在ANSI标准中是不支持的,

而定义了_GNU_SOURCE相当于开启了对所有特性的支持。

参考:

_GUN_SOURCE宏

链接
vote up0vote down

这个问题我碰到了,是因为内核中有个BUG,在lib里面没有把strndup 加EXPORT_SYMBOL

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.