tcamalloc 如何链接到主程序
我想知道 malloc 如何链接到主程序。基本上,我有一个使用多个静态和动态库的程序。我使用选项“-llibName1 -llibName2”将所有这些包含在我的 makefile 中。 TCmalloc 的文档说我们可以通过调用“LD_PRELOAD=/usr/lib64/libtcmalloc.so”来覆盖我们的 malloc。我无法理解 tcamlloc 是如何被调用到所有这些静态和动态库的。另外 tcmalloc 是如何做到的还链接到 STL 库和 C++ 的新建/删除操作? 任何人都可以对此提供任何见解吗?
I want to know how malloc gets linked to the main program.Basically I have a program which uses several static and dynamic libraries.I am including all these in my makefile using option "-llibName1 -llibName2".
The documentation of TCmalloc says that we can override our malloc simply by calling "LD_PRELOAD=/usr/lib64/libtcmalloc.so".I am not able to understand how tcamlloc gets called to the all these static and dynamic libraries.Also how does tcmalloc also gets linked to STL libraries and new/delete operations of C++?
can anyone please give any insights on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“LD_PRELOAD=/usr/lib64/libtcmalloc.so”指示加载程序在解析程序外部的符号时在任何其他共享库之前使用 libtcmalloc.so,因为 libtcmalloc.so 定义了一个名为“malloc”的符号,这就是版本你的程序将会使用。
如果省略 LD_PRELOAD 行,glibc.so(或系统上的任何 C 库)将是第一个定义名为“malloc”的符号的共享库。
另请注意,如果您链接到定义名为“malloc”的符号(并使用正确的参数等)的静态库,或者加载另一个定义名为“malloc”的符号的共享库,您的程序将尝试使用该版本的malloc。
无论如何,这就是一般的想法;实际发生的事情非常有趣,我必须直接告诉你http://en.wikipedia.org /wiki/Dynamic_linker 作为更多信息的起点。
"LD_PRELOAD=/usr/lib64/libtcmalloc.so" directs the loader to use libtcmalloc.so before any other shared library when resolving symbols external to your program, and because libtcmalloc.so defines a symbol named "malloc", that is the verison your program will use.
If you omit the LD_PRELOAD line, glibc.so (or whatever C library you have on your system) will be the first shared library to define a symbol named "malloc".
Note also that if you link against a static library which defines a symbol named "malloc" (and uses proper arguments, etc), or another shared library is loaded that defines a symbol named "malloc", your program will attempt to use that version of malloc.
That's the general idea anyway; the actual goings-on is quite interesting and I will have to direct to you http://en.wikipedia.org/wiki/Dynamic_linker as a starting point for more information.