未定义引用'__gthrw ___ pthread_key_create(unsigned int *,void(*)(void *))

时间:2014-12-02 19:45:47

标签: c++11 gcc opensuse gcc4.8

我正在使用64位gcc-4.8.2生成32位目标,而我的机器是64位。我正在使用c ++ 11并发功能,如thread,mutex,conditiona_variables等。

尝试链接可执行文件时,链接器给出了上述错误消息。 libMyLib也是该项目的一部分。

libMyLib.so: undefined reference to '__gthrw___pthread_key_create(unsigned int*, void (*)(void*))

nm libMyLib.so | grep pthread_key_create显示:

U _ZL28__gthrw___pthread_key_createPjPFvPvE
w __pthread_key_create@@GLIBC_2.0

符号'ghtrw___pthread_key_create'来自哪里?我尝试添加'-lpthread(-pthread)'作为编译器标志,但它没有帮助。

更多信息。 nm libMyLib.so | grep pthread显示其他符号,例如_ZL20__gthread_mutex_lockP15pthread_mutex_t已定义

2 个答案:

答案 0 :(得分:4)

  

其中是符号' ghtrw ___ pthread_key_create'从?

它在GCC" gthreads"中定义。 gthr-posix.h标题中的线程基元的抽象层。

#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
# ifndef __gthrw_pragma
#  define __gthrw_pragma(pragma)
# endif
# define __gthrw2(name,name2,type) \
  static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
  __gthrw_pragma(weak type)
# define __gthrw_(name) __gthrw_ ## name
#else
# define __gthrw2(name,name2,type)
# define __gthrw_(name) name
#endif

/* Typically, __gthrw_foo is a weak reference to symbol foo.  */
#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)

...

#ifdef __GLIBC__
__gthrw2(__gthrw_(__pthread_key_create),
     __pthread_key_create,
     pthread_key_create)

预扩展后,扩展为:

static __typeof(pthread_key_create) __gthrw___pthread_key_create __attribute__ ((__weakref__("__pthread_key_create")));

它应该是对__pthread_key_create的弱引用,因此它永远不应该有定义,因为它只是对glibc的内部__pthread_key_create符号的引用。

所以看起来你构建图书馆的方式出了问题。你不应该有一个未定义的弱符号。

答案 1 :(得分:1)

我最近偶然发现了这个错误,而不是因为缺少-pthread

情况发生在一个不寻常的设置中:我正在编译一个用Centos7下的C ++ 14编写的软件。由于C ++ 14需要最新版本的GCC,我依靠devtoolset 6来实现它。

鉴于具体的设置,我在Centos邮件列表上打开了一个帖子,所以我直接引用相关的线程。请参阅https://lists.centos.org/pipermail/centos-devel/2018-May/016701.htmlhttps://lists.centos.org/pipermail/centos-devel/2018-June/016727.html

简而言之,它可能是由预处理器宏中的一些错误引起的,无论是glibc还是libgcc。可以通过将#include <thread>放在源代码的开头来修复,这会在编译后产生问题。是的,即使您没有使用std::thread

我并不认为这适用于所有人,但它可能会在某些特定情况下发挥作用。

相关问题