2013-09-30 58 views
2

通常,通過使用KEEP(),即使符號未被引用,ld也會保留該節中的符號。但是,這不是我的經驗。如果沒有引用,我不能創建保留符號的ld linkerscript。爲什麼ld的KEEP()不保留我的符號?

這是否有一些先決條件的工作?

回答

1

KEEP 確實保留我的符號,但提交的檔案被事先剝去了所有被視爲不必要的對象文件。爲了防止這種情況,必須在鏈接命令中使用選項--whole-archive

ld手冊頁:

--whole-archive 
     For each archive mentioned on the command line after the 
     --whole-archive option, include every object file in the archive in 
     the link, rather than searching the archive for the required object 
     files. This is normally used to turn an archive file into a shared 
     library, forcing every object to be included in the resulting 
     shared library. This option may be used more than once. 

     Two notes when using this option from gcc: First, gcc doesn't know 
     about this option, so you have to use -Wl,-whole-archive. Second, 
     don't forget to use -Wl,-no-whole-archive after your list of 
     archives, because gcc will add its own list of archives to your 
     link and you may not want this flag to affect those as well. 
相關問題