Ubuntu 11.10 linking perftools library

I cannot get gcc in Ubuntu 11.10 for the correct link in google perftools -lprofiler. The problem is that the linker discards libraries that are not directly used in the program.

An example will help.

Call this 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; } 

Compile using:

 g++ -c main.cpp -o main.o g++ main.o -o main -lm -lprofiler 

Check the executable using 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) 

I usually ran:

 CPUPROFILE=/tmp/profile ./main 

to display the profile. But since the profile library is not associated with any profile, a profile is generated.

I made sure that the profiler library is in my search path and tried to link directly to the shared library and the static library.

The above test works fine on Ubuntu 10.04, Ubuntu 10.10, Ubuntu 11.04, SUSE 12.1 and Fedora 16.

Also, when I include function calls that use the profiler (such as ProfilerStart () and ProfilerStop ()), then the profiler library is associated with the executable.

Any ideas on how to get gcc for the link in the profiler library?

Thanks.

+4
source share
2 answers
 g++ main.o -o main -lm -lprofiler 

As another noted. anon.coward, you are likely to fall prey to your g++ using the --as-needed linker flag. Try instead:

 g++ main.o -Wl,--no-as-needed -lprofiler -Wl,--as-needed 

Notes:

  • g++ already adds -lm , no need to add it again
  • It is important to turn on --as-needed again. Not doing this will most likely lead to a link to additional libraries that you really don't need.
+4
source

In my case, the problem was that there was only libprofiler.so.0 , and no libprofiler.so in /usr/lib/ :

 user@compy :/usr/include$ dpkg -L libgoogle-perftools4 /. /usr /usr/share /usr/share/doc /usr/share/doc/libgoogle-perftools4 /usr/share/doc/libgoogle-perftools4/README.Debian /usr/share/doc/libgoogle-perftools4/copyright /usr/lib /usr/lib/libprofiler.so.0.4.5 /usr/lib/libtcmalloc.so.4.2.6 /usr/lib/libtcmalloc_debug.so.4.2.6 /usr/lib/libtcmalloc_and_profiler.so.4.2.6 /usr/share/doc/libgoogle-perftools4/AUTHORS /usr/share/doc/libgoogle-perftools4/TODO /usr/share/doc/libgoogle-perftools4/README.gz /usr/share/doc/libgoogle-perftools4/NEWS.gz /usr/share/doc/libgoogle-perftools4/changelog.Debian.gz /usr/lib/libtcmalloc.so.4 /usr/lib/libtcmalloc_and_profiler.so.4 /usr/lib/libprofiler.so.0 /usr/lib/libtcmalloc_debug.so.4 

I don’t know what the official fix is, but I just created a symlink in / usr / lib:

 user@compy :/usr/lib$ sudo ln -s libprofiler.so.0 libprofiler.so 

This will do the -lprofiler job.

If you don't mind changing your Makefile, you can instead specify -l:libprofiler.so.0 instead of -lprofiler (note the extra colon) ( source ).

EDIT: The official way to get .so apparently to install the libgoogle-perftools-dev package, as described here :

 user@compy :/usr/lib$ dpkg -S libprofiler.so libgoogle-perftools-dev: /usr/lib/libprofiler.so libgoogle-perftools4: /usr/lib/libprofiler.so.0.4.5 libgoogle-perftools4: /usr/lib/libprofiler.so.0 

I understand that if you want to link to a specific lib library, you must install the libx-dev package, which will contain /usr/lib/libx.so . This file will only be a symlink to a specific version, for example /usr/lib/libx.so.1.2 . When you contact /usr/lib/libx.so by pointing -lx to your linker, you will actually create a link in your program against the specific version linked at that time by writing SONAME from libx.so.1 (the last version number is deleted as oaled here ). Therefore, when you run your program at a later point in time, the dynamic linker will only look for /usr/lib/libx.so.1 , which is symbolically associated with /usr/lib/libx.so.1.2 , and no /usr/lib/libx.so , therefore, the dev package must exist.

So, libx-dev packages libx-dev designed to compile and bind to libx , and the libx package libx designed to run a precompiled program with libx .

+3
source

Source: https://habr.com/ru/post/1399917/


All Articles