cmake compiler dynamic libray path & runtime dynamic library search path

0
-project
 -src
   ...
   CMakeLists.txt
 -test
   test.cpp
   CMakeLists.txt

in src/CMakeLists.txt generate an so, libabc.so
in test/CMakeList.txt link the library by using LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/src) the compile is ok, the test can find the libabc.so, and i package the libabc.so and test to rpm

but after i install the rpm on another machine,when run `test', an error will show like this

error while loading shared libraries: ../src/libncnn.so: cannot open shared object file: No such file or directory

since the libabc.so in the rpm is install to /usr/lib, but seems the runtime use the relative path

i also try to use find_library(ABC_LIBRARY abc ${PROJECT_SOURCE_DIR}/src), but it occur the same runtime error

use readelf -d to check

$ readelf -d test

Dynamic section at offset 0x1890 contains 35 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [../libabc.so]
 0x00000001 (NEEDED)                     Shared library: [libopencv_core.so.4.2]
 0x00000001 (NEEDED)                     Shared library: [libopencv_highgui.so.4.2]
 0x00000001 (NEEDED)                     Shared library: [libopencv_imgproc.so.4.2]
 0x00000001 (NEEDED)                     Shared library: [libopencv_imgcodecs.so.4.2]
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libgomp.so.1]
 0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]
 0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

the libabc.so contains relative path,while opencv libray doesn't 0x00000001 (NEEDED) Shared library: [../libabc.so]

how to avoid the relatvie path


I found this problem is caused by cmake usually,we link the so like this

  • use -l to specify libname
  • use '-L` to specify libpath

gcc -o test test.cpp -labc -L../src

the generated executable doesn't contain relatvie path

$ readelf -d test

Dynamic section at offset 0x1890 contains 35 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libabc.so]

but in the in CMake link.txt,i found it does not use libabc.so as lib ,but input file ,like this gcc -o test test.cpp ../src/libabc.so and then occur this problem

$ readelf -d test

Dynamic section at offset 0x1890 contains 35 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [../src/libabc.so]
cmake
shared-libraries
asked on Stack Overflow Sep 1, 2020 by Frankdog Deng • edited Sep 2, 2020 by Frankdog Deng

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0