Linux下編譯一個靜態連結的程式的注意點

來源:互聯網
上載者:User
1. ld的-static開關加上之後,就可以編譯出一個靜態連結的程式了。要注意該程式用到的所有的庫,都要有相應的.a版本,編譯的時候作為一個source file放到命令列中即可。光有庫的.so沒用,需要重新編譯該庫,讓它產生.a檔案。
2. 要注意ld的一個特性:當ld尋找一個symbol的時候,ld只會往後尋找,不會往前尋找。比如程式以來兩個庫A和B,而A庫又調用了B庫中的函數,那 麼編譯的時候,在命令列上,A庫檔案要出現在B庫檔案的前面。當ld發現A庫中某個函數找不到時,他就會往後尋找,於是在B庫中找到了這個函數,OK。如 果AB兩個庫互相依賴,那麼一般命令列中要寫成A B A或B A B這樣的格式。這就是為什麼也會經常看到命令列中有重複出現相同庫的原因。比如A B A這種情況,當ld發現A中有個函數undefined時,往後尋找,在B中找到了,OK;繼續在B中發現一個函數undefined,也往後找,在A中 找到了該函數,然後也OK了。
3. 當不知道哪個函數在哪個.a中的時候,用objdump -t <filepath>|grep xxx來尋找即可。注意看objdump的manual,不是grep出了東西就一定表示該庫中有這個函數,還要看前面的flag。

還有一個非常重要的內容:在一個靜態連結的程式中,不能出現dlopen函數調用,看下面:

The dlopen/() function is available only to a dynamically-linked process. A statically-linked process (one where libc is linked statically) can't call dlopen() because a statically-linked executable:

* doesn't export any of its symbols
* can't export the required structure for libraries to link against
* can't fill structures at startup needed to load subsequent shared objects.

 

 看看此文是不是一個情況:http://www.cnblogs.com/super119/archive/2011/03/06/1972333.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.