giawa/opengl4csharp

Unable to run on Linux with Mono

TheRealNOIG opened this issue · 8 comments

Unable to run my program in Linux using mono.
I get an exception that OpenGL cannot find systemDLL opnegl32.dll

/Debug$ mono Test2.exe
Creating OpenGL instance

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'OpenGL.Gl' threw an exception. ---> System.DllNotFoundException: opengl32.dll
  at (wrapper managed-to-native) OpenGL.Gl+NativeMethods.glxGetProcAddress(string)
  at OpenGL.Gl+GetProcAddressX11.GetProcAddress (System.String function) [0x00000] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
  at OpenGL.Gl.GetAddress (System.String function) [0x0005a] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
  at OpenGL.Gl.GetExtensionDelegate (System.String name, System.Type signature) [0x00000] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
  at OpenGL.Gl.GetDelegate (System.String name, System.Type signature) [0x00000] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
  at OpenGL.Gl.ReloadFunctions () [0x0007b] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
  at OpenGL.Gl..cctor () [0x0007f] in <118e14bf4e6e43458e7031331eb2b4d0>:0 
   --- End of inner exception stack trace ---
  at OpenGL.Platform.Window.CreateContextFromWindow (System.IntPtr window, System.Boolean fullscreen) [0x00049] in <1a9c11f978ab4564b09d050cd056057e>:0 

giawa commented

Can you make sure that the OpenGL.dll.config file is alongside OpenGL.dll? OpenGL.dll.config contains the information that Mono needs to map the calls to libGL.so.1 on Linux.

Yes OpenGL.dll.config is alongside OpenGL.dll.

My mono compiler version version is 5.12.0.301

giawa commented

Very strange. I can tell that mono is still trying to look for opengl32.dll (which is the wrong library to use) due to the error message in your exception here: "System.DllNotFoundException: opengl32.dll". So it seems like that .dll.config file is not being used properly. Unfortunately, I don't have a Linux installation right now, so I am unable to debug this issue. Do you have any time to try to debug what is going on?

giawa commented

I'm going to try installing a Linux VM, but it will likely be a day or two before I have time to address this. Sorry for the delay, and definitely let me know if you find a work around/solution. Thanks!

I fixed this problem by changing the dll map to target libGL.so.1.
Because according to https://www.mono-project.com/docs/advanced/pinvoke/dllmap/

... The <dllmap> element takes two attributes:

dll: This should be the same string used in the DllImport attribute. This can be prefixed with i: to specify that name matching should be done in a case-insensitive fashion.
target: This should be the name of the library where the function can be found: this name should be suitable for use with the platform native shared library loading routines (dlopen etc.).

Here are the changes I made to the config file

<configuration>
	<dllmap dll="opengl32.dll">
  		<dllmap os="linux" target="libGL.so.1"/>
  		<dllentry os="windows" target="opengl32.dll" />
  		<dllentry os="osx" target="opengl32.dll" />
  	</dllmap>
</configuration>

But im now getting this error

Creating OpenGL instance
Stacktrace:

  at <unknown> <0xffffffff>
  at OpenGL.Gl.GetAddress (string) [0x000cd] in <67e44ecdff4b47748523e7986adaa0e4>:0
  at OpenGL.Gl.GetExtensionDelegate (string,System.Type) [0x00000] in <67e44ecdff4b47748523e7986adaa0e4>:0
  at OpenGL.Gl.GetDelegate (string,System.Type) [0x00000] in <67e44ecdff4b47748523e7986adaa0e4>:0
  at OpenGL.Gl.ReloadFunctions () [0x00034] in <67e44ecdff4b47748523e7986adaa0e4>:0
  at OpenGL.Gl..cctor () [0x0009c] in <67e44ecdff4b47748523e7986adaa0e4>:0
  at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) [0x0001e] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
  at <unknown> <0xffffffff>
  at OpenGL.Platform.Window.CreateContextFromWindow (intptr,bool) [0x00049] in <1a9c11f978ab4564b09d050cd056057e>:0
  at OpenGL.Platform.Window.CreateWindow (string,int,int,bool) [0x00091] in <1a9c11f978ab4564b09d050cd056057e>:0
  at GzreEngine.Engine.Core.Window..ctor () [0x00006] in <cfbb2c895f9e436d9d2e12a323dba916>:0
  at GzreEngine.Program.Main (string[]) [0x00010] in <cfbb2c895f9e436d9d2e12a323dba916>:0
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) [0x0004e] in <cfbb2c895f9e436d9d2e12a323dba916>:0

BTW the program successfully creates the window but crashes before the screen is cleared.
Image: https://imgur.com/a/sA13gV3

I fixed the issue. I don't know if this also works for mac as I don't have one to test it on. I also haven't tested this on Windows as I need to head to work now.

<configuration>
	<dllmap dll="opengl32.dll">
		<dllentry os="linux" dll="libGL.so.1" />
		<dllentry os="osx" target="opengl32.dll" />
		<dllentry os="windows" target="opengl32.dll" />
	</dllmap>
</configuration>

Made a change to osx and Widnows to point to dll not target. My test on windows and Linux work as expect. I have created a merge request to get these changes added to the repo. #30

giawa commented

Okay, so I think it was just a typo in the dll.config file, correct? dllentry should have been dllmap? Thanks for taking a look!