0

我已经克隆了 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 时,它给了我一个错误。

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

4

1 回答 1

0

从 Aliaksey Kandratsenka

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

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

于 2015-11-21T22:12:45.563 回答