为什么ld的KEEP()不保留我的符号?

时间:2013-09-30 18:22:09

标签: gcc ld linker-scripts

通常,通过使用KEEP(),即使未引用符号,ld也会在符号中保留符号。但是,这不是我的经验。如果没有引用符号,我就无法创建保留符号的ld linkerscript。

这有什么先决条件吗?

1 个答案:

答案 0 :(得分: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.