【发布时间】:2016-06-13 11:07:39
【问题描述】:
我正在编译一个包含来自 pthread 库的互斥信号量的程序,但是当我使用 -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
我已经搜索了答案,但不知所措,如果我在包含互斥锁的库中进行链接,我将不胜感激找出为什么它具有未定义的引用。
【问题讨论】:
标签: c pthreads mutex linker-errors undefined-reference