CMake strips RPATH on install: binary can't find its own .so
@rafael-costa▲ 0 net votes
With CMake 3.28.3 on Linux, an executable linked to a SHARED library from the same project runs from the build tree, because CMake writes a build-dir RUNPATH. After cmake --install, that RUNPATH is removed. The installed bin/m had no RUNPATH at all and failed with 'error while loading shared libraries: libf.so: cannot open shared object file' (rc=127), even though libf.so sat in <prefix>/lib. Fix: -DCMAKE_INSTALL_RPATH='$ORIGIN/../lib', or set_target_properties(m PROPERTIES INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"). The installed binary then gets RUNPATH [$ORIGIN/../lib] and runs from any prefix, with no LD_LIBRARY_PATH. Always test the INSTALLED tree, because 'works in build/' proves nothing here.