链接器中不必要的链接库
我有一个项目,我可以从链接器中排除一些库并且仍然可以构建?
就最终产品的性能和内存而言,排除它们是否会更好?
I have a project which I can exclude some of libraries from linker and still builds ?
Is it any better to exclude them in terms of the performance and memory of final product ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个好的 C++ 链接器不会包含来自代码中未使用的任何库的任何调用(所谓的“死代码剥离”)。
所以,我想说这取决于您使用哪种 C++ 链接器来发出最终版本。也许您应该参考链接器文档来获取有关死代码剥离的信息。如果不这样做,那么它肯定有助于减少程序的最终内存占用。
干杯,希望信息有所帮助!
A good c++ linker would not include any calls from any libs that are not used in the code (the so-called ¨dead-code stripping¨).
So, I would say it depends what kind of C++ linker you are using to emit the final release. Maybe you should refer to your linker documentation to get information on dead-code stripping. If it is not doing that, then it would definitely help reducing the final memory footprint of the program.
Cheers, and hope that info helps !
从最终的可执行文件中排除一些未使用的库可能会使启动速度更快并节省少量内存 - 很可能只有标头和库启动代码实际上最终会被加载,并且可以对它们进行分页启动后退出。
但是,不要手动执行此操作。如果您被告知添加该库,则可能是有原因的 - 也许您尚未使用的某些函数调用需要它,稍后如果您使用该函数调用,您可能会忘记它。
大多数链接器都有一个选项可以自动排除未使用的库,因此您可能只想启用该选项以让它为您处理事情。
注意:在极少数情况下,库的启动代码可能会产生一些重要的影响,在这种情况下您不应排除它。最好通过检查库的文档来确定这一点;像这样的事情应该(希望!)被清楚地记录下来。
Excluding some unused libraries from the final executable might make startup a bit faster and save a tiny amount of memory - chances are only the header and library startup code will actually end up being loaded, and these can be paged out after startup.
However, don't do it manually. If you were told to add the library there's probably a reason for it - perhaps some function call you're not using yet requires it, and later on if you use that function call you may have forgotten about it.
Most linkers have an option to exclude unused libraries automatically, so you may want to just enable that option to have it take care of things for you.
Note: In some rare cases, the library's startup code might have some important effect, in which case you should not exclude it. This is something that is best determined by checking the library's documentation; things like this should (hopefully!) be clearly documented.
应该没有什么区别。
任何价值的链接器都不会包含应用程序未(直接或间接)引用的库中的任何内容,即使这些库是在命令行上指定的。
包含(一部分)库的唯一原因是:
- 应用程序使用库中的函数或全局对象
- 包含用于解析某些引用的库的一部分具有对此库的函数或全局对象的引用。
链接器不仅会盲目地将您提供的所有内容放在应用程序中,而且还会区分对象文件(针对应用程序)和库。
链接器首先收集所有目标文件并解析文件之间进行的尽可能多的引用。
之后,链接器遍历指定的库,并从每个库中获取解析(已知)未解析的引用所需的部分。由于库之间的依赖关系,这可能会创建新的未解析的引用。大多数链接器只会对库进行一次传递,但有些链接器可能会执行多次传递来解析所有引用。
解析引用不需要的库部分不包含在可执行文件中。
It should not make any difference.
Any linker of any worth will not include anything from libraries that are not (directly or indirectly) referenced by the application, even if those libraries are specified on the command line.
The only reasons to include (a part of) a library are:
- The application uses a function or global object from the library
- A part of a library that was included to resolve some references has a reference to a function or global object of this library.
A linker does not just blindly put all the things you provide together in an application, but it makes a distinction between object files (for the application) and libraries.
The linker first collects all the object files and resolves as many references that are made between the files.
After that, the linker goes through the specified libraries and takes from each library those parts that are needed to resolve the (known) unresolved references. This may create new unresolved references due to dependencies between the libraries. Most linkers will make only a single pass over the libraries, but some may perform multiple passes to resolve all the references.
Parts of libraries that are not needed to resolve a reference are not included in the executable.
是的,排除不必要的库总是更好。
Yes, it's always better to exclude the unneccessary libraries.