简体   繁体   中英

no reference to pthread_mutex_lock with -lpthread compiled

I am compiling a program that contains mutex semaphores from the pthread library but when i compile using the -lpthread flag I am getting an undefined reference error.

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

The syntax for the mutex lock is like so:

pthread_mutex_t mutex1;

is a global declaration so that it can be used by multiple threads. within the functions I am calling the mutex like so:

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

But i am getting the compiler error, I also tried compiling with the -pthread flag

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

I have searched for answers but am at a loss and would appreciate any help with figuring out why it has an undefined reference when I am linking in the library that contains the mutex locks.

Order matters, so use:

gcc prodcon.c -lpthread 

Or better:

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

Are you sure you use pthead_mutex_lock ? Or is this a typo? Anyways it should be pthread_mutex_lock . Same for pthread_mutex_unlock .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM