【问题标题】:undefine reference to tcmalloc public API对 tcmalloc 公共 API 的未定义引用
【发布时间】:2016-02-24 05:13:14
【问题描述】:

我已经克隆了 google-perf git 树。

> ./autogen.sh
> ./configure --enable-frame-pointers --prefix=/usr/
> make
> sudo make install

以上所有步骤均成功。我可以在 /usr/include/gperftools/tcmalloc.h 等中看到头文件

我的程序

 #include <stdio.h>
 #include <gpertools/malloc_extension.h>
 #include <iostream>

 int main()
 {
 const unsigned int tcmalloc_stats_buf_len_ = 32768;
 char tcmalloc_stats_buf[tcmalloc_stats_buf_len_];

 MallocExtension::instance()->GetStats(tcmalloc_stats_buf,
                                      tcmalloc_stats_buf_len_);
 printf("%s ",tcmalloc_stats_buf);
 fflush(stdout);
 }

编译

g++ -ltcmalloc my_prog.c -o my_prog
my_prog.cc: undefine reference to MallocExtension::instance

如果我注释掉 GetStats 行,那么编译工作正常。所以我假设它与 tcmalloc 链接。但是,当我尝试访问 API 时,它给了我一个错误。

可能是什么问题?也许有什么想法?

【问题讨论】:

  • 您能否向我们提供您收到的错误消息?所以我们可以帮助你。

标签: c++ malloc tcmalloc gperftools


【解决方案1】:

来自 Aliaksey Kandratsenka

尝试将 -ltcmalloc 移到末尾。特别是在静态链接模式下, 链接器在查看库对象时 按命令行中给出的顺序处理它,它只提取符号 当时知道需要。这不是广为人知的“陷阱” 关于有时需要指定库的静态链接 两次或多次。

这不应该影响动态链接,但我想我听说过一些 发行版(也许是 ubuntu)的链接器过于有创意 配置(并且需要 -Wl,--no-as-needed 或类似的东西 那)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2017-03-20
    • 1970-01-01
    • 2020-10-15
    • 2023-04-09
    • 2013-06-21
    • 1970-01-01
    相关资源
    最近更新 更多