Chinaunix

标题: 关于编译多线程遇到的问题,请大虾们指点一二。 [打印本页]

作者: rune_zhang    时间: 2005-11-10 15:44
标题: 关于编译多线程遇到的问题,请大虾们指点一二。
小弟在多线程中用了下列函数(文件名:pthread.c)
make_new_item();
pthread_delay_np();
consume_item();
在编译的时候出了问题,希望各位大虾指点一二。
源代码如下:
#include <stdio.h>
#include <pthread.h>

void reader_function(void);
void writer_function(void);
char buffer;
int buffer_has_item=0;
pthread_mutex_t mutex;
struct timespec delay;
main()
{
        pthread_t reader;
        delay.tv_sec=2;
        delay.tv_nsec=0;
        pthread_mutex_init(&mutex,NULL);
        pthread_create(&reader,NULL,(void *)&reader_function,NULL);
        writer_function();
}

void writer_function(void)
{
        while(1){
                pthread_mutex_lock(&mutex);
                if(buffer_has_item==0){
                        buffer=make_new_item();
                        buffer_has_item=1;
                }
                pthread_mutex_unlock(&mutex);
                pthread_delay_np(&delay);
        }
}
void reader_function(void)
{
        while(1){
                pthread_mutex_lock(&mutex);
                if(buffer_has_item==1){
                        consume_item(buffer);
                        buffer_has_item=0;
                }
                pthread_mutex_unlock(&mutex);
                pthread_delay_np(&delay);
        }
}

编译方式如下:
gcc -lpthread -o pthread pthread.c
编译结果如下:
/tmp/ccqNNbHD.o(.text+0x73): In function `writer_function':
: undefined reference to `make_new_item'
/tmp/ccqNNbHD.o(.text+0x9f): In function `writer_function':
: undefined reference to `pthread_delay_np'
/tmp/ccqNNbHD.o(.text+0xd4): In function `reader_function':
: undefined reference to `consume_item'
/tmp/ccqNNbHD.o(.text+0xfe): In function `reader_function':
: undefined reference to `pthread_delay_np'
collect2: ld returned 1 exit status
作者: hyq_111    时间: 2005-11-10 19:51
你试试gcc -o pthread pthread.c  -lpthread
或许能行
作者: renstone921    时间: 2005-11-11 00:18
你的程序里面的读者和写着的函数在那里定义的?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2