2014-12-02 164 views
1

我使用的64位的gcc-4.8.2,以產生一個32位的目標,以及我的機器是64位的。我使用C++ 11併發功能,如線程,互斥,conditiona_variables等未定義參照「__gthrw ___ pthread_key_create(無符號整數*,無效(*)(無效*))

鏈接器試圖將可執行鏈接時給出了上面的錯誤消息。 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 [email protected]@GLIBC_2.0 

其中是符號 'ghtrw___pthread_key_create'?我嘗試添加'-lpthread(-pthread)'作爲編譯器標誌,但它沒有幫助。

更多信息。 nm libMyLib.so | grep的並行線程示出了其它符號如_ZL20__gthread_mutex_lockP15pthread_mutex_t被定義

+0

'我嘗試添加「-lpthread」作爲編譯器flag' - 你知道關於[正確順序選項(http://stackoverflow.com/questions/18388710/what-is-the-proper-sequence-of-選擇換GCC最重要 - 的 - 即序列)? – Gluttton 2014-12-02 19:53:34

+3

改爲使用'-pthread'編譯器標誌。 – 2014-12-02 19:56:35

+0

添加'-pthread'不能解決我的問題。還有一個問題,缺少符號從哪裏來? – user11869 2014-12-03 18:28:41

回答

2

哪裏是符號「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

上次我看到這個錯誤是因爲沒有定義'__GNUC__'的另一個編譯器(oracle),所以sys/cdefs將'__attribute __(X)'定義爲空(sunCC提供了它自己的sys/cdefs來避免這種情況)。 – 2014-12-12 17:54:13

+0

我檢查了__GNUC__被定義爲 – user11869 2014-12-16 19:45:40

+0

@Jonathan Wakely,我從gcc文檔中找到了這個「如果目標符號只是通過弱引用引用,那麼它將成爲一個弱的未定義符號。」我不太明白這句話的意思。但是,這可能是原因嗎? – user11869 2014-12-16 19:57:59

相關問題