Choose folder where DllImport looks for libs? (and the dependencies of a dependency)

Hello, I’m porting a game I made with MonoGame to mac and I’m stuck on a DllImport problem, it usually works just fine, looking for any lib (dylib) on the same folder as the assembly, but I’m facing a serious problem now that a dependency of mine has it own dependencies.

The lib I’m trying to load, it loads just fine from the same folder as the executable, but it looks for the dependencies of that lib on the usr/local/lib.

What is the best way to face this problem?

For me, the optimal solution would be if every lib could be look at in a lib folder inside the game directory, but I’m not sure on how to do that.

Hi, what ide do you use to build your game ?

Xamarin Studio, actually. (edit)

This is Linux, so it depends on the RPATH set in the library. That defines where that library looks for libraries it loads. A lot messier than library loading on Windows, but not as messy as Mac. Do a Google search on setting the RPATH in a library.

Well, but I’m porting it to Mac, so there is less messier method? Or setting the RPATH still the only solution I need to aim for?
Does that mean I need to recompile those libraries? I’m extremely ignorant to anything Mac and Linux related, and as those libraries that I’m talking about is for playing videos with TheoraPlay, there are a lot of dependencies that I’m not sure I can even compile them myself…

Mono: DllImport error loading library '/Users/dumativa/Desktop/LendaDoHeroi/trunk/DumaLegend/DumaLegend.DesktopGL/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/bin/Debug/libtheoraplay.dylib': 'dlopen(/Users/dumativa/Desktop/LendaDoHeroi/trunk/DumaLegend/DumaLegend.DesktopGL/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/bin/Debug/libtheoraplay.dylib, 9): Library not loaded: /usr/local/lib/libogg.0.dylib
  Referenced from: /Users/dumativa/Desktop/LendaDoHeroi/trunk/DumaLegend/DumaLegend.DesktopGL/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/DumaLegend.DesktopGL2/bin/Debug/libtheoraplay.dylib
  Reason: image not found'.

Here is a dump from running the game with MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll

If there was a less messy method on Mac, I would have jumped on it long ago. You don’t need to recompile the lib. You can use some command line tools to fix the paths embedded in the library. This Stack Overflow post has some details.

Thank you, @KonajuGames. That made the trick.

Just for future reference, in case of anyone with the same problem finds this post.
I’ve used the following commands:

To figure out where is it looking for the references:

otool -l -v mylib.dylib

To change the path for a relative path

install_name_tool -change /existing/old/path/otherlib.dylib @loader_path/new/path/otherlib.dylib  mylib.dylib