35

I wish to build and install a software locally to the $HOME/.local/ path instead of a system-wide /usr/ folder. The software uses CMAKE for compilation.

After installation, the software binaries and libraries get stored in $HOME/.local/bin/ and $HOME/.local/lib/, respectively. However, when I try to run the program, it throws an error that the required library is not found (which, by the way, is present in $HOME/.local/lib/).

The program works fine if I set the $LD_LIBRARY_PATH to $HOME/.local/lib. But I don't want to do this. Hence, instead of this, I would like to know how to specify the RPATH variable (which would point to $HOME/.local/lib) while compiling the software using CMAKE.

Kindly help.

1

4 Answers 4

30

I am using the following two lines in the CMakefile

set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

(the first one is required only if you use MacOSX)

2
  • 10
    CMAKE_INSTALL_RPATH is a predefined list, so I can see scenarios where it would be better to do list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib ). If you include( GNUInstallDirs ) you could also list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_LIBDIR} ). I am a CMake novice though, and I'm just getting into it, so if someone sees an issue with the above, do please let me know.
    – PfunnyGuy
    Commented Oct 11, 2021 at 22:19
  • 2
    @PfunnyGuy thanks for the list( APPEND ...) advice. Maybe put that up as an alternative answer? Commented Feb 6, 2023 at 20:59
13

CMAKE_INSTALL_RPATH is a predefined list, so I can see scenarios where it would be better to do

list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib )

If you include( GNUInstallDirs ) you could also

list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_LIBDIR} )

I am a CMake novice though, so if someone sees an issue with the above please let me know.

3

you may also use:

set(CMAKE_BUILD_RPATH "/my/libs/location")

specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). The entries will not be used for binaries in the install tree. See also the CMAKE_INSTALL_RPATH variable.

1

CMAKE_BUILD_RPATH works for me, or setting the linker rpah.

  • Solution 1: set the rpath before add_executable
project(app)
set(RPATH "/lib/custom")
list(APPEND CMAKE_BUILD_RPATH ${RPATH})
...
add_executable(${PROJECT_NAME}
    ${SOURCES}
    ${HEADERS}
)
  • Solution 2: change linker rpath
set(RPATH "/lib/custom")
set_target_properties(${PROJECT_NAME}
       PROPERTIES
       LINK_FLAGS "-Wl,-rpath,${RPATH}"
)

Not the answer you're looking for? Browse other questions tagged or ask your own question.