LinuxSir.cn,穿越时空的Linuxsir!

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

程序链接时出错了,不知为什么

[复制链接]
发表于 2007-5-6 20:31:44 | 显示全部楼层 |阅读模式
程序如下:只是创建两个线程。
#include <stdio.h>

#include <pthread.h>
#include <stdlib.h>

//线程一

void thread1(void)

{

        int i=0;

        for(i=0;i<6;i++){

                printf("This is a pthread1.\n");

                if(i==2)

                        pthread_exit(0);

                sleep(1);

                }

}

//线程二

void thread2(void)

{

        int i;

        for(i=0;i<3;i++)

                printf("This is a pthread2.\n");

        pthread_exit(0);

}



int main(void)

{

        pthread_t id1,id2;

        int i,ret;

        ret=pthread_create(&id1,NULL,(void *)thread1,NULL);

        if(ret!=0){

                printf ("Create pthread error!\n");

        exit (1);

        }

        ret=pthread_create(&id2,NULL,(void *)thread2,NULL);

        if(ret!=0){

                printf ("Create pthread error!\n");

                exit (1);

        }

        pthread_join(id1,NULL);

        pthread_join(id2,NULL);

        exit (0);

}

编译后错误如下:
/tmp/ccYy89hh.o: In function `main':
thread.c.text+0xae): undefined reference to `pthread_create'
thread.c.text+0xf3): undefined reference to `pthread_create'
thread.c.text+0x127): undefined reference to `pthread_join'
thread.c.text+0x13a): undefined reference to `pthread_join'

好像是说没有给那两个函数定义参数,编译,汇编都没错,就是链接时出现这个错误。
发表于 2007-5-6 21:20:51 | 显示全部楼层
加-lpthread
回复 支持 反对

使用道具 举报

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

本版积分规则

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