【问题标题】:no reference to pthread_mutex_lock with -lpthread compiled没有参考 pthread_mutex_lock 编译 -lpthread
【发布时间】: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


    【解决方案1】:

    顺序很重要,所以使用:

    gcc prodcon.c -lpthread 
    

    或者更好:

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

    你确定你使用pthead_mutex_lock吗?或者这是一个错字?无论如何它应该是pthread_mutex_lockpthread_mutex_unlock 也一样。

    【讨论】:

    • 这是一个错字,但是像这样编译似乎已经完成了,尽管我确信我尝试过我一定在某个地方犯了错误。谢谢!
    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多