LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 504|回复: 2

编译这个线程的程序为什么出错?

[复制链接]
发表于 2004-8-2 12:05:02 | 显示全部楼层 |阅读模式
/tmp/ccGycMxS.o(.text+0x21): In function `main':
: undefined reference to `pthread_create'
/tmp/ccGycMxS.o(.text+0x5c): In function `main':
: undefined reference to `pthread_create'
/tmp/ccGycMxS.o(.text+0x8f): In function `main':
: undefined reference to `pthread_join'
/tmp/ccGycMxS.o(.text+0x9f): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>

  4. void task1(int *counter);
  5. void task2(int *counter);
  6. void cleanup(int counter1, int counter2);

  7. int g1 = 0;
  8. int g2 = 0;

  9. int main(int argc, char *argv[])
  10. {
  11.         pthread_t thrd1, thrd2;
  12.         int ret;

  13.         ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);
  14.         if (ret) {
  15.                 perror("pthread_create: task1");
  16.                 exit(EXIT_FAILURE);
  17.         }
  18.         ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
  19.         if (ret) {
  20.                 perror("pthread_create: task2");
  21.                 exit(EXIT_FAILURE);
  22.         }

  23.         pthread_join(thrd2, NULL);
  24.         pthread_join(thrd1, NULL);

  25.         cleanup(g1, g2);

  26.         exit(EXIT_SUCCESS);
  27. }

  28. void task1(int *counter)
  29. {
  30.         while (*counter < 5) {
  31.                 printf("task1 count: %d\n", *counter);
  32.                 (*counter)++;
  33.                 sleep(1);
  34.         }
  35. }

  36. void task2(int *counter)
  37. {
  38.         while (*counter < 5) {
  39.                 printf("task2 count: %d\n", *counter);
  40.                 (*counter)++;
  41.         }
  42. }

  43. void cleanup(int counter1, int counter2)
  44. {
  45.         printf("total iterations: %d\n", counter1 + counter2);
  46. }
复制代码
发表于 2004-8-2 12:15:38 | 显示全部楼层
加上-lpthread试试
 楼主| 发表于 2004-8-2 12:18:44 | 显示全部楼层
thanks!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表