How can LD_LIBRARY_PATH be changed within CMake?

后端 未结 3 417
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 19:23

I have a local shared library which is not in $LD_LIBRARY_PATH. I want to run my executable, but since it cannot find the shared library in the system folders, it outputs \"

相关标签:
3条回答
  • 2021-01-03 20:02

    When you use this solution:

    SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    

    Remembter to set the RPATH before defining the targets in your CMake-File. So this instruction have to be before add_executable() or add_library() is called, otherwise it has no effect.

    0 讨论(0)
  • 2021-01-03 20:06

    You can set the runtime shared library search path using the -rpath linker option:

    SET(CMAKE_EXE_LINKER_FLAGS 
              "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,/usr/local/lib")
    
    0 讨论(0)
  • 2021-01-03 20:09

    If your shared lib is not build in the same CMake project of your executable, you can use the CMake rpath handling like this:

    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    

    When you will run make install, CMake will automatically set the runtime path of your executable to your shared library.

    If your shared library is built in the same CMake project, use this:

    set(CMAKE_INSTALL_RPATH "/usr/local/lib")
    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    

    In this case you must add yourself the directory where your shared library will be installed to the runtime path.

    For more information, you can read CMake rpath handling

    0 讨论(0)
提交回复
热议问题