Failed to load SDL library when running game on Mac

Hi all,

Thanks to some help from Kimimaru, I’ve been able to build my game to LInux using Mono’s mkbundle and this tutorial: https://dotnetcoretutorials.com/2018/03/22/bundling-mono-with-a-net-executable-using-mkbundle-on-windows/

Unfortunately, I’ve come across an exception I haven’t been able to fix on the Macbook we’re using for testing.

I’m using the Monogame 3.7 Release.

I’ve tried mkbundling the game with both mono-5.16.0-osx-10.7-x64 and mono-5.16.0-osx-10.7-x64 with the same results. Near as I can tell from the error it’s having trouble loading libSDL2-2.0.0.dylib, which is the Mac-specific replacement for SDL2.dll, but that’s present and accounted for in the same folder as the executable.

What am I missing?

Exception follows:

Last login: Thu Oct 18 08:13:10 on ttys000
/Users/[REDACTED]/Desktop/Species\ ALRE/SpeciesOSX64 ; exit;
admins-MacBook:~ [REDACTED]$ /Users/[REDACTED]/Desktop/Species\
ALRE/SpeciesOSX64 ; exit;

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Sdl'
threw an exception. ---> System.Exception: Failed to load SDL library.
at Sdl.GetNativeLibrary () [0x00156] in <11a2afe69fa8407e8841850a70a7d30a>:0
at Sdl..cctor () [0x00000] in <11a2afe69fa8407e8841850a70a7d30a>:0
--- End of inner exception stack trace ---
at Microsoft.Xna.Framework.GamePlatform.PlatformCreate
(Microsoft.Xna.Framework.Game game) [0x00001] in
<11a2afe69fa8407e8841850a70a7d30a>:0
at Microsoft.Xna.Framework.Game..ctor () [0x00225] in
<11a2afe69fa8407e8841850a70a7d30a>:0
at SpeciesALRE.Game1..ctor () [0x0011c] in
<31ae773542af4bbaaa3eb6a1d4413be6>:0
at SpeciesALRE.Program.Main (System.String[] args) [0x00001] in
<31ae773542af4bbaaa3eb6a1d4413be6>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException:
The type initializer for 'Sdl' threw an exception. --->
System.Exception: Failed to load SDL library.
at Sdl.GetNativeLibrary () [0x00156] in <11a2afe69fa8407e8841850a70a7d30a>:0
at Sdl..cctor () [0x00000] in <11a2afe69fa8407e8841850a70a7d30a>:0
--- End of inner exception stack trace ---
at Microsoft.Xna.Framework.GamePlatform.PlatformCreate
(Microsoft.Xna.Framework.Game game) [0x00001] in
<11a2afe69fa8407e8841850a70a7d30a>:0
at Microsoft.Xna.Framework.Game..ctor () [0x00225] in
<11a2afe69fa8407e8841850a70a7d30a>:0
at SpeciesALRE.Game1..ctor () [0x0011c] in
<31ae773542af4bbaaa3eb6a1d4413be6>:0
at SpeciesALRE.Program.Main (System.String[] args) [0x00001] in
<31ae773542af4bbaaa3eb6a1d4413be6>:0
logout

[Process completed]

Okay, I’m now working from Source.

I found Sdl.GetNativeLibrary() in the source code and worked out that

FuncLoader.LoadLibrary(Path.Combine(assemblyLocation, "libSDL2-2.0.0.dylib")) == IntPtr.Zero. 

Unfortunately, the fallback routine for this situation is “Welp, all failed, PANIC!!!”. That is the actual comment. Not exactly helpful, but it made me giggle. You get a pass.

So, that means that …

        [DllImport("/usr/lib/libSystem.dylib")]
        public static extern IntPtr dlopen(string path, int flags);

… is returning IntPtr.Zero where path=Path.Combine(assemblyLocation, “libSDL2-2.0.0.dylib”). I have to assume assembly location is wrong, then. I don’t think libSD2-2.0.0.dylib would be broken or there’d be a lot more uproar.

Assembly location is defined thusly:

var assemblyLocation = Path.GetDirectoryName((new Uri(typeof(Sdl).Assembly.CodeBase)).LocalPath);

Perhaps if I change LocalPath to AbsolutePath? Worth a shot I guess.

I’m assuming typeof(Sdl) is stored in MonoGame.Framework.dll, so it would have to be a reference to the bundled location? Could it be getting typeof(Sdl) from somewhere else somehow? I could reset assemblyLocation to System.Windows.Forms.Application.StartupPath if that’s the case.

Will submit a few different apps for testing and let you know which, if any, work.

Welp! None of that worked.

I think libSDL2-2.0.0.dylib is legit broken, I have no idea how to fix it, and could really use some help here. Thanks!

Can you print out the path it’s trying to load from? It’s possible Path.Combine is combining them using a back slash instead of a forward slash.

I managed to solve this by replacing:

Path.GetDirectoryName((new Uri(typeof(Sdl).Assembly.CodeBase)).LocalPath

with:

Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

The former was returning the install location of Mono on the test machine, rather than the executable path where the bundled libraries reside.

Had to do this with the bundled OpenAL libraries as well.

@Quasar is this related to my issue on this post? Wondering if sdl library is actually broken as it compiles on a different os version. Any help would be appreciated. Thanks!

@samba Yeah that looks like the same issue.

How I solved it was to do a search in the Monogame source for “new Uri(typeof” and replace it with System.Reflection.Assembly.GetEntryAssembly().Location where it appears.

1 Like

@Quasar Thanks a lot for your help! I succeeded compiling it by adding all the .dll files manually in .csproj as @phuoc.tran suggested here, but would love to try your solution too. I just cannot fully compile MonoGame right now due to a non-code-related issue (or can I?).

I have another question—if I understood correctly, does this mean that High Sierra (10,13) and Catalina (10.15) return different values on new Uri() command? Do you happen to know what makes this difference? Assumption/speculation is welcome. Thanks again!

MonoGame on Mac: Ultimate Installation Guide in 2020

Attaching this link for future reference.