- 论坛徽章:
- 0
|
从Stevens大师UNIX Network Programming, Volume 2 IPC 一书中拷贝了一个多线程的程序代码,编译出错如下:
xlc -o ../prog/prodcons2 ../objs/prodcons2.o ../objs/error.o
ld: 0711-317 ERROR: Undefined symbol: .pthread_create
ld: 0711-317 ERROR: Undefined symbol: .pthread_join
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_lock
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_unlock
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_unlocak
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
make: 1254-004 The error code from the last command is 8.
Stop.
实际上pthread_create, pthread_join, pthread_mutex_lock这几个函数在头文件pthread.h中都已经有了,man pthread_create有如下提示:
Note: The pthread.h header file must be the first included file of each source file using the threads library. Otherwise, the -D_THREAD_SAFE compilation flag should be used, or the cc_r compiler used. In this case, the flag is automatically set.
故我在prodcons2.c中的第一行#include <pthread.h>;
但编译还是报线程操作的几个函数没有声名。
请教各位高手,这是什么原因?
附:源代码如下:
/* prodcons2.c */
#include <pthread.h>;
#include "unpipc.h"
#define MAXNITEMS 1000000
#define MAXNTHREADS 100
int nitems; /* read-only by producer and consumer */
struct{
pthread_mutex_t mutex;
int buff[MAXNITEMS];
int nput;
int nval;
} shared = {
PTHREAD_MUTEX_INITIALIZER
};
void *produce(void *), *consume(void *);
int
main(int argc, char ** argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if(argc != 3)
err_quit("usage: prodcons2 <#items>; <#threads>;" ;
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);
set_concurrency(nthreads);
/* start all the producer threads */
for(i = 0; i< nthreads; i++)
{
count=0;
pthread_create(&tid_produce, NULL, produce, &count);
}
for(i=0;i<nthreads; i++)
{
pthread_join(tid_produce, NULL);
printf("count[%d] = %d\n", i, count);
}
pthread_create(&tid_consume, NULL, consume, NULL);
pthread_join(tid_consume, NULL);
exit(0);
}
void *
produce(void *arg)
{
for(;
{
pthread_mutex_lock(&shared.mutex);
if(shared.nput >;= nitems){
pthread_mutex_unlock(&shared.mutex);
return(NULL);
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
Pthread_mutex_unlocak(shared.mutex);
*((int *)arg)+=1;
}
}
void *
consume(void *arg)
{
int i;
for(i=0;i<nitems; i++)
{
if(shared.buff!= i)
printf("buff[%d]=%d\n", i, shared.buff);
}
return(NULL);
} |
|