how to get Xcode to set LC_RPATH (so that @rpath loads will work)?

7k Views Asked by At

I want to make a command-line tool with a few bundled frameworks that it needs (SDL and related libs). I have Xcode set to "Embed & Sign" these frameworks, and this results in them being copied to the build folder next to the executable. The resulting executable runs correctly within Xcode.

But when I try to run the same executable on the command line, it fails with:

dyld: Library not loaded: @rpath/SDL2.framework/Versions/A/SDL2
  Referenced from: /Users/jstrout/Data/CPP-dev/soda/Build/Products/Debug/./soda
  Reason: image not found
zsh: abort      ./soda

even though, relative to the current (and executable) directory, the image is indeed at SDL2.framework/Versions/A/SDL2. The problem appears to be that rpath is not set correctly. All my searching says that I should find an LC_RPATH command in the output of otool -l on my executable, but in fact there is no LC_RPATH command in there at all (despite several LC_LOAD_DYLIB commands that reference @rpath).

So. What must I do in Xcode to get it the linker to emit the proper LC_RPATH command? Or, if I'm barking up the wrong tree entirely: how do I get my command-line executable to reference frameworks that are sitting right next to it?

2

There are 2 best solutions below

1
Joe Strout On

The answer appears to be (thanks to @Siguza):

  1. Navigate to project settings, selecting the Target on the left and "All" on the top.
  2. Under "Linking", find "Other Linker Flags", and enter -rpath @executable_path/

Building now produces a proper LC_RPATH command in the executable, and it is able to find frameworks sitting next to it.

0
Jeshua Lacock On

In case this helps someone, the existing answer did not work for me, but adding @executable_path/ to Runpath Search Paths of my target did.

From: dyld: Library not loaded: @rpath/libswiftCore.dylib / Image not found