为什么我得到pthread_mutexattr_settype的未定义引用?

时间:2014-03-04 18:47:30

标签: c linux gcc shared-libraries linker-errors

我正在尝试编译和链接来自Novell LDAP C SDK的示例文件,但由于pthread库中有3个未解析的符号,链接失败。我在Ubuntu 13.10(Saucy)上使用gcc 4.8.1。我已将pthread库正确地包含在命令行中的最后一个库中,但这些特定符号仍未解析。

$ 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

2 个答案:

答案 0 :(得分:23)

只是将评论作为答案写下来,因为根据其他用户的反馈,它被证明是有用的。


请将-pthread传递给编译器(不带-l)。另见gcc - significance of -pthread flag when compiling

答案 1 :(得分:8)

在cmake中,您可以添加set( CMAKE_CXX_FLAGS " -pthread " )

不要编辑间距!