Chinaunix首页 | 论坛 | 博客
  • 博客访问: 380844
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 560
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-10 08:38
文章分类
文章存档

2016年(32)

2015年(48)

我的朋友

分类: LINUX

2015-06-03 16:05:29

在编译时, 报PTHREAD_MUTEX_ERRORCHECK undeclared
和PTHREAD_PRIO_INHERIT 
undeclared的解决方法


error: implicit declaration of function ‘pthread_mutexattr_settype’ 引出GNU_SOURCE探索

分类: 经验总结 Linux C2013-05-23 14:58 2004人阅读 评论(2)  举报

这两天在做64位移植的工作,在编译某组建时报错如下:


  1. error: implicit declaration of function ‘pthread_mutexattr_settype’  




造成这个错误的原因网上已经说得很明白了,就是由于没有添加所需要的头文件导致的,于是找到了函数pthread_mutexattr_settype的声明(在 文件中):


  1. #ifdef __USE_UNIX98  
  2. /* Return in *KIND the mutex kind attribute in *ATTR.  */  
  3. extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict __attr, int *__restrict __kind)  
  4.     __THROW;  
  5.   
  6. /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL, 
  7.    PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or 
  8.    PTHREAD_MUTEX_DEFAULT).  */  
  9. extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)  
  10.     __THROW;  
  11.   
  12. /* Return in *PROTOCOL the mutex protocol attribute in *ATTR.  */  
  13. extern int pthread_mutexattr_getprotocol (__const pthread_mutexattr_t *__restrict __attr, int *__restrict __protocol)  
  14.     __THROW;  
  15.   
  16. /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either 
  17.    PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT).  */  
  18. extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr, int __protocol)  
  19.     __THROW;  
  20.   
  21. /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR.  */  
  22. extern int pthread_mutexattr_getprioceiling (__const pthread_mutexattr_t *__restrict __attr, int *__restrict __prioceiling)  
  23.     __THROW;  
  24.   
  25. /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING.  */  
  26. extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr, int __prioceiling)  
  27.     __THROW;  
  28. #endif  




如果没有定义__USE_UNIX98这个宏,那就没有声明pthread_mutexattr_settype,后面也就无法使用该函数。接下来,翻阅了一番资料后发现,/usr/include/features.h头文件中有如下代码:


  1. #ifdef  _XOPEN_SOURCE  
  2. #     define __USE_XOPEN    1  
  3. #     if (_XOPEN_SOURCE - 0) >= 500  
  4. #         define __USE_XOPEN_EXTENDED  1  
  5. #         define __USE_UNIX98  1  
  6. #         undef _LARGEFILE_SOURCE  
  7. #         define _LARGEFILE_SOURCE     1  
  8. #         if (_XOPEN_SOURCE - 0) >= 600  
  9. #             if (_XOPEN_SOURCE - 0) >= 700  
  10. #                 define __USE_XOPEN2K8      1  
  11. #             endif  
  12. #             define __USE_XOPEN2K        1  
  13. #             undef __USE_ISOC99  
  14. #             define __USE_ISOC99         1  
  15. #         endif  
  16. #     else  
  17. #         ifdef _XOPEN_SOURCE_EXTENDED  
  18. #             define __USE_XOPEN_EXTENDED 1  
  19. #         endif  
  20. #     endif  
  21. #endif  






代码表明,如果_XOPEN_SOURCE 的值大于等于500,那么就会定义_USE_UNIX98为1,所以我们可以定义_XOPEN_SOURCE为500。
这样的话,pthread.h头文件中的pthread_mutexattr_settype函数也就会被声明。




而features.h中还有另一段代码:


  1. /* If _GNU_SOURCE was defined by the user, turn on all the other features.  */  
  2. #ifdef _GNU_SOURCE  
  3. # undef  _ISOC99_SOURCE  
  4. # define _ISOC99_SOURCE 1  
  5. # undef  _POSIX_SOURCE  
  6. # define _POSIX_SOURCE  1  
  7. # undef  _POSIX_C_SOURCE  
  8. # define _POSIX_C_SOURCE        200809L  
  9. # undef  _XOPEN_SOURCE  
  10. # define _XOPEN_SOURCE  700  
  11. # undef  _XOPEN_SOURCE_EXTENDED  
  12. # define _XOPEN_SOURCE_EXTENDED 1  
  13. # undef  _LARGEFILE64_SOURCE  
  14. # define _LARGEFILE64_SOURCE    1  
  15. # undef  _BSD_SOURCE  
  16. # define _BSD_SOURCE    1  
  17. # undef  _SVID_SOURCE  
  18. # define _SVID_SOURCE   1  
  19. # undef  _ATFILE_SOURCE  
  20. # define _ATFILE_SOURCE 1  
  21. #endif  





	


也就是说,_GUN_SOURCE宏一旦被定义,就会定义上面几种宏,其中会定义_XOPEN_SOURCE为700,正好满足了定义_GNU_SOURCE宏的条件。所以我也可以定义宏_GNU_SOURCE.


综上所述,在编译时加上编译选项:
-D_XOPEN_SOURCE=500 或 -D_GNU_SOURCE 即可解决。


其实还有另一种方法解决error: implicit declaration of function ‘pthread_mutexattr_settype’ 错误,那就是在编译的时候把编译选项“-Werror-implicit-function-declaration”去掉即可,但是不推荐去掉该选项!



阅读(5381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~