godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.71k stars 573 forks source link

Can not bind GDNative compiled with CMakeLists in 3.3 and 3.x via Mingw-w64 (Windows) #673

Closed Banderi closed 1 year ago

Banderi commented 2 years ago

Repo branch: 3.3 (and a few others) Godot version: 3.3.stable

After lots of trial and errors, comparing multiple guides, docs, tutorials, different versions, different toolchains and different C++ standards I managed to make godot-cpp compile and generate the required files & bindings on Windows with the 3.3 (and some others) branches by using the included CMakeLists.txt instead of SCons, and the linked commits of godot-headers; the GDNative project also compiles successfully, and generates a .dll. However, no matter what I try, trying to bind the dll in Godot results in the extremely obnoxious Windows "Error 126" which doesn't give back any kind of hint as to why.

This is the error as appears in one of my projects, https://github.com/Banderi/EMWaves/tree/master/CLionCPP and compiled on CLion with Mingw-w64 9.0/GCC 11.2.0 on C++14:

ERROR: Can't open dynamic library: E:/Godot/Projects/EMWaves/Godot/gdnative_cpp/windows/libwavescpp.dll, error: Error 126: The specified module could not be found.

.
   At: platform/windows/os_windows.cpp:2425
ERROR: get_symbol: No valid library handle, can't get symbol from GDNative object
   At: modules/gdnative/gdnative.cpp:502
ERROR: init_library: No nativescript_init in "res://gdnative_cpp/windows/libwavescpp.dll" found
   At: modules/gdnative/nativescript/nativescript.cpp:1479

This error is extremely deceiving and frustrating because it's a generic error message that Windows throws when the .dll fails to load, NOT (necessarily) because the file is not found; it appears many many times over various threads on Discord, Reddit, Github, the forums etc. and it's incredibly rare to find a solution for it because it can happen for a multitude of different reasons. (e,g, https://github.com/godotengine/godot-cpp/issues/540 https://github.com/godotengine/godot-cpp/issues/403 https://github.com/godotengine/godot-cpp/issues/232 https://github.com/godotengine/godot-cpp/issues/14)

Here's what I made 100% sure of, in my project's case:

As I notice 3.x is the closest to being working, I suppose that's the one I should focus on if there's anything else to try. Here's my full project with all the files: https://github.com/Banderi/EMWaves It contains both the C++ attempts (CLionCPP) and a plain C one (CLion) which loads and works as intended.

Just to be safe, here's the steps I took to build the repos & libraries:

Also I partially followed this handy guide: https://aleksandrbazhin.github.io/godot/2021/06/25/GDNative-cpp-in-2k21.html

Any clue as to what could be the cause of all these issues? Maybe I made some obvious, dumb mistake in the above steps, but I genuinely wouldn't know. As far as I can tell, the libraries all compile correctly... that's the extent of my knowledge.

pinting commented 2 years ago

I am in a similar shoes. I would like to use a GDNative module compiled by GCC of MingGW, but the DLL fails to load. When compiling with MSVC, it loads successfully. I am using Godot v3.4.2.stable.official [25eaa2daf] with godot-cpp godot-3.4.2-stable and godot-headers godot-3.4.2-stable.

The error message in Godot is the following:

E 0:00:00.635   open_dynamic_library: Can't open dynamic library: C:/Users/pinting/Dropbox/ipc-test/godot-integration/project/gdnative/windows.mingw64/msys-gdrackclient.dll, error: Error 126: The specified module could not be found.
.
  <C++ Error>   Condition "!p_library_handle" is true. Returned: ERR_CANT_OPEN
  <C++ Source>  platform/windows/os_windows.cpp:2353 @ open_dynamic_library()

Both DLL have the following export functions, but the MingGW one have a bunch of others too (see the attached MINGW64 build export functions.txt file).

godot_gdnative_init
godot_gdnative_terminate
godot_nativescript_init

Google Drive link with the built DLLs

SConstruct of the top module.txt SConstruct of the godot-cpp submodule.txt Installed packages.txt MINGW64 build export functions.txt MSVC build export functions.txt

Faless commented 1 year ago

Closing as fixed in 3.5 via godotengine/godot#62173 . Please leave a comment if you are still encountering this issue after updating godot-cpp to the latest 3.5 branch so we can reopen it in case.