bmk icon indicating copy to clipboard operation
bmk copied to clipboard

Dll's created in Bmax can only be used in Bmax. (missing static lib)

Open Kerntrick opened this issue 1 year ago • 0 comments

Most of this description is a copy of what I posted here: https://discord.com/channels/613699895139762176/679291789840089108/1111856194957627412

I've found a bug with either maxgui, or generating Dll's on windows. It seems that if you import maxgui.drivers in a dll, only Bmax can load the dll. Loading by any other non-bmax program will give an error. (126) I think it's related to a missing library.

Example Here is a simple dll source in bmax. : https://gist.github.com/Kerntrick/99b1ff8762b1cd847b868733989958c9 Here is a simple C file to load the dll and catch the error. : https://gist.github.com/Kerntrick/f14af49ca175da229d1f94a4b93bbd99

I think the libstdc is not being statically linked as it should. but I'm not a C guy. image

Doing some research, it seems that adding -static-libstdc++ will not help, as that is not the linking operation.

I've tried adding different options to maxgui.drivers, but nothing seemed to help.

ModuleInfo "CC_OPTS: -static" 
ModuleInfo "CC_OPTS: -Bstatic -lstdc++" 
import "-lstdc++" 
Import "D:\Dev\Lang\BmaxNG\MinGW32x64\lib\gcc\x86_64-w64-mingw32\8.1.0\libstdc++.a"  
Import "D:\Dev\Lang\BmaxNG\MinGW32x64\x86_64-w64-mingw32\lib\libwinpthread.a"

I did get it working (i think) by doing 2 things.

  1. I removed the file libstdc++.dll.a from the mingw dir.

This should force the compiler to really statically link the lib . The second thing is that I had to declare a function from that lib in the dll to keep the linker from skipping it.

Extern
    Function nanosleep%( req:Byte Ptr, timespec:Byte Ptr)
EndExtern

Then in the dll main function I call this func.

  1. After getting stdc++ to import, the dll failed to work because of a missing reference to libwinpthread .

So I import that lib directly in the dll.

Import "D:\Dev\Lang\BmaxNG\MinGW32x64\x86_64-w64-mingw32\lib\libwinpthread.a"

Here is the modifed example bmax code that I was able to make working. https://gist.github.com/Kerntrick/2fa0e359304c8bbb1ee6e092b90b9c00

Creating a dll that imports maxgui.drivers, seems to work now with these hacks, but I don't know yet what else I may have broken by removing that lib in mingw.

Kerntrick avatar May 27 '23 20:05 Kerntrick