QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 822|回复: 1

pthread的一个程序,咋编译不过呢?

[复制链接]
发表于 2005-6-13 11:22:33 | 显示全部楼层 |阅读模式
在《Beginning Linux Programming 3rd》的第481页


  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <string.h>

  6. char message[] = "Hello World";

  7. void *thread_function(void *arg){
  8.         printf("thread_function is running. Argument was %s\n", (char *)arg);
  9.         sleep(3);
  10.         strcpy(message, "Bye!");
  11.         pthread_exit("Thank you for the CPU time");
  12. }

  13. int main(){
  14.         int res;
  15.         pthread_t a_thread;
  16.         void *thread_result;

  17.         res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
  18.         if(0 != res){
  19.                 perror("Thread creation failed");
  20.                 exit(EXIT_FAILURE);
  21.         }

  22.         printf("Waiting for thread to finish...\n");

  23.         res = pthread_join(a_thread, &thread_result);
  24.         if(0 != res){
  25.                 perror("Thread join failed");
  26.                 exit(EXIT_FAILURE);
  27.         }

  28.         printf("Thread joined, it returned %s\n", (char *)thread_result);
  29.         printf("Message[] is now %s\n", message);

  30.         return 0;
  31. }

复制代码


编译输出为


  1. [root@localhost thread]# gcc -Wall thread.c -o thread
  2. /tmp/cca0ZbjQ.o(.text+0x69): In function 'main':
  3. : undefined reference to 'pthread_create'
  4. /tmp/cca0ZbjQ.o(.text+0xae): In function 'main':
  5. : undefined reference to 'pthread_join'
  6. collect2: ld returned 1 exit status
复制代码


咋整啊?
 楼主| 发表于 2005-6-13 11:42:36 | 显示全部楼层
got it

用下边的命令编译
gcc -Wall thread.c -o thread -lpthread
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-5 21:41 , Processed in 0.039890 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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