(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.