1
votes

I have installed SDL2. I can link my program with

clang++ -lSDL2 -lv8 main.o test.o -o nano8

However, for distributing reasons, I'd like to give SDL2 away with the binary, and hence i've copied libSDL2-2.0.0.dylib under /myapp/lib/libSDL2-2.0.0.dylib As for the documentation, @executable_path should allow me to link to that dylib instead of the one in /usr/local/lib, but if I run

clang++ @executable_path/lib/libSDL2-2.0.0.dylib -lv8 main.o test.o -o nano8

I get the error

clang: error: no such file or directory: '@executable_path/lib/libSDL2-2.0.0.dylib'

How to set the search path for a dylib?

1

1 Answers

1
votes

This is a pain to get right.

I assume that /myapp can be anywhere in the filesystem, and want to load the library from @executable_path/../lib/libSDL2.dylib?

If so you have to use the Run Path and copy and modify the library during linking (this needs to be done every build):

  1. cp /usr/local/lib/libSDL2.dylib build_dir/lib.
  2. Change the install name of the .dylib using install_name_tool:

    install_name_tool -change /usr/local/lib/libSDL2.dylib @rpath/libSDL2.dylib build_dir/lib/libSDL2.dylib
    
  3. Link against build_dir/lib/libSDL2.dylib and set the rpath during linking:

    cd build_dir/bin
    clang++ obj_dir/main.o obj_dir/test.o -o nano8 -L ../lib -lv8 -lSDL2 -Xlinker -rpath -Xlinker "@executable_path/../lib"
    

I have not tested this and there is likely to be errors. Use otool -L to examine the library references in both the binary and the libraries in order to home-in on this issue.

A cleaner way:

Install your binaries into /usr/local/bin and provide any .dylibs to be installed into /usr/local/lib. This is better as /usr/local is the place for user-binaries and you don't need the faff above.