天天看點

轉:關于GCC中同時使用動态和靜态庫連結的操作參數和解釋

來自:http://blog.sina.com.cn/s/blog_4cb133e5010009zx.html

    在我們開發的一個系統中,由于動态連結其中的一個動态庫時,編譯時沒有問題,而運作時不能進行,如果将該庫靜态連接配接時,運作卻沒有問題。具體什麼原因,一直沒有搞清楚,權且當作暫時的解決辦法。

   如何同時 同時使用動态和靜态庫連結,同僚周楠提供了一個參數的用法,在GCC指令參數中具體參數如下:     -Wl,-Bstatic -L/usr/local/sqlite-arm-linux/.libs -lsqlite -Wl,-Bdynamic -L/usr/local/arm/3.3.2/lib    具體用途解釋:sqlite庫靜态連接配接,其它庫動态連接配接。 -Wl,-Bstatic 與-Wl,-Bdynamic參數,從字面意義上可以了解,有靜态和動态的意思,但是具體的真正規則在查找了GCC的原版手冊上有說明。   原文:

Note - if the linker is being invoked indirectly, via a compiler driver (eg

gcc

) then all the linker command line options should be prefixed by

-Wl,

(or whatever is appropriate for the particular compiler driver) like this:

gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup      

This is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link.

實際上主要針對隐式應用LINKER的參數,用“-Wl,”來辨別,,“--startgroup foo.o bar.o -Wl,--endgroup”表示一組,,-Bstatic -Bdynamic 作為關鍵字與-WL,不可分,在GCC連接配接庫時,預設連結是動态連結,現在用上面的指令限制在連結sqlite庫時采用靜态連結。

-Bstatic 還有三個寫法:

-dn和

-non_shared 和

-static

-Bdynamic 還有兩個寫法:

-dy

-call_shared

上面參數“-L/usr/local/sqlite-arm-linux/.libs ”放不放在-Wl,...之間無所謂,因為它隻是提供了sqlite動靜态庫的位置。可以改成下面的參數形式,更直覺。

-L/usr/local/sqlite-arm-linux/.libs -L/usr/local/arm/3.3.2/lib -Wl,-dn -lsqlite -Wl,-dy  

-Wl,-dn 和 -Wl,-dy成對出現才能起到标題所說的作用。  

關于-Wl,後面的參數還有很多,全部明白我也不能。

還有一個問題值得注意,在-Wl,後面不能有空格,否則會出錯!

關于-Wl,option 說明還有一段說明

GCC指令參數的英文原文

-Wl,option

Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.

傳遞參數option作為linker的一個參數,如果option包含逗号,将在逗号處分割成幾個參數。

例如:

-Wl,-dn –lsqlite

-dn 開始靜态連結

-lsqlite 靜态連結sqlite庫

靜态連結完後,然後需要動态連結

-Wl,-dy

重新開始動态連結。