CMAKE RPATH not working - could not find shared object file

前端 未结 3 897
旧时难觅i
旧时难觅i 2021-01-05 02:21

I am trying to get rid of setting LD_LIBRARY_PATH everytime time I run my program. After adding in the library and targeting my executable to the library, when

3条回答
  •  暖寄归人
    2021-01-05 02:56

    I had a similar issue as the original post. I created executables which linked to external shared libraries. This approach compiled and executed fine from the build directory. However, the executable that was installed to a separate directory could not find a shared library at runtime:

    error while loading shared libraries: libxxxx.so.1: cannot open shared object file: No such file or directory

    To solve, I

    1) upgraded to CMake 3.17

    2) used Craig Scott's recommended: set(CMAKE_INSTALL_RPATH $ORIGIN) as explained in his talk

    3) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) as directly mentioned to solve this error in the second common question in Kitware's documention

    4) Put all this before adding the targets as mentioned in this post

    5) Used the "$ORIGIN/../lib" syntax instead of Craig's Scott's mentioned $ORIGIN as mentioned by @explo91

    In summary, and to my suprise, only the "$ORIGIN/../lib" before the target definition was necessary from above (I tested the other combinations which did not fix the cannot open shared object file runtime issue).

    Anyway the solution I finally applied, which may be of better, fine-grained CMake style or at least may be helpful to others on their RPATH journey is:

    set_target_properties(target_defined_above PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")

提交回复
热议问题