# Help with RPATH and install prefix

**URL:** <https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095>\
**Category:** Code\
**Created:** [September 26, 2023, 9:37am UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095 "2023-09-26T09:37:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![buxtonpaul](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buxtonpaul/32/3852_2.png) [@buxtonpaul](https://discourse.cmake.org/u/buxtonpaul)\
**Post date:** [September 26, 2023, 9:37am UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095/1 "2023-09-26T09:37:24Z")

</div>

Hi,  
I am trying to produce a project that is relocatable at install time with it’s own shared libraries. Such that someone could either install it system wide by just running the installer, or could install it in a user folder and have it pick up the correct library paths to it’s own version of it’s libraries.

From the Wiki [https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath) it reads as though you can configure the installer such that it will cleanly handle both build and install time rpath requirements. Using the sample at the link I get the expected behaviour when I set INSTALL\_PREFIX at generation time, but if I specify the install path using the --prefix option to the installer, the filest get installed into the correct locations but the RPATH is setup to look in the standard install paths e.g. /usr/local/lib.

It looks to me like CMAKE\_INSTALL\_PREFIX is in effect hard coded at generate/build time, which makes sense, but that isn’t clear in the example in the wiki. Is that the expected behaviour, should I just change my applications RPATH to be a relative path e.g.

```auto
set(CMAKE_INSTALL_RPATH "../lib")

```

The cmake project based on the wiki looks like

```auto

cmake_minimum_required(VERSION 3.14)

Project(localtest)
# use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
    set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif("${isSystemDir}" STREQUAL "-1")

add_library(test_lib SHARED mylib.cpp )
add_executable(helloapp app.cpp)
target_link_libraries(helloapp test_lib)

install(TARGETS helloapp test_lib RUNTIME DESTINATION bin LIBRARY DESTINATION lib)

```

Thanks,  
Paul

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 26, 2023, 1:35pm UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095/2 "2023-09-26T13:35:32Z")

</div>

I think you want `$ORIGIN/../lib` as the rpath here. Bare relative paths may be relative to the current working directory when loaded.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 1, 2023, 6:33pm UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095/3 "2023-10-01T18:33:01Z")

</div>

It seems to me, `$ORIGIN` is only for Linux?  
See too [RPATH-handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling)

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [October 1, 2023, 7:17pm UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095/4 "2023-10-01T19:17:08Z")

</div>

Yes. `@loader_path/` is the equivalent on macOS.

---

<div class="post-metadata">

**Author:** ![buxtonpaul](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buxtonpaul/32/3852_2.png) [@buxtonpaul](https://discourse.cmake.org/u/buxtonpaul)\
**Post date:** [October 2, 2023, 9:36am UTC](https://discourse.cmake.org/t/help-with-rpath-and-install-prefix/9095/5 "2023-10-02T09:36:17Z")

</div>

Thanks for the help.

Found quite a nice article here [CMake Fundamentals Part 8 | Jeremi Mucha](https://jeremimucha.com/2021/04/cmake-fundamentals-part8/) which also used the $ORIGIN

Paul
