# Embedding dylib in a macos bundle

**URL:** https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465
**Category:** Usage
**Tags:** os:macos
**Created:** [July 5, 2023, 4:17pm UTC](https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465 "2023-07-05T16:17:43Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![fry](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fry/32/613_2.png) [@fry](https://discourse.cmake.org/u/fry)
#### Post date: [July 9, 2023, 6:07pm UTC](https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465/5 "2023-07-09T18:07:07Z")

</div>

After more trial and error, I was finally able to come up with a satisfactory solution.

First of all, I could never make `BundleUtilities/fixup_bundle` work. I have a feeling that `fixup_bundle` ended up removing some `rpath` in one of the libraries which made it fail to load.

So this is what I came up with. I have no idea whether it is the right approach, but it does work for my use case:

Following [Apple’s documentation](https://developer.apple.com/documentation/xcode/embedding-nonstandard-code-structures-in-a-bundle), I manually updated the 2 libraries that I am using, something like this:

```plaintext
> install_name_tool -id "@rpath/libEGL.dylib" libEGL.dylib
> codesign -s "-" -fv libEGL.dylib

```

Then I simply link with the modified libraries:

```cmake
  set(GOOGLE_ANGLE_DIR "${CMAKE_CURRENT_LIST_DIR}/external/google/angle")
  list(APPEND google_angle_libs "${GOOGLE_ANGLE_DIR}/macOS/libEGL.dylib" "${GOOGLE_ANGLE_DIR}/macOS/libGLESv2.dylib")
  target_link_libraries(${target} PRIVATE "${google_angle_libs}")

```

Then I added a target property to make sure that the rpath is correct:

```cmake
set_target_properties(${target} PROPERTIES
    INSTALL_RPATH "@executable_path/../Frameworks"
)

```

And then finally, I copy the libraries during install

```cmake
install(
    FILES "${google_angle_libs}"
    DESTINATION "$<TARGET_BUNDLE_DIR_NAME:${target}>/Contents/Frameworks"
)

```

So what happens, is that during the build, cmake generates an executable with the following rpath:

```plaintext
Load command 26
      cmd LC_RPATH
      cmdsize 88
      path /Volumes/Vault/tmp/misc-projects/test-raylib/external/google/angle/macOS (offset 12)

```

and the libraries are never copied inside the .app (bundle) because they are found in the source tree… So it just works.

During install, due to the property `INSTALL_RPATH` that is set, cmake will do the right thing and remove the hard-coded rpath and replace it with the correct one and because the libraries are copied inside the bundle (under `Contents/Frameworks`) they will be found as well:

```plaintext
Load command 29
      cmd LC_RPATH
      cmdsize 48
      path @executable_path/../Frameworks (offset 12)

```

---

_[View the full topic](https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465)._
