简体   繁体   English

从静态库中剥离所有符号是否会阻止链接?

[英]Does stripping all symbols from a static library prevent linking?

As far as I know, the linker's job is to match undefined symbols in an object file with symbols defined in a static library.据我所知,链接器的工作是将目标文件中未定义的符号与静态库中定义的符号进行匹配。 If found, the proper symbol definition is included into the final executable.如果找到,正确的符号定义将包含在最终的可执行文件中。 This is the so-called symbol resolution.这就是所谓的符号解析。

So what happens if all symbols get stripped away from the static library?那么,如果所有符号都从静态库中剥离,会发生什么? Does it prevent the linker to resolve symbols correctly?它是否会阻止链接器正确解析符号?

Stripping symbols is an operation that's normally meant to remove debug information on symbols, not symbols needed for the actual linking process (though it can be used to remove symbols for things that you don't want used in the linking process if, for example, you didn't generate the object file).剥离符号是一种操作,通常用于删除符号的调试信息,而不是实际链接过程所需的符号(尽管它可用于删除链接过程中不希望使用的符号,例如,您没有生成目标文件)。

It would be a rather bad use of the tool were it to render libraries or object file useless for linking.如果该工具使库或目标文件无法用于链接,那将是一种相当糟糕的使用。

Don't get me wrong, you can actually do this with, for example, strip --strip-all , it's just not a sensible thing to do.不要误会我的意思,您实际上可以使用例如strip --strip-all做到这一点,这只是不明智的做法。 For example, consider the files prog1.c and prog2.c :例如,考虑文件prog1.cprog2.c

// prog1.c
int fn(void);
int main(void) { return fn(); }

// prog2.c
int fn(void) { return 42; }

Running the following commands on these files:对这些文件运行以下命令:

gcc -o prog1.o -c prog1.c
gcc -o prog2.o -c prog2.c
strip --strip-all prog2.o
gcc -o prog prog1.o prog2.o

will result in a linker error because it cannot find fn() - it's been stripped from prog2.o .将导致链接器错误,因为它找不到fn() - 它已从prog2.o However, executing the same commands except for the strip will work just fine.但是,执行除strip之外的相同命令也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM