Trouble using SDL2_Image with .Net on OSX (DotNet Core)

NOTE: I figured this out shortly after asking the question. See bottom of post for the answer.

Hi. I’m not sure if this is the right place to ask this question. (It may be more about my environment, and not sdl-specific.) If it’s not, please let me know and I’ll try on Stack Overflow. Cheers.

I’ve been using SDL2 in C++ for a while, and have successfully had a cross platform project set up between windows and osx (using MingW and GCC, building on command line). I’ve been recently trying to get a similar cross platform setup going using dotnetcore. This was relatively easy for an SDL2-only project, but I’m having trouble getting SDL2_Image working.

The process I’ve followed is based on this tutorial, which uses Homebrew to install sdl on osx.

youtube DOT com/watch?v=vCI8XwHrbL0

And part two, which brings SDL2_Image into the mix:

I just repeated the entire process again from scratch with the following steps:

Install sdl and sdl_image:

brew install sdl2
brew install sdl2_image

(Both install fine.)

Create a console project:

dotnet new console

Then I include the CSharp sdl2 bindings from flibitijibibo. (These are the ones created for use in the FNA-XNA project. I’ve succesfully used these in Windows.)

I entered the following code to test

using System;
using SDL2;

namespace SDL2_Image_test
{
    class Program
    {
        static void Main(string[] args)
        {

            SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING);

            var window = SDL.SDL_CreateWindow(
                "Test",
                SDL.SDL_WINDOWPOS_CENTERED,
                SDL.SDL_WINDOWPOS_CENTERED,
                800,
                600,
                SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE
            );

            var renderer = SDL.SDL_CreateRenderer(
                window,
                -1,
                SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC
            );

            var texture = SDL_image.IMG_LoadTexture(renderer, "bunny.png");
            
            SDL.SDL_Event e;

            var running = true;

            while (running)
            {
                while (SDL.SDL_PollEvent(out e) != 0)
                {
                    switch (e.type)
                    {
                        case SDL.SDL_EventType.SDL_QUIT:
                            running = false;
                            break;
                    }
                }
            }

            SDL.SDL_DestroyTexture(texture);
            SDL.SDL_DestroyRenderer(renderer);
            SDL.SDL_DestroyWindow(window);
            SDL.SDL_Quit();
        }
    }
}


This results in sdl succesfully opening a window, but then immediately crashing with the following message.

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'SDL2_image' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libSDL2_image, 1): image not found
   at SDL2.SDL_image.INTERNAL_IMG_LoadTexture(IntPtr renderer, Byte[] file)
   at SDL2.SDL_image.IMG_LoadTexture(IntPtr renderer, String file)

Following the advice of the message I set DYLD_PRINT_LIBRARIES with:

`export DYLD_PRINT_LIBRARIES=1’

When I next run the project, I get a very long list of dyld events. I can see sdl2_image a few lines up:

dyld: loaded: <6065E6D9-57CA-3DFE-B894-52C533F7CEB4> /usr/local/lib/libSDL2_image.dylib
dyld: loaded: <144C7AAF-D976-3665-ABB2-9FEA80AB1384> /usr/local/opt/libpng/lib/libpng16.16.dylib
dyld: unloaded: <6065E6D9-57CA-3DFE-B894-52C533F7CEB4> /usr/local/lib/libSDL2_image.dylib
dyld: unloaded: <144C7AAF-D976-3665-ABB2-9FEA80AB1384> /usr/local/opt/libpng/lib/libpng16.16.dylib
dyld: loaded: <93D03DD2-8CA3-3199-9238-9FDED9189F14> /usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.0.0/System.Globalization.Native.dylib
Unhandled exception. dyld: loaded: <14FD3ABB-E36C-3F75-BBFE-8218AE7B5AFE> /usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.0.0/System.Native.dylib
System.DllNotFoundException: Unable to load shared library 'SDL2_image' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libSDL2_image, 1): image not found
   at SDL2.SDL_image.INTERNAL_IMG_LoadTexture(IntPtr renderer, Byte[] file)

I note that is seems to load libSDL2_image.dylib, and then unloads it again? At this point I’m at a loss. I’m not sure if this is a problem with Homebrew, Dotnet, my SDL2_Binary, or something else?

I just updated my mac to the latest Catalina, in the hope that it might help, but no luck.

Any tips or advice would be greatly appreciated. Thanks.

UPDATE:
Funny how you often figure it out right after posting to the forum, huh?
I went back to my C++ project on mac, and noticed that it was also crashing, with this message:

dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.9.dylib

Based on some advice from Stack Overflow, I ran:

sudo find / -name 'libjpeg.*'

I saw that there were lots of libjpgs on my machine. As an experiment I ran:

brew install libjpeg

…and got the message

Warning: jpeg 9c is already installed
The currently linked version is 8d
You can use `brew switch jpeg 9c` to link this version.

After running…

brew switch jpeg 9c

… all was well. My C++ project ran fine, and so does my C# project!

I guess I’ll leave this info here in case it helps someone else. Cheers! :slight_smile: