我正在尝试编译和链接来自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
答案 0 :(得分:23)
只是将评论作为答案写下来,因为根据其他用户的反馈,它被证明是有用的。
请将-pthread
传递给编译器(不带-l
)。另见gcc - significance of -pthread flag when compiling。
答案 1 :(得分:8)
在cmake中,您可以添加set( CMAKE_CXX_FLAGS " -pthread " )
不要编辑间距!