静态链接时,链接器是否包含整个库? [英] When linking statically, does the linker include the whole library?

查看:177
本文介绍了静态链接时,链接器是否包含整个库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我静态链接到freeglut,编译器是否包括从freeglut或我使用的部分的一切?当然,这意味着链接器(或编译器?)做某种依赖性分析,以确定它可以安全地排除。

For example, if I static link to freeglut, does the compiler include everything from freeglut or only the parts that I use? Of course, this implies that the linker (or compiler?) do some kind of dependency analysis to figure out what it can safely exclude.

如果是这样,看看在Visual Studio中包含或排除了什么?

If so, is there a way to see what have been included or excluded in Visual Studio?

推荐答案

这部分是执行质量问题,

It's partly a Quality of Implementation issue, but there is a real gotcha.

也就是说,根据标准,链接器必须在引用的所有编译单元中添加 。但是在库中,你有一个编译单元,只有一个静态变量,它的初始化注册一个东西与某个注册表,例如。消息处理,工厂,任何,或者它的构造函数和析构函数输出分别在main之前和after main。如果在编译单元中没有被引用,那么链接器在它的权限之内,只需跳过它,删除它。

Namely, by the standard the linker has to add in all compilation units that are referenced. But say that in the library, you have a compilation unit with nothing but a static variable whose initialization registers something with a something registry, e.g. message handling, factory, whatever, or perhaps its constructor and destructor output, respectively, "before main" and "after main". If nothing in that compilation unit is referenced, then the linker is within its rights to just skip it, remove it.

因此,为了确保这样的静态变量不是优化的使用一个符合标准的工具链,在编译单元中引用某些东西是非常必要和足够的。

So, to ensure that such static variables are not optimized away, with a standard-conforming toolchain it is necessary and sufficient to reference something in that compilation unit.

在Visual Studio中查看已经包含的内容,知道没有办法,除了要求链接器的详细输出,像链接器选项 / verbose:ref

Re seeing in Visual Studio what has been included, as far as I know there's no way except asking for verbose output from the linker, like, linker option /verbose:ref.

另一种方法是请求链接器提供一个映射文件,如:linker option

An alternative is to ask the linker for a map file, like, linker option /map:blah.

这个输出也很详细。

这篇关于静态链接时,链接器是否包含整个库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