2016-03-01 67 views
0

我正在编译一个程序,其中包含来自pthread库互斥信号量,但是当我编译使用-lpthread标志时,我得到一个未定义的引用错误。没有引用pthread_mutex_lock与-lpthread编译

gcc -lpthread prodcon.c 
/tmp/ccESOlOn.o: In function `producer': 
prodcon.c:(.text+0x2e): undefined reference to `pthead_mutex_lock' 
prodcon.c:(.text+0xd6): undefined reference to `pthead_mutex_unlock' 
collect2: ld returned 1 exit status 

互斥锁的语法如下:

pthread_mutex_t mutex1; 

是一个全球性的声明,以便它可以被多个线程使用。在函数中我打电话互斥像这样:

pthead_mutex_lock(&mutex1); 
pthead_mutex_unlock(&mutex1); 

但我得到的编译器错误,我也试图与-pthread标志

gcc -pthread prodcon.c 
/tmp/cc6wiQPR.o: In function `producer': 
prodcon.c:(.text+0x2e): undefined reference to `pthead_mutex_lock' 
prodcon.c:(.text+0xd6): undefined reference to `pthead_mutex_unlock' 
collect2: ld returned 1 exit status 

...我已经寻找答案,但我在编译如果我在包含互斥锁的库中进行链接时,我会很感激它为什么会有一个未定义的引用。

回答

1

订购事宜,所以使用:

gcc prodcon.c -lpthread 

或者更好:

gcc -Wall -Wextra -pedantic -pthread prodcon.c -lpthread 

你确定你使用0​​?或者这是一个错字?反正它应该是pthread_mutex_lock。相同的pthread_mutex_unlock

+0

这是一个错字,但像这样的编译似乎已经做到了,尽管我确信我尝试过我必须在某处发生错误。谢谢! – aastorms