Why am I getting undefined reference to pthread_mutexattr_settype?

Bob picture Bob · Mar 4, 2014 · Viewed 25.2k times · Source

I am trying to compile and link a sample file from the Novell LDAP C SDK but the link is failing due to 3 unresolved symbols in the pthread library. I am using gcc 4.8.1 on Ubuntu 13.10 (Saucy). I have properly included the pthread library as the last library on the command line, but these particular symbols are still not being resolved.

$ gcc -Wl,-trace-symbol=pthread_mutexattr_settype -Wl,-trace-symbol=pthread_mutexattr_init   -Wl,-trace-symbol=pthread_mutex_trylock  -o search search.o -L../lib -lldapsdk -lpthread

../lib/libldapsdk.so: reference to pthread_mutexattr_settype<br>
../lib/libldapsdk.so: reference to pthread_mutexattr_init<br>
../lib/libldapsdk.so: reference to pthread_mutex_trylock<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutexattr_settype<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutexattr_init<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutex_trylock<br>
../lib/libldapsdk.so: undefined reference to pthread_mutexattr_settype<br>
../lib/libldapsdk.so: undefined reference to pthread_mutex_trylock<br>
../lib/libldapsdk.so: undefined reference to pthread_mutexattr_init<br>
collect2: error: ld returned 1 exit status

$ readelf --all  /lib/i386-linux-gnu/libpthread.so.0|grep pthread_mutexattr_settype<br>
   114: 00009d90    36 FUNC    GLOBAL DEFAULT   13 pthread_mutexattr_settype@@GLIBC_2.1<br>
   493: 00009d90    36 FUNC    GLOBAL DEFAULT   13 pthread_mutexattr_settype

Answer

Ali picture Ali · May 5, 2014

Just writing up the comment as an answer as it proved to be helpful based on other users' feedback.


Please pass -pthread to the compiler (without the -l). See also gcc - significance of -pthread flag when compiling.