1

I am using CMake to compile an executable that is linked against several libraries that I have built and installed into a local project directory (libs/3rdparty). Note that this is prior to installation of the project, primarily for the purpose of running unit tests and debugging. The problem I am having is that sometimes there is a library that is linked, but the executable is missing the path to the library. The library I am currently having an issue with is leptonica. However, I have run into this issue several times with different libraries on different platforms (osx, fedora, centos, ubuntu). Through research I have seen similar issues, but I have never been able to find a definitive answer of why the full path to the library would be missing.

I've tried playing with:

CMAKE_BUILD_WITH_INSTALL_RPATH
CMAKE_INSTALL_RPATH
CMAKE_INSTALL_RPATH_USE_LINK_PATH

and these don't seem to have much effect.

My CMakeLists contains:

find_package(Leptonica REQUIRED)

target_link_libraries(${target}
    PRIVATE
        ...
        ${Leptonica_LIBRARIES}
)

Here is the output from ldd on one of the unit test executables:

ldd test_utilities
...
libleptonica.so.5.3.0 => not found
libtesseract.so.4 => {MY PROJECT}/libs/3rdparty/tesseract/lib/libtesseract.so.4

leptonica is the only library that is not found out of ~30 other libraries.

Does anyone know what the root cause of this problem is? I am not looking to work around the problem by modifying LD_LIBRARY_PATH.

-- Added LeptonicaTargets-release.cmake. According to this the full path to the lib should be in the target.

#----------------------------------------------------------------
# Generated CMake target import file for configuration "RELEASE".
#----------------------------------------------------------------

# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)

# Import target "leptonica" for configuration "RELEASE"
set_property(TARGET leptonica APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(leptonica PROPERTIES
  IMPORTED_LINK_INTERFACE_LIBRARIES_RELEASE "/usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libz.so;m"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libleptonica.so.1.77.0"
  IMPORTED_SONAME_RELEASE "libleptonica.so.5.3.0"
  )

list(APPEND _IMPORT_CHECK_TARGETS leptonica )
list(APPEND _IMPORT_CHECK_FILES_FOR_leptonica "${_IMPORT_PREFIX}/lib/libleptonica.so.1.77.0" )

# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

Here are the files in the leptonica/lib directory:

ll libs/3rdparty/leptonica/lib/ 
total 2776
drwxr-xr-x 3 user user    4096 May 30 14:17 ./
drwxr-xr-x 5 user user    4096 May 30 14:17 ../
lrwxrwxrwx 1 user user      21 May 30 14:17 libleptonica.so -> libleptonica.so.5.3.0
-rw-r--r-- 1 user user 2829784 May 30 09:49 libleptonica.so.1.77.0
lrwxrwxrwx 1 user user      22 May 30 14:17 libleptonica.so.5.3.0 -> libleptonica.so.1.77.0
drwxr-xr-x 2 user user    4096 May 30 14:17 pkgconfig/

Output from chrpath --list test_utilities appears to contain the correct path to the library as well:

chrpath --list test_utilities
test_utilities: RUNPATH=...:{MY PROJECT}/libs/3rdparty/leptonica/lib:...
user3157892
  • 41
  • 1
  • 9
  • What is the value of `Leptonica_LIBRARIES`? Depending on it's value RPATH may or may not be what you expect. Refer to https://stackoverflow.com/questions/25378337/cmake-not-setting-rpath-when-adding-link-library-with-l. – fdk1342 May 30 '19 at 16:22
  • The value is just "leptonica", however AFAIK the paths should be handled by the target properties defined in LeptonicaTargets-release.cmake. Maybe there is an error in their cmake files... – user3157892 May 30 '19 at 19:15
  • Added the referenced file above – user3157892 May 30 '19 at 19:28
  • Seems strange that it uses version `5.3.0` in some places and `1.77.0` in others. Looks like `ldd` is looking for `5.3.0` but that doesn't exist. – fdk1342 May 30 '19 at 20:31
  • Do you ever have `libleptonica.so.5.3.0` file in the `{MY_PROJECT}/libs/3rdparty/leptonica` directory? This should be a **symlink** to the file `libleptonica.so.1.77.0` which is used by the linker to link with. BTW, the file `lept.pc` is completely unrelated to the problem: `find_package(Leptonica)` uses `LeptonicaConfig.cmake` one (and the `LeptonicaTargets-release.cmake`, which is included into that script). – Tsyvarev May 30 '19 at 20:34
  • Yeah, I realized that after I made the first update. Not sure why I included that in the first place. Just made an update that shows the files in the lib directory. – user3157892 May 30 '19 at 21:09
  • Maybe use `chrpath --list test_utilities` to list RPATH in the executable to make sure that `{MY_PROJECT}/libs/3rdparty/leptonica/lib` is present? – fdk1342 May 30 '19 at 21:40
  • It appears that the RPATH is correct in the executable as well. It is odd that all of the libraries are linked like this, but this is the only one with issues. Must be missing something small. Also note that I do not have this issue on Fedora 29+. Just with Ubuntu 18.04 at the moment. – user3157892 May 30 '19 at 21:49

1 Answers1

0

For anyone who runs across this, I have finally figured it out.

The issue was related to the library being a transitive dependency of OpenCV. On Ubuntu, ld now defaults to using using --enable-new-dtags which uses RUNPATH, not RPATH. There is an issue where RUNPATH is not searched for transitive dependencies.

See https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1253638

Simply adding "-Wl,--disable-new-dtags" to the target linker options resolved my issue. All libraries are now found, including other libraries than leptonica that I added today. I am sure that I will likely have to make changes when building a package for installation though.

user3157892
  • 41
  • 1
  • 9