Hi there,
Could someone help me with best practices for setting up rust to link to a dylib.
I'm trying to create a mixed Rust/C++ application, and I can't seem to get Rust to link against the dylib generated from my C++ codebase. I'm on a Mac, and using CMake+Clang to build the C++ code out.
So it seems the issue is that while rustc can find it correctly during the build, but it fails to find it when running. I can work around this by copying the dylib next to the exe before running it.
What's the best way to handle this situation?
Thank you in advance. I'll try and make a small sample code base to demonstrate this if my description isn't clear enough.
For dynamic linking each library contains a path under which it's expected to be, and linking with that library copies the path to the executable. This is not related to the search path.
In your case your dylib says it has to be in the same directory as the executable, so before running the executable you have to copy the dylib there.
You could also change the rpath to an absolute path using otool, and ensure the dylib is there for all users of the executable (i.e. distribute it with an installer).
Use otool -L exefile to find what paths the executable uses.
So now the issue is that it runs when I use cargo run, but if I navigate to the target/debug directory and run the exe, it fails to find the dylib again.
./spam
dyld: Library not loaded: @rpath/libfoo.dylib
Referenced from: /repo/target/debug/./spam
Reason: image not found
[1] 10710 abort ./spam
Running ottol -L spam gives me:
otool -L spam
spam:
@rpath/libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)
Isn't it possible to set some environment variable to force it to load the library from another path? I thought DYLD_LIBRARY_PATH could be the way to do that.