Gcc 为什么ld';s KEEP()不保留我的符号?

Gcc 为什么ld';s KEEP()不保留我的符号?,gcc,ld,linker-scripts,Gcc,Ld,Linker Scripts,通常,通过使用KEEP(),即使未引用符号,ld也会在节中保留符号。然而,这不是我的经验。如果未引用符号,则无法创建保留符号的ld linkerscript 是否有一些先决条件使其起作用?保留确实保留了我的符号,但呈现的存档文件事先已被剥夺了所有被认为不必要的目标文件。为防止出现这种情况,必须在link命令中使用选项--whole archive 从ld的手册页: --whole-archive For each archive mentioned on the comman

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

是否有一些先决条件使其起作用?

保留确实保留了我的符号,但呈现的存档文件事先已被剥夺了所有被认为不必要的目标文件。为防止出现这种情况,必须在link命令中使用选项
--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.