(Sorry if this is more of a SDL2/Homebrew question than Nim)
Just got my new M1 MacBook pro, Nim is installed (via Homebrew) and seems to be running fine. However, when I try to compile and run anything using SDL2, I get an error saying "could not load: libSDL2.dylib".
If I compile with -d:nimDebugDlOpen, I can see that all the paths it's searching for sdl2 are under "/usr/...", but the location that Homebrew installs any ARM library is under "/opt/homebrew/..."
I could copy the files manually to my application, but that seems wrong and sounds like a pain to maintain (i.e. every time the library is updated). Is there a way to fix this so that the search path is correct?
After some googling around I added these to ~/.zprofile:
eval "$(/opt/homebrew/bin/brew shellenv)"
export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib
PATH=/opt/homebrew/bin:$PATH
But that didn't work. It's like the C compiler is ignoring the path completely?
Any help is welcome. Thanks!
You should be using LD_LIBRARY_PATH. Also, you should not overwrite it but append to it like so:
export LD_LIBRARY_PATH=/opt/homebrew/lib:$LD_LIBRARY_PATH
Thanks, but still doesn't work. The compiler still ignores any path I add. I'm currently setting these environment variables:
export PATH=/opt/homebrew/Cellar:/opt/homebrew/lib:$PATH
export CPATH=/opt/homebrew/include:$CPATH
export LIBRARY_PATH=/opt/homebrew/Cellar:/opt/homebrew/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/homebrew/Cellar:/opt/homebrew/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=/opt/homebrew/Cellar:/opt/homebrew/lib:$DYLD_LIBRARY_PATH
I can verify that they are exported correctly in the command shell. But when I compile I get (formatted for clarity):
dlopen(libSDL2.dylib, 0x0002): tried:
'libSDL2.dylib' (no such file),
'/usr/local/lib/libSDL2.dylib' (no such file),
'/usr/lib/libSDL2.dylib' (no such file),
'/usr/local/lib/libSDL2.dylib' (no such file),
'/usr/lib/libSDL2.dylib' (no such file)
could not load: libSDL2.dylib
Super frustrating... Well if the link target is the library name without version it won't be found by modifying LD_LIBRARY_PATH.
Often what is done is to use symlink to solve this ergo having libmystuff.so being a symlink to libmystuff.so.1.7
you schould check the architecture target of the dylib: file libSDL2.dylib
M1 can run both arm and x86_64 program types, but as far i saw nim compiles to x86_64. So maybe you need to install or build libSDL2 for this target.
Having the exact same problem with SDL2 in Nim 2.2.4 on an M2Max MacbookPro in MacOS 14.
Refuses to find libsdl2.dylib even when it's copied into the same working directory as my source / binaries.
Symlink does not fix it.
Anyone have a solution for this?
MacOS doesn't really support LD_LIBRARY_PATH anymore. Instead the best way is to modify the link search paths at compile time with: -Wl,-rpath,$LIBPATH. There's some way to do it later using otool or some other tool but I never could figure it out. The good thing is that you can add several paths and they're all searched.
Example:
when defined(macosx):
{.passL: "-Wl,-rpath,/opt/homebrew/lib/postgresql@14".}
Note that MacOS security stuff unsets DYLD_LIBRARY_PATH in child processes. It's part of their SIP stuff. So it works until it doesn't.
Thanks for the headsup.
(I often wonder why I bother providing binaries for macOS. They make it difficult for developers at every turn.)
(I often wonder why I bother providing binaries for macOS. They make it difficult for developers at every turn.)
MacOS is annoying at times. To be fair DYLD_LIBRARY_PATH is a pretty big vulnerability, especially for system processes. So SIP and sandboxing isn't too bad after you get used to it.
Unlike on Windows where every Nim binary gets flagged as a virus. That'd not be so bad if there was a way to say "yes really this isn't a virus", even if you had to create a CA or something.
I guess it's a tradeoff. Even some Linux distros have started adding all sorts of annoying SELinux / AppArmor stuff, sigh. The new Wayland stuff apparently doesn't let you run tons of apps like automation apps or screen-readers for "security" reasons.
Maybe it's time to move to *BSD's. ;) FreeBSD + Macbook M4 might just be amazing.