我无法在 Ubuntu 11.10 中获得 gcc 以在 google perftools -lprofiler 中正确链接。问题似乎是链接器丢弃了未在程序中直接使用的库。
一个例子会有所帮助。
我们称之为 main.cpp:
#include <math.h>
int main()
{
double value;
for (int i=0; i < 1000000; i++)
{
for (int j=0; j < 1000; j++)
value = sqrt(100.9);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译使用:
g++ -c main.cpp -o main.o
g++ main.o -o main -lm -lprofiler
Run Code Online (Sandbox Code Playgroud)
使用 ldd ./main 检查可执行文件:
linux-vdso.so.1 => (0x00007fff5a9ff000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f32bc1c9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f32bc593000)
Run Code Online (Sandbox Code Playgroud)
通常,我会运行:
CPUPROFILE=/tmp/profile ./main
Run Code Online (Sandbox Code Playgroud)
生成配置文件输出。但由于配置文件库未链接,因此不会生成配置文件输出。
我确保分析器库在我的搜索路径中,并尝试直接链接共享库和静态库。
上述测试在 Ubuntu 10.04、Ubuntu 10.10、Ubuntu 11.04、SUSE 12.1 和 Fedora …