2

I am trying to link against a system library: /usr/lib/x86_64-linux-gnu/libtcmalloc.so.4, when I have this CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(all)

find_library(TCMALLOC_LIB NAMES tcmalloc)
if(TCMALLOC_LIB)
   message("Found TCMALLOC_LIB: ${TCMALLOC_LIB}")
else()
   message(FATAL_ERROR "TCMALLOC_LIB library not found")
endif()

(also tried find_library(TCMALLOC_LIB tcmalloc))

I get

CMake Error at CMakeLists.txt:13 (message):
  TCMALLOC_LIB library not found

While, if I have

find_library(TCMALLOC_LIB NAMES libtcmalloc.so.4)

everything is fine: Found TCMALLOC_LIB: /usr/lib/x86_64-linux-gnu/libtcmalloc.so.4

Am I doing something wrong? Why do I need to specify filename precisely? How can I debug find_library?

DimanNe
  • 1,791
  • 3
  • 12
  • 19
  • 1
    "Am I doing something wrong?" - As usual, for create application which uses the library you need to install appropriate `-dev` package, which provides `libtcmalloc.so` symlink and header files. – Tsyvarev Jul 03 '19 at 21:24
  • You are absolutely right, I was thinking `-dev` library would been installed as a dependency of `libgoogle-perftools4`, but it is not true. I will update my answer. Thanks! – DimanNe Jul 04 '19 at 06:32
  • 1
    I know I'm late to the party, but just in case it might be useful for someone: I had a similar problem (not related to a dev package) and I solved it with SET(CMAKE_FIND_LIBRARY_SUFFIXES "") which I found in this answer: https://stackoverflow.com/a/14248656 – felix-b Sep 22 '20 at 19:25

1 Answers1

3

Update - Real cause

As @Tsyvarev mentioned in the comment, it turns out that cmake could not find the library because I installed it only "partially", the -dev version (libgoogle-perftools-dev) should have been installed.


TL;DR

Well, it turns out that tcmalloc is not a regular/conventional library, namely, it does not have libtcmalloc.so symlink pointing to .so.4, that is why cmake cannot find it.

How can I debug find_library?

I found a way of debugging find_library via strace:

$ rm -rf ./* && strace cmake ../scripts/ 2>&1 | grep tcmalloc
access("/usr/local/nvidia/bin/libtcmalloc.so", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/local/cuda/bin/libtcmalloc.so", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/local/sbin/libtcmalloc.so", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/local/bin/libtcmalloc.so", R_OK) = -1 ENOENT (No such file or directory)
...
<many lines skipped>
...

From this strace output we see that cmake, indeed tries to prepend lib and append .so, but it does not try to prepend any version.

Now, if we look at another "normal" library:

# locate protobuf | grep "\.so"
/usr/lib/x86_64-linux-gnu/libprotobuf.so
/usr/lib/x86_64-linux-gnu/libprotobuf.so.17
/usr/lib/x86_64-linux-gnu/libprotobuf.so.17.0.0

we will see that it DOES install .so symlink, while tcmalloc does not:

# locate tcmalloc | grep "\.so"
/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4
/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4.3.0
DimanNe
  • 1,791
  • 3
  • 12
  • 19
  • I have the same problem in OSX i did install gperftools with `brew install gperftools` and cmake gives me `Found GooglePerfTools: /usr/local/lib/libtcmalloc.dylib` but my make fails saying `ld: library not found for -ltcmalloc` what can i do? – Josyula Krishna Sep 23 '20 at 09:24