即使使用-lpthread和-ltr,也会对sem_init和其他此类函数进行未定义的引用

时间:2014-11-18 14:06:53

标签: c pthreads semaphore

这是我在linux中用于读写器问题的代码。 我看到了一些相同的先前问题,他们的解决方案是使用-lpthread或-ltr,我尝试了这两个单独和一起,但我仍然持续得到相同的错误。请帮忙。

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

sem_t rmutex,wmutex,canwrite;
int readcount=0;

void *reader1(int i)
{
printf("Reader %d wants to read\n",i);
sem_wait(&rmutex);
readcount++; 
sem_post(&rmutex); 
printf("Reader %d is reading",i);

sem_wait(&rmutex);
readcount--;

if(readcount==0) 
sem_post(&canwrite);

sem_post(&rmutex);
printf("Reader %d is exiting",i);

}

void *writer1(int i)
{
printf("Writer wants to write\n");
sem_wait(&canwrite);
sem_wait(&wmutex);
printf("Writer is writing\n");
sem_post(&wmutex);
printf("Writer is exiting\n");
}



int main()
{
pthread_t *reader[3],*writer;
int i;
sem_init(&rmutex,0,1);
sem_init(&wmutex,0,1);
sem_init(&canwrite,0,1);


pthread_create(&writer,NULL,writer1,(void *)i);
for(i=0;i<3;i++)
pthread_create(&(reader[i]),NULL,reader1,(void *)i);

sleep(10000);

for(i=0;i<3;i++)
pthread_join(reader[i],NULL);

pthread_join(writer[i],NULL);

return 0;
}

这是我得到的错误

vineet@vineet-TravelMate-P643-M:~/Work$ cc -lpthread rw.c
rw.c: In function ‘main’:
rw.c:49:1: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type     [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’
 rw.c:49:1: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of    type ‘void * (*)(int)’
rw.c:51:1: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’
rw.c:51:1: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of  type ‘void * (*)(int)’
rw.c:56:1: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [enabled by default]
/usr/include/pthread.h:242:12: note: expected ‘pthread_t’ but argument is of type  ‘pthread_t *’
/tmp/ccYZOwcD.o: In function `reader1':
rw.c:(.text+0x22): undefined reference to `sem_wait'
rw.c:(.text+0x3b): undefined reference to `sem_post'
rw.c:(.text+0x5b): undefined reference to `sem_wait'
rw.c:(.text+0x7d): undefined reference to `sem_post'
rw.c:(.text+0x89): undefined reference to `sem_post'
/tmp/ccYZOwcD.o: In function `writer1':
rw.c:(.text+0xbd): undefined reference to `sem_wait'
rw.c:(.text+0xc9): undefined reference to `sem_wait'
rw.c:(.text+0xe1): undefined reference to `sem_post'
/tmp/ccYZOwcD.o: In function `main':
rw.c:(.text+0x115): undefined reference to `sem_init'
rw.c:(.text+0x131): undefined reference to `sem_init'
rw.c:(.text+0x14d): undefined reference to `sem_init'
rw.c:(.text+0x172): undefined reference to `pthread_create'
rw.c:(.text+0x1aa): undefined reference to `pthread_create'
rw.c:(.text+0x1e4): undefined reference to `pthread_join'
rw.c:(.text+0x20f): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

如果您也可以解释警告,那将非常有用。

1 个答案:

答案 0 :(得分:4)

这有一些问题。

首先,您应该只使用pthread_t *(而不是指针),而不是使用pthread_t作为变量类型。这就是警告所指的内容。

其次,您获取这些链接器错误的原因是因为在命令行上指定输入文件和链接器选项的顺序很重要。由于rw.c取决于-lpthread-lpthread选项应 rw.c之后,而不是之前:

cc rw.c -lpthread