当静态链接,并链接器包括整个图书馆吗? [英] When linking statically, does the linker include the whole library?

查看:137
本文介绍了当静态链接,并链接器包括整个图书馆吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我静态链接到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.

也就是说,链接器必须在所有添加标准的编译单元的被引用。但是说,在图书馆,你有一个编译单元只是一个静态变量在初始化时有登记的东西,例如一些注册消息处理,工厂什么的,或者它的构造函数和析构函数的输出,分别为主之前和之后主力。如果没有在该编译单元引用,则链接器内的权利,只是跳过它,删除它。

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中已经包含什么看,据我知道有没有办法,除了要求详细的输出连接器一样,连接器选项 /详细: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.

然而,你该选项的真正的详细输出。

However, with that option you get really verbose output.

另一种方法是要求连接器的地图文件一样,链接器选项 /图:废话

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

另外这款输出非常详细,但。

Also this output is very verbose, though.

这篇关于当静态链接,并链接器包括整个图书馆吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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