本文介绍了tcmalloc:当我静态编译时,如何获取我的malloc调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 LD_PRELOAD = / usr / local / lib / libtcmalloc.so 时,我对malloc的所有调用都会成为tcmalloc调用。但是,当我静态链接到libtcmalloc,我发现直接malloc正在调用,除非我仍然使用 LD_PRELOAD 设置。

When I use LD_PRELOAD=/usr/local/lib/libtcmalloc.so, all my calls to malloc become tcmalloc calls. However, when I link statically against libtcmalloc, I find that straight malloc is getting called unless I still use the LD_PRELOAD setting.

那么,我怎么能对tcmalloc进行静态编译,使我的malloc钩到tcmalloc?

So how can I statically compile against tcmalloc in such a way that my mallocs hook into tcmalloc?

注意:


  • 我使用大量的C ++新的等等,所以
    只是#defining malloc to tcmalloc
    无法工作

  • 可能我必须使用
    malloc_hook自己,但我会有
    认为我可以得到tcmalloc为它做
    ,因为它明显是做它
    当链接动态

推荐答案

符号在第一次匹配的基础上解析。您需要确保libtcmalloc.a在之前通过链接器 libc.a搜索。我假设你没有明确链接libc.a,因为你通常不需要这样做。解决方案是指定-nostdlibs,然后按您希望它们被搜索的顺序显式链接所有必需的库。通常为:

Symbols are resolved on a first match basis. You need to make sure that libtcmalloc.a is searched before libc.a by the linker. I assume that you are not explicitly linking libc.a since you do not normally need to do so. The solution is to specify -nostdlibs, and then explicitly link all necessary libraries in the order you want them to be searched. Usually something like:

-nostdlibs -llibtcmalloc -llibm -llibc -llibgcc

另一个可能更简单的解决方案是链接解析tcmalloc而不是静态库所需的对象文件,因为对象文件优先于库在解析符号。

Another solution which may be simpler, is to link the object file(s) needed to resolve tcmalloc rather than the static library, since object files take precedence over libraries in resolving symbols.

这篇关于tcmalloc:当我静态编译时,如何获取我的malloc调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:40