在Linux平臺編譯iLog3動態鏈接庫時遇到的問題:`tls_g' can not be used when making a shared object

在Linux平臺編譯iLog3動態鏈接庫時遇到的問題:`tls_g' can not be used when making a shared object

環境

在 Visual Studio 2015 上使用 Visual C++ for Linux 插件在遠程 Linux 主機上編譯和調試 C 程序。其中,日誌庫我打算採用 iLog3( Github上的 calvinwilliams/iLOG3 ),打算編譯成動態鏈接庫(Shared Library)供其它程序調用。

問題

建立 Cross Platform 的 Linux shared library 工程。此處並無特殊之處,按下不表。
但是在編譯時,遇到了一些鏈接錯誤:

1>/usr/bin/ld : error : relocation R_X86_64_TPOFF32 against symbol `tls_g’ can not be used when making a shared object; recompile with -fPIC

1>/usr/bin/ld : error : relocation R_X86_64_TPOFF32 against symbol `_g_logc_pid’ can not be used when making a shared object; recompile with -fPIC

1>/usr/bin/ld : error : relocation R_X86_64_TPOFF32 against symbol `tls_gs’ can not be used when making a shared object; recompile with -fPIC

在這裏插入圖片描述

檢索

通過搜索 gcc 的 -fPIC 編譯選項,在 stackoverflow 上面看到了這個有價值的帖子:

GCC -fPIC option
https://stackoverflow.com/questions/5311515/gcc-fpic-option
閱讀帖子發現 -fPIC 的含義就是 Generate position-independent code 的意思。

解決問題

在 Visual C++ 的 Code Generation 這一項目屬性中,果然見到了 -fPIC 這個選項:

在這裏插入圖片描述按照以上截圖更改項目屬性支持 -fPIC 編譯選項之後,果然解決了這個編譯鏈接的問題:

在這裏插入圖片描述如以上截圖所示,編譯成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章