SFML / SFML.Net

Official binding of SFML for .Net languages

Home Page:https://www.sfml-dev.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't figure out why it's not working on linux anymore

Thraka opened this issue · comments

I'm trying to run my .NET 5.0 game which uses a library that references SFML.NET. A year or so ago this was running fine, but now I can't seem to get it to work. I get this error:

unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.DllNotFoundException: Unable to load shared library 'csfml-graphics' or one of its dependencies. In order 
to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libcsfml-graphics: cannot open 
shared object file: No such file or directory

Looking at the output, the libraries are there in the the runtimes folders, so perhaps the mapping isn't working? I'm using xubuntu 20.04. When I swap to MonoGame, it works fine with the mapping. I've not discovered what is different between the two libraries though.

Here is my publishing output:

andy@linuxserver:~/code/sadconsolegame$ ls bin/Debug/net5.0/ -R -l
bin/Debug/net5.0/:
total 1488
-rwxrw-r-- 1 andy andy 693680 Nov  9  2019 Newtonsoft.Json.dll
drwxrwxr-x 2 andy andy   4096 Jan  7 11:02 ref
drwxrwxr-x 7 andy andy   4096 Jan  7 11:02 runtimes
-rwxrw-r-- 1 andy andy 322560 Dec 19 21:55 SadConsole.dll
-rwxr-xr-x 1 andy andy 143656 Jan  7 11:02 sadconsolegame
-rw-rw-r-- 1 andy andy  17526 Jan  7 11:02 sadconsolegame.deps.json
-rw-rw-r-- 1 andy andy   5120 Jan  7 11:02 sadconsolegame.dll
-rw-rw-r-- 1 andy andy   9884 Jan  7 11:02 sadconsolegame.pdb
-rw-rw-r-- 1 andy andy    149 Jan  7 11:02 sadconsolegame.runtimeconfig.dev.json
-rw-rw-r-- 1 andy andy    217 Jan  7 11:02 sadconsolegame.runtimeconfig.json
-rwxrw-r-- 1 andy andy  44032 Dec 19 22:03 SadConsole.Host.SFML.dll
-rwxrw-r-- 1 andy andy  22016 Feb 13  2019 SFML.Audio.dll
-rwxrw-r-- 1 andy andy  63488 Feb 14  2019 SFML.Graphics.dll
-rwxrw-r-- 1 andy andy  14336 Feb 15  2019 SFML.System.dll
-rwxrw-r-- 1 andy andy  32768 Feb 14  2019 SFML.Window.dll
-rwxrw-r-- 1 andy andy 101888 Nov 23 01:38 TheSadRogue.Primitives.dll
-rwxrw-r-- 1 andy andy  10752 Oct 25 00:33 TheSadRogue.Primitives.SFML.dll

bin/Debug/net5.0/ref:
total 8
-rw-rw-r-- 1 andy andy 5120 Jan  7 11:02 sadconsolegame.dll

bin/Debug/net5.0/runtimes:
total 20
drwxrwxr-x 3 andy andy 4096 Jan  7 11:02 alpine-x64
drwxrwxr-x 3 andy andy 4096 Jan  7 11:02 debian-x64
drwxrwxr-x 3 andy andy 4096 Jan  7 11:02 fedora-x64
drwxrwxr-x 3 andy andy 4096 Jan  7 11:02 win-x64
drwxrwxr-x 3 andy andy 4096 Jan  7 11:02 win-x86

bin/Debug/net5.0/runtimes/alpine-x64:
total 4
drwxrwxr-x 2 andy andy 4096 Jan  7 11:02 native

bin/Debug/net5.0/runtimes/alpine-x64/native:
total 2980
-rwxrw-r-- 1 andy andy  56912 Jan 23  2019 libcsfml-audio.so
-rwxrw-r-- 1 andy andy 207584 Jan 23  2019 libcsfml-graphics.so
-rwxrw-r-- 1 andy andy  16216 Jan 23  2019 libcsfml-system.so
-rwxrw-r-- 1 andy andy  38616 Jan 23  2019 libcsfml-window.so
-rwxrw-r-- 1 andy andy 144552 Jan 23  2019 libsfml-audio.so
-rwxrw-r-- 1 andy andy 144552 Jan 23  2019 libsfml-audio.so.2.5
-rwxrw-r-- 1 andy andy 144552 Jan 23  2019 libsfml-audio.so.2.5.1
-rwxrw-r-- 1 andy andy 477424 Jan 23  2019 libsfml-graphics.so
-rwxrw-r-- 1 andy andy 477424 Jan 23  2019 libsfml-graphics.so.2.5
-rwxrw-r-- 1 andy andy 477424 Jan 23  2019 libsfml-graphics.so.2.5.1
-rwxrw-r-- 1 andy andy  69528 Jan 23  2019 libsfml-system.so
-rwxrw-r-- 1 andy andy  69528 Jan 23  2019 libsfml-system.so.2.5
-rwxrw-r-- 1 andy andy  69528 Jan 23  2019 libsfml-system.so.2.5.1
-rwxrw-r-- 1 andy andy 209456 Jan 23  2019 libsfml-window.so
-rwxrw-r-- 1 andy andy 209456 Jan 23  2019 libsfml-window.so.2.5
-rwxrw-r-- 1 andy andy 209456 Jan 23  2019 libsfml-window.so.2.5.1

bin/Debug/net5.0/runtimes/debian-x64:
total 4
drwxrwxr-x 2 andy andy 4096 Jan  7 11:02 native

bin/Debug/net5.0/runtimes/debian-x64/native:
total 2944
-rwxrw-r-- 1 andy andy  56664 Jan 23  2019 libcsfml-audio.so
-rwxrw-r-- 1 andy andy 204912 Jan 23  2019 libcsfml-graphics.so
-rwxrw-r-- 1 andy andy  15152 Jan 23  2019 libcsfml-system.so
-rwxrw-r-- 1 andy andy  38056 Jan 23  2019 libcsfml-window.so
-rwxrw-r-- 1 andy andy 141888 Jan 23  2019 libsfml-audio.so
-rwxrw-r-- 1 andy andy 141888 Jan 23  2019 libsfml-audio.so.2.5
-rwxrw-r-- 1 andy andy 141888 Jan 23  2019 libsfml-audio.so.2.5.1
-rwxrw-r-- 1 andy andy 471856 Jan 23  2019 libsfml-graphics.so
-rwxrw-r-- 1 andy andy 471856 Jan 23  2019 libsfml-graphics.so.2.5
-rwxrw-r-- 1 andy andy 471856 Jan 23  2019 libsfml-graphics.so.2.5.1
-rwxrw-r-- 1 andy andy  65832 Jan 23  2019 libsfml-system.so
-rwxrw-r-- 1 andy andy  65832 Jan 23  2019 libsfml-system.so.2.5
-rwxrw-r-- 1 andy andy  65832 Jan 23  2019 libsfml-system.so.2.5.1
-rwxrw-r-- 1 andy andy 206704 Jan 23  2019 libsfml-window.so
-rwxrw-r-- 1 andy andy 206704 Jan 23  2019 libsfml-window.so.2.5
-rwxrw-r-- 1 andy andy 206704 Jan 23  2019 libsfml-window.so.2.5.1

bin/Debug/net5.0/runtimes/fedora-x64:
total 4
drwxrwxr-x 2 andy andy 4096 Jan  7 11:02 native

bin/Debug/net5.0/runtimes/fedora-x64/native:
total 2776
-rwxrw-r-- 1 andy andy  62072 Jan 23  2019 libcsfml-audio.so
-rwxrw-r-- 1 andy andy 201760 Jan 23  2019 libcsfml-graphics.so
-rwxrw-r-- 1 andy andy  19384 Jan 23  2019 libcsfml-system.so
-rwxrw-r-- 1 andy andy  43032 Jan 23  2019 libcsfml-window.so
-rwxrw-r-- 1 andy andy 149032 Jan 23  2019 libsfml-audio.so
-rwxrw-r-- 1 andy andy 149032 Jan 23  2019 libsfml-audio.so.2.5
-rwxrw-r-- 1 andy andy 149032 Jan 23  2019 libsfml-audio.so.2.5.1
-rwxrw-r-- 1 andy andy 407864 Jan 23  2019 libsfml-graphics.so
-rwxrw-r-- 1 andy andy 407864 Jan 23  2019 libsfml-graphics.so.2.5
-rwxrw-r-- 1 andy andy 407864 Jan 23  2019 libsfml-graphics.so.2.5.1
-rwxrw-r-- 1 andy andy  71408 Jan 23  2019 libsfml-system.so
-rwxrw-r-- 1 andy andy  71408 Jan 23  2019 libsfml-system.so.2.5
-rwxrw-r-- 1 andy andy  71408 Jan 23  2019 libsfml-system.so.2.5.1
-rwxrw-r-- 1 andy andy 199632 Jan 23  2019 libsfml-window.so
-rwxrw-r-- 1 andy andy 199632 Jan 23  2019 libsfml-window.so.2.5
-rwxrw-r-- 1 andy andy 199632 Jan 23  2019 libsfml-window.so.2.5.1

bin/Debug/net5.0/runtimes/win-x64:
total 4
drwxrwxr-x 2 andy andy 4096 Jan  7 11:02 native

bin/Debug/net5.0/runtimes/win-x64/native:
total 3716
-rwxrw-r-- 1 andy andy 1295360 Jan 23  2019 csfml-Audio.dll
-rwxrw-r-- 1 andy andy 1211392 Jan 23  2019 csfml-Graphics.dll
-rwxrw-r-- 1 andy andy  215040 Jan 23  2019 csfml-System.dll
-rwxrw-r-- 1 andy andy  404992 Jan 23  2019 csfml-Window.dll
-rwxrw-r-- 1 andy andy  669696 Jan 23  2019 openal32.dll

bin/Debug/net5.0/runtimes/win-x86:
total 4
drwxrwxr-x 2 andy andy 4096 Jan  7 11:02 native

