2

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?

Joe Strout
  • 2,634
  • 2
  • 28
  • 39
  • 1
    Not familiar with Xcode, but if the frameworks are really in the same folder as the executable, then as a direct linker arg, `-rpath @executable_path/` should work. As a compiler flag, `-Wl,-rpath,@executable_path/`. – Siguza Jul 31 '21 at 22:15
  • That works! I will add it as an answer (with the Xcode details). – Joe Strout Aug 01 '21 at 14:05

1 Answers1

1

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.

Joe Strout
  • 2,634
  • 2
  • 28
  • 39
  • The space in the `-rpath @executable_path/` splits that expression into two expressions for me. Is that how it should work? – Eduard Feb 14 '23 at 06:26