# How to get an LC\_RPATH and @rpath/ prefix on a dylib on macos

**URL:** <https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540>\
**Category:** Code\
**Tags:** os:macos\
**Created:** [April 26, 2022, 12:23am UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540 "2022-04-26T00:23:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris-miner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chris-miner/32/2374_2.png) [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Post date:** [April 26, 2022, 12:23am UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/1 "2022-04-26T00:23:59Z")

</div>

Hi All,  
I’m trying to solve the apparently common problem on macos of getting cmake to do what I’m doing with `install_name_tool`. The common problem is that I link against 3rd party dylib, and at run-time the dylib is not found because it is not where macos expects it. (It isn’t installed in /usr/local/lib)

For my purposes, I’d like the reference to the dylib to be prefixed with `@rpath/`, and I’d like an LC\_RPATH entry pointing at the location of the library that I linked against. I can do this using the `install_name_tool`:

```auto
install_name_tool -add_rpath /path/to/lib MyExecutable
install_name_tool -change libThirdParty.dylib @rpath/libThirdParty.dylib MyExecutable

```

Trouble is, despite all the posts on the subject, I have yet to find one that spells out what to do. There are hints about `MACOSX_RPATH`, use a `POST_BUILD` command (to do what I’m doing on the command-line), do something with `CMAKE_BUILD_WITH_INSTALL_RPATH`, or simply `CMAKE_INSTALL_RPATH_USE_LINK_PATH`.

Regardless of what I’ve tried, I never end up with new name in the `LC_LOAD_DYLIB` command nor a new path and an `LC_RPATH` command.

What can I try next?

---

<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:** [April 26, 2022, 1:29am UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/2 "2022-04-26T01:29:18Z")

</div>

If the dependency is not built with an `@rpath`-using library ID, I would recommend getting it built with such an ID first. Any fixes like `install_name_tool -change` are best done during packaging rather than on every use within the build because it doesn’t scale at that point.

---

<div class="post-metadata">

**Author:** ![chris-miner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chris-miner/32/2374_2.png) [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Post date:** [April 26, 2022, 1:28pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/3 "2022-04-26T13:28:50Z")

</div>

Thanks for the response! Supposing I did patch the build of the dependency. Would I get my executable’s LC\_RPATH command in the usual way via `-rpath` or is there a “proper” CMake way to do it?

---

<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:** [April 26, 2022, 1:52pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/4 "2022-04-26T13:52:06Z")

</div>

You can use the target [`BUILD_RPATH`](https://cmake.org/cmake/help/latest/prop_tgt/BUILD_RPATH.html) and [`INSTALL_RPATH`](https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html) properties to add the path you need.

---

<div class="post-metadata">

**Author:** ![chris-miner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chris-miner/32/2374_2.png) [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Post date:** [April 26, 2022, 3:51pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/5 "2022-04-26T15:51:52Z")

</div>

Hmm… something like this?

```auto
add_executable(MyExecutable ${SOURCES_STAGE} )
set_target_properties(MyExecutable PROPERTIES
    BUILD_RPATH "/this/path/should/be/used"
    INSTALL_RPATH "/so/should/this"    
    )

```

Does this only affect the loader commands in the executable when it is installed? It doesn’t look like it changed anything according to `otootl -l` after I did a clean rebuild `cmake --build build --clean-first`.

---

<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:** [April 26, 2022, 4:48pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/6 "2022-04-26T16:48:28Z")

</div>

`INSTALL_RPATH` only affects the installed binary; `BUILD_RPATH` should show up in the build tree.

---

<div class="post-metadata">

**Author:** ![chris-miner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chris-miner/32/2374_2.png) [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Post date:** [April 26, 2022, 8:23pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/7 "2022-04-26T20:23:25Z")

</div>

Does this mean that if I set the `BUILD_RPATH` property on my executable, that I should expect to see a matching LC\_RPATH entry in executable found in my build tree?

---

<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:** [April 26, 2022, 8:41pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/8 "2022-04-26T20:41:36Z")

</div>

Yes. It should show up on the executable’s link line as well.

---

<div class="post-metadata">

**Author:** ![chris-miner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chris-miner/32/2374_2.png) [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Post date:** [April 27, 2022, 5:36pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540/9 "2022-04-27T17:36:09Z")

</div>

Well this hasn’t been at all painful. 😉 Based on your comments I created a test project with nothing but hello world and an rpath — and … it worked. Just didn’t work in my “real” project.

After reducing my “real” project to building my test project, it still didn’t work, so I thought to compare CMakeCaches between the two projects. I found that the broken project had `CMAKE_SKIP_RPATH` set. So I trashed the cache, and reconfigured at which point I noticed a log message: `Conan: Adjusting default RPATHs Conan policies` which is presumably where the bit about skipping rpaths is set up. Just … wow.

Now to figure out where/ **why** /how conan does that.

Anyway… thanks for moving the ball down the field!
