LinuxSir.cn,穿越时空的Linuxsir!

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

有关线程编程的问题!!

[复制链接]
发表于 2004-5-29 09:14:47 | 显示全部楼层 |阅读模式
我想实现一下多线程编程,我的程序如下:
#include<stdio.h>
#include<pthread.h>
void thread(void)
{
  int i;
  for(i=0;i<3;i++)
  printf("This is a pthread.\n");
}

int main(void)
{
  pthread_t id;
  int i,ret;
  ret=pthread_create(&id,NULL,(void *) thread,NULL);
  if(ret!=0)
  {
  printf ("Create pthread error!\n");
  exit (1);
  }
for(i=0;i<3;i++)
   printf("This is the main process.\n");
pthread_join(id,NULL);
return (0);
}
是照着书抄的,为什么会有错呀??
错误如下:
[sumy@sunheart myapp]$ gcc -o example example.c
/tmp/ccsb8FJS.o(.text+0x4c): In function `main':
: undefined reference to `pthread_create'
/tmp/ccsb8FJS.o(.text+0xa5): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
谁能帮我看一下
分析一下每个错误是怎么产生的!!
先谢过了!!
发表于 2004-5-29 09:20:50 | 显示全部楼层
gcc -lpthread
 楼主| 发表于 2004-5-29 09:25:21 | 显示全部楼层

好了,谢谢kj501版主

[sumy@sunheart myapp]$ gcc -o example example.c -lpthread
[sumy@sunheart myapp]$ ./example
This is a pthread.
This is a pthread.
This is a pthread.
This is the main process.
This is the main process.
This is the main process.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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