pthread接口遇到的问题:undefined reference to `pthread_create'

软件和网站开发以及相关技术探讨
回复
xmqdtc
帖子: 7
注册时间: 2006-11-10 18:17

pthread接口遇到的问题:undefined reference to `pthread_create'

#1

帖子 xmqdtc » 2008-05-25 9:29

头文件己包含:#include <pthread.h>
试过了在编译中加入-lpthread
请帮忙看一下怎么了,源码如下:

代码: 全选

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

void task1(int *counter);
void task2(int *counter);
void cheanup(int counter1,int counter2);

int g1 = 0;
int g2 = 0;

int main(int argc,char *argv[])
{
    pthread_t thrd1,thrd2;
    int ret;
/*create the first thread*/
    ret = pthread_create(&thrd1,NULL,(void *)task1,(void *)&g1);
    if(ret){
        perror("pthread_creat: task1");
        exit(EXIT_FAILURE);
    }
/*create the second thread*/
    ret = pthread_create(&thrd2,NULL,(void *)task2,(void *)&g2);
    if(ret){
        perror("pthread_creat: task2");
        exit(EXIT_FAILURE);
    }
    pthread_join(thrd2,NULL);
    pthread_join(thrd1,NULL);

    cleanup(g1,g2);
    exit(EXIT_SUCCESS);
}
void task1(int *counter)
{
    while(*counter < 5){
        printf("task1 cout: %d\n",*counter);
        (*counter)++;
        sleep(1);
    }
}

void task2(int *counter)
{
    while(*counter < 5){
        printf("task2 cout: %d\n",*counter);
        (*counter)++;
        sleep(1);
    }
}

void cleanup(int counter1,int counter2)
{
    printf("total iterations: %d\n",counter1 + counter2);
}
[/code]
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#2

帖子 laborer » 2008-05-25 9:34

有sudo apt-get install build-essential没?

程序有个小问题,第7行应该是cleanup。不过在我这里可以编译。
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
xmqdtc
帖子: 7
注册时间: 2006-11-10 18:17

#3

帖子 xmqdtc » 2008-05-25 9:44

laborer 写了:有sudo apt-get install build-essential没?

程序没有问题,可以在我这里编译。
谢谢你。我发现问题了。
build-essential最新版,因为我用make thrcreate.c来编译,出错了,用gcc thrcreate.c -o thrcreate -lpthread就可以了。还有上面打错了一个字符,cleanup声明时写错了写成cheanup
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#4

帖子 laborer » 2008-05-25 9:50

呵呵,你引用恢复那个键按得很快啊。

用make的话要写Makefile
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
回复