【问题标题】:Why am I getting undefined reference to pthread_mutexattr_settype?为什么我得到对 pthread_mutexattr_settype 的未定义引用?
【发布时间】:2014-04-06 11:36:53
【问题描述】:

我正在尝试编译和链接来自 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

【问题讨论】:

  • 请尝试将-pthread 传递给编译器(不带-l)。另请参阅gcc - significance of -pthread flag when compiling
  • 成功了!谢谢!
  • 好的。很高兴听到它有帮助!
  • @ali:你应该这样回答。
  • 阿里添加了他的评论作为答案,所以可能愿意接受它,因为它似乎回答了你的问题。

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


【解决方案1】:

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


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

【讨论】:

  • 为什么? -pthread-lpthread 有什么区别?不过感谢您的解决方案,它也对我有用。
【解决方案2】:

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

不要编辑间距!

【讨论】:

  • 像魅力一样工作。谢谢:)
  • 就我而言,我使用了 set(CMAKE_C_FLAGS " -pthread ") ... 它解决了我的头痛问题。梅林的胡子!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多