For using find_package(
for another library that I am using (it is not supporting CMake), I have developed something like this (I am using gcc10 on CentOS 7 and CMake 3.20 with Ninja 1.10.2):
add_library(MyLib UNKNOWN IMPORTED)
target_include_directories(MyLib
INTERFACE "$ENV{MY_LIB}/include")
target_link_libraries(MyLib INTERFACE ${CMAKE_DL_LIBS})
set_property(TARGET MyLib PROPERTY
IMPORTED_LOCATION "$ENV{MY_LIB}/lib/libmylib.so"
)
and in my project, I simply use find_package
and link against it:
find_package(MyLib REQUIRED PATHS "${CMAKE_MODULE_PATH}" NO_DEFAULT_PATH)
target_link_libraries(myApp
PUBLIC MyLib
)
The strange behavior that I see with this code is that the linker puts the absolute path of libmylib.so
in myApp
target. In other words, if I copy/move my whole project into a new folder, it fails to load the library:
/data/projects/myApp/external/myLib/lib/libmylib.so: cannot open shared object file: No such file or directory
Can anyone tell me why the path to 'libmylib.so' is hard-coded?
Note: the LD_LIBRARY_PATH is already set and valid.
Update 1: If I run readelf -d myApp | head -20
, I will see this result:
[user]$ readelf -d myApp | head -20
Dynamic section at offset 0x101588 contains 73 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libsth1.so]
0x0000000000000001 (NEEDED) Shared library: [libsth2.so]
0x0000000000000001 (NEEDED) Shared library: [/data/projects/myApp/external/myLib/lib/libmylib.so]
Update 2: I have tried to unset these two variables, but nothing changes:
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_SKIP_RPATH TRUE)