免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 13558 | 回复: 14
打印 上一主题 下一主题

pthread_atfork()的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-21 14:43 |只看该作者 |倒序浏览
I am so close!!在我的程序中用到了pthread_atfork函数,在编译的时候我也用了 -lpthread 但是还是出现下面的引用错误,不知道是为什么?向各位请教。
/tmp/ccUmWc7g.o(.text+0xf9):In function 'main':
: undefined reference to `pthread_atfork`
collect2: ld returned 1 exit status

论坛徽章:
0
2 [报告]
发表于 2007-06-21 15:01 |只看该作者
一定要等个人回答

论坛徽章:
0
3 [报告]
发表于 2007-06-21 16:21 |只看该作者
去/lib下看一看,libpthread.so符号链接指向的那个动态库里面有没有这个函数.

论坛徽章:
0
4 [报告]
发表于 2007-06-21 16:25 |只看该作者
原帖由 andyxie407 于 6/21/2007 14:43 发表
I am so close!!在我的程序中用到了pthread_atfork函数,在编译的时候我也用了 -lpthread 但是还是出现下面的引用错误,不知道是为什么?向各位请教。
/tmp/ccUmWc7g.o(.text+0xf9):In function 'main':
: unde ...


不知道你的程序包含了头文件#include <pthread.h>了吗?如果没有的话也会报这个错的!

论坛徽章:
0
5 [报告]
发表于 2007-06-21 19:36 |只看该作者
原帖由 weigongwan 于 2007-6-21 16:25 发表


不知道你的程序包含了头文件#include <pthread.h>了吗?如果没有的话也会报这个错的!


头文件有#include <pthread.h>

论坛徽章:
0
6 [报告]
发表于 2007-06-21 19:45 |只看该作者
原帖由 baohuaihuai 于 2007-6-21 16:21 发表
去/lib下看一看,libpthread.so符号链接指向的那个动态库里面有没有这个函数.

在/usr/lib下ls -l 看到 libthread.so 没有符号连接
在/lib下并没有 libthread.so 文件

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
7 [报告]
发表于 2007-06-21 20:44 |只看该作者
原帖由 andyxie407 于 2007-6-21 14:43 发表
I am so close!!在我的程序中用到了pthread_atfork函数,在编译的时候我也用了 -lpthread 但是还是出现下面的引用错误,不知道是为什么?向各位请教。
/tmp/ccUmWc7g.o(.text+0xf9):In function 'main':
: unde ...

完整的编译命令。必要的话把源码也附上。

论坛徽章:
0
8 [报告]
发表于 2008-10-23 14:36 |只看该作者

回复 #1 andyxie407 的帖子

我遇到了一样的问题。也是链接的时候找不到。在静态库里能找到这个函数。再请问一下动态库里的函数能用什么命令查找吗?

@s2 ~/testprogram $ nm -os  /usr/lib/libpthread.a>liblist.c
@s2 ~/testprogram $ grep "pthread_atfork" liblist.c
__pthread_atfork in pthread_atfork.o
pthread_atfork in pthread_atfork.o
/usr/lib/libpthread.a:pthread_atfork.o:         w __dso_handle
/usr/lib/libpthread.a:pthread_atfork.o:00000000 T __pthread_atfork
/usr/lib/libpthread.a:pthread_atfork.o:         U __register_atfork
/usr/lib/libpthread.a:pthread_atfork.o:00000000 T pthread_atfork
@s2 ~/testprogram $
@s2 ~/testprogram $

  1. wei.liang@s2 ~/testprogram $ cat pthread_atfork_test.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>

  5. pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
  6. pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;

  7. void prepare(void)
  8. {
  9. printf("prepare lock...\n");
  10. pthread_mutex_lock(&lock1);
  11. pthread_mutex_lock(&lock2);
  12. }
  13. void parent(void)
  14. {
  15. printf("parent unlock...\n");
  16. pthread_mutex_unlock(&lock1);
  17. pthread_mutex_unlock(&lock2);
  18. }
  19. void child(void)
  20. {
  21. printf("child unlock...\n");
  22. pthread_mutex_unlock(&lock1);
  23. pthread_mutex_unlock(&lock2);
  24. }

  25. void thr_fn(void *arg)
  26. {
  27. printf("thread started...\n");
  28. pause();
  29. return;
  30. }

  31. int main(void)
  32. {
  33. int err;
  34. int pid;
  35. pthread_t tid;

  36. #if defined(BSD)||defined(MACOS)
  37. printf("pthread_atfork not support!\n");
  38. #else
  39. err = pthread_atfork(prepare,parent,child);
  40. if (err != 0){
  41.     printf("can't install fork handles!\n");
  42.     return -1;
  43.     }
  44. err = pthread_create(&tid,NULL,thr_fn,NULL);
  45. if (err != 0){
  46.    printf("can't create new thread!\n");
  47.    return -1;
  48.    }

  49. sleep(2);
  50. printf("parent about to fork...\n");
  51. pid = fork();
  52. if (pid < 0){
  53.     printf("fork failed.\n");
  54.     return -1;
  55.    }else if (pid == 0){
  56.    printf("child return fork!\n");
  57.    }else{
  58.    printf("parent return fork!\n");
  59.    }
  60. #endif

  61. return 0;
  62. }
  63. wei.liang@s2 ~/testprogram $
  64. wei.liang@s2 ~/testprogram $
  65. wei.liang@s2 ~/testprogram $ gcc -Wall -lpthread pthread_atfork_test.c
  66. pthread_atfork_test.c: In function 'main':
  67. pthread_atfork_test.c:48: warning: passing argument 3 of 'pthread_create' from incompatible pointer type
  68. /tmp/ccM75C6J.o: In function `main':
  69. pthread_atfork_test.c:(.text+0xc6): undefined reference to `pthread_atfork'
  70. collect2: ld returned 1 exit status
  71. @s2 ~/testprogram $ uname -a
  72. Linux s2 2.6.25-gentoo-r7 #1 SMP Sun Jul 27 15:19:28 CST 2008 i686 Intel(R) Pentium(R) Dual CPU E2180 @ 2.00GHz GenuineIntel GNU/Linux
  73. @s2 ~/testprogram $ gcc -v
  74. Using built-in specs.
  75. Target: i686-pc-linux-gnu
  76. Configured with: /var/tmp/portage/sys-devel/gcc-4.1.2/work/gcc-4.1.2/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.1.2 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4 --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --disable-libunwind-exceptions --disable-multilib --enable-libmudflap --disable-libssp --disable-libgcj --with-arch=i686 --enable-languages=c,c++,treelang,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
  77. Thread model: posix
  78. gcc version 4.1.2 (Gentoo 4.1.2 p1.1)
复制代码

[ 本帖最后由 wliang511 于 2008-10-23 14:41 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2010-03-31 16:55 |只看该作者
我也碰到一样的问题啊,有谁知道怎么解决吗?告知一声啊

论坛徽章:
0
10 [报告]
发表于 2010-03-31 18:48 |只看该作者
记得不是太清了,编译时在-lpthread后面加个 -lrt试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP