2016-05-31 65 views
1

我遇到了一些項目問題。似乎我鏈接到一個需要pthread符號的庫,但是庫本身沒有與pthread鏈接。症狀是,當我嘗試運行程序時,程序會出現段錯誤。我跑了:使用G ++強制鏈接到pthread

LD_DEBUG=all ./my_program 

並發現它seg段,而尋找pthread_create。

LD_PRELOAD=/lib/x86_64-linux-gnu/libpthread.so.0 ./my_program 

爲了解決這個問題,我想我只是在並行線程構建腳本使用-lpthread鏈接MY-:該計劃,如果我強迫使用此命令並行線程的運行預緊力。這沒有用,所以我嘗試了-pthread。也沒有工作。我能夠重現這對裸最小:

echo "int main() { return 0; }" | g++ -x c++ - -pthread -lpthread -o my_program && ldd my_program 
linux-vdso.so.1 => (0x00007ffe4fd08000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f150ca5c000) 
/lib64/ld-linux-x86-64.so.2 (0x000055ac8c275000) 

你可以從LDD的輸出看,MY-不與並行線程聯繫。我懷疑發生這種情況是因爲g ++理解我實際上沒有使用pthread中的任何符號,也沒有與它聯繫。於是,我就加入了呼籲在pthread_create:

echo -e "#include <pthread.h>\n int main() { pthread_create(0,0,0,0); return 0; }" | g++ -x c++ - -pthread -o my_program && ldd my_program 
<stdin>: In function ‘int main()’: 
<stdin>:2:37: warning: null argument where non-null required (argument 1) [-Wnonnull] 
<stdin>:2:37: warning: null argument where non-null required (argument 3) [-Wnonnull] 
linux-vdso.so.1 => (0x00007ffd977f9000) 
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fafe1bb6000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fafe17f1000) 
/lib64/ld-linux-x86-64.so.2 (0x0000563ee0002000) 

請忽略這些警告,我知道電話給pthread_create是僞造的。關鍵是現在它實際上與pthread鏈接。

所以我的問題是:無論如何強迫g ++鏈接的東西(在這種情況下pthread)沒有添加虛擬代碼使用符號?

+0

嘗試輪候冊, - 沒有讓 - SHLIB-不確定-lbrokenlib -pthread。 –

+0

另外-Wl, - undefiled = pthread_create。 –

+0

感謝您的幫助,但這些工作都不幸, – bofjas

回答

1

有沒有辦法強制g ++鏈接的東西(在這種情況下pthread)沒有添加虛擬代碼使用符號?

試試這個:

echo "int main() { return 0; }" | 
g++ -x c++ - -o my_program -Wl,--no-as-needed -lpthread -Wl,--as-needed 
+0

解決了問題。謝謝!在我發佈這個問題後,我嘗試了使用gcc而不是g ++的完全相同的東西,並且沒有使用--no-as-needed標誌。你有什麼想法,爲什麼? – bofjas