没有参考 pthread_mutex_lock 编译 -lpthread
no reference to pthread_mutex_lock with -lpthread compiled
我正在编译一个包含来自 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
我已经搜索了答案,但不知所措,如果能帮助我在包含互斥锁的库中链接时弄清楚为什么它有未定义的引用,我将不胜感激。
顺序很重要,所以使用:
gcc prodcon.c -lpthread
或更好:
gcc -Wall -Wextra -pedantic -pthread prodcon.c -lpthread
您确定使用pthead_mutex_lock
吗?或者这是一个错字?无论如何它应该是pthread_mutex_lock
。 pthread_mutex_unlock
.
相同
我正在编译一个包含来自 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
我已经搜索了答案,但不知所措,如果能帮助我在包含互斥锁的库中链接时弄清楚为什么它有未定义的引用,我将不胜感激。
顺序很重要,所以使用:
gcc prodcon.c -lpthread
或更好:
gcc -Wall -Wextra -pedantic -pthread prodcon.c -lpthread
您确定使用pthead_mutex_lock
吗?或者这是一个错字?无论如何它应该是pthread_mutex_lock
。 pthread_mutex_unlock
.