'How to deploy a shared library with cmake on Linux

I have shared library called "libmylib.so" which sits inside a folder "LIBS" outside my application source directory. To link the library to my main executable, I'm using the following CMake commands:

set(LIBS_FOLDER "${CMAKE_SOURCE_DIR}/../LIBS")
find_library(MYLIB NAMES mylib libmylib mylib.dll HINTS ${LIBS_FOLDER})

target_link_libraries(MyApp PUBLIC ${MYLIB})

This works well on both Linux and Windows, however on Linux when I deploy my application the executable links to mylib using its absolute path. Indeed, running ldd MyApp shows that it is linked to:

libmylib.so => /absolute_path_to_libs_dir/libmylib.so (0x00007f9dae78f000)

This is obviously a problem since I can't deploy this to any environment. How can I get around this problem? Is there way to link and deploy the shared library correctly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source