2017-01-09 114 views
3

我試圖使用g ++靜態庫(staticLib.a)鏈接到使用動態庫(dynamicLib.so):克++鏈接靜態庫成一個動態庫(不-fPIC)

g++ *.o -Wl,--whole-archive staticLib.a -Wl,--no-whole-archive -shared -o dynamicLib.so 

而我得到了相同的錯誤here

/usr/bin/ld: staticLib.a(object.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC staticLib.a(object.o): error adding symbols: Bad value collect2: error: ld returned 1 exit status

我看了幾個主題,但我無法找到我要找的答案。 staticLib.a未被編譯爲位置無關代碼(PIC)。根據link above,這似乎是強制性的。但是,staticLib.a是另一個我無法控制的項目的庫。

我的第一個想法是提取對象*.o using ar -x(如解釋in this second link)。但問題依然如此,因爲該對象未編譯爲-fPIC

我的第二個想法是創建我自己的Makefile以在我的項目中使用-fPIC重新編譯staticLib.a(我不想搞亂現有的項目)。但我不知道這是一個好辦法...

所以我的問題是以下幾點:有沒有任何可能的方式鏈接靜態庫(編譯沒有-fPIC)到一個動態的?

相關主題:

回答

2

So my question is the following: Is there any possible way to link a static library (compiled without -fPIC) into a dynamic one ?

作爲提供與位置無關的代碼需要編譯沒有實際這是不可能改變已經編譯的代碼。從理論上說,你可能會從二進制源代碼中逆向工程並重新編譯,但這將是完全無效的解決方案。所以你必須(重新)編譯通過-fPIC的原始項目。

+0

這是我害怕。謝謝回覆。 – Nicolas