18

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
Ali
  • 56,466
  • 29
  • 168
  • 265
Bob
  • 320
  • 1
  • 3
  • 9
  • 6
    Please try passing `-pthread` to the compiler (without the `-l`). See also [gcc - significance of -pthread flag when compiling](http://stackoverflow.com/q/2127797/341970). – Ali Mar 04 '14 at 19:15
  • OK. Glad to hear it helped! – Ali Mar 04 '14 at 19:20
  • @ali: You should make this an answer. – alk May 05 '14 at 13:19
  • 1
    Ali added his comment as an answer, so might like to accept it as it seems to answer your question. – alk May 05 '14 at 14:04

2 Answers2

31

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.

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • Why? What's the difference between `-pthread` and `-lpthread`? Thanks for the solution though, it worked for me as well. – TheNotMe May 28 '16 at 11:57
18

In cmake you can addset( CMAKE_CXX_FLAGS " -pthread " )

Do not edit spacing!

John
  • 181
  • 1
  • 7