bin/Debug/net5.0/runtimes/win-x86/native:
total 3224
-rwxrw-r-- 1 andy andy 1169920 Jan 23  2019 csfml-Audio.dll
-rwxrw-r-- 1 andy andy  988160 Jan 23  2019 csfml-Graphics.dll
-rwxrw-r-- 1 andy andy  177664 Jan 23  2019 csfml-System.dll
-rwxrw-r-- 1 andy andy  325120 Jan 23  2019 csfml-Window.dll
-rwxrw-r-- 1 andy andy  630784 Jan 23  2019 openal32.dll

Anyone?

commented

I've never tried SFML.Net on Linux, but i'm going to assume this error is due to the inconsistent naming of the csfml libraries between Windows and everything else. On Mono, you could use dllmap configuration to map the different names but .NET 5 doesn't have that feature out of the box.

This might be of help to explain how to do something similar on .NET Core/.NET 5
https://github.com/dotnet/runtime/blob/main/docs/design/features/dllmap.md

Thanks, I'll check it out.

I have a similar problem when trying to use audio. I figured out a work around. When I publish the project, it does not include the .so files. I would have to copy them into the directory the executable is located. This makes it work without an issue, but would rather have it point to a runtime folder. I'm going to see if I can configure NativeLibrary APIs for platforms other than windows. I'm using arch linux inside WSL 2 and routing pulseaudio from WSL to windows. I also configured ASLA in WSL to use pulseaudio as a sound device. I will comment later to see if it is working.

commented

Curious, was there any success resolving this?

I've not really returned to it. 😦

The same goes for me with .NET6. Did you find a solution?

commented

For folks having issue on .NET Core, .NET 5 and 6, I think you've got to do some manual dll mapping as the csfml library names are different on non-windows and DllImports are expecting the windows names. There are multiple ways of doing this which can be found in my first comment.

You can try something like this by adding the mapping via the ResolvingUnmanagedDll event. Make sure you its the first thing your application does especially before using SFML. I don't have a linux install to try this so your results may vary however, this is in essence what I think you need to do to get SFML to work on linux without going back to Mono using DllMap which is an alternative option.

using System.Runtime.Loader;
using System.Runtime.InteropServices;

AssemblyLoadContext.Default.ResolvingUnmanagedDll += (Assembly asm, string libname) =>
{
    if(!OperatingSystem.IsWindows())
    {
        return libname switch
        {
            "csfml-system" => NativeLibrary.Load("libcsfml-system"),
            "csfml-window" => NativeLibrary.Load("libcsfml-window"),
            "csfml-graphics" => NativeLibrary.Load("libcsfml-graphics"),
            "csfml-audio" => NativeLibrary.Load("libcsfml-audio"),
            _ => IntPtr.Zero
        };                
    }
    return IntPtr.Zero;
};

Here is also some info on how PInvoke libraries get resolved on .NET Core / 5 that might be of help.
https://docs.microsoft.com/en-us/dotnet/core/dependency-loading/loading-unmanaged

I fixed this by installing the csfml package on my system (sudo pacman -S csfml on arch, but may be different on other systems). After I ran that, it seemed to no longer have any dependency issues.

Here is the github for it if you're curious https://github.com/SFML/CSFML

commented

same for raspberry pi 3: i had to install libcsfml libraries via the "add/remove software" app.

commented

A workaround on Debian based distros (Mint in my case) is running dotnet run --runtime debian-x64 (or dotnet run --runtime debian-x64 --no-self-contained to avoid warnings). I think this is an issue because there is no linux-x64 runtime (only a debian-x64 one) in the CSFML package. But I'm not sure. This would explain why it isn't an issue on Debian. At least, with my tests. I was also able to solve the issue by manually adding the linux-x64 runtime and editing *.deps.json (copied from debian-x64)

I fixed this by installing the csfml package on my system (sudo pacman -S csfml on arch, but may be different on other systems). After I ran that, it seemed to no longer have any dependency issues.

This is working for me in .NET 7.

In my ubuntu machine, I could get my project running with apt install libcsfml-dev

commented

I fixed this by installing the csfml package on my system (sudo pacman -S csfml on arch, but may be different on other systems). After I ran that, it seemed to no longer have any dependency issues.

This is working for me in .NET 7.

In my ubuntu machine, I could get my project running with apt install libcsfml-dev

This does seem to fix the issue for me as well. But I think now it is using the binaries installed on the system and not the binaries that come with the nuget package. So I would still consider this a bug.

@Thraka / @den-nis / @AzuxirenLeadGuy can you give the latest SFML.Net / CSFML version a try?

@eXpl0it3r I can confirm that the package version 2.5.1 is working as expected.

I had a simple project on 2.5.0, and I removed the libcsfml packages in my system, in which case I get the System.DllNotFoundException on running it. On updating my project to 2.5.1, It runs without issues as expected.

Awesome, I will thus close the issue. Might be worth creating a new issue, if someone runs into a problem with Linux