godot-cpp
godot-cpp copied to clipboard
Undefined reference to Object::cast_to<T> when using Ref<T>
Depending on compiler and level of optimization something like this:
#include <Ref.hpp>
#include <SpatialMaterial.hpp>
// [...]
Ref<SpatialMaterial> mat = SpatialMaterial::_new();
can cause an error either during linking, or when loading the compile shared library along those lines:
ERROR: open_dynamic_library: Can't open dynamic library: <...>/libmygame.so. Error: <...>/libmygame.so: undefined symbol: _ZN5godot6Object7cast_toINS_15SpatialMaterialEEEPT_PKS0_
At: drivers/unix/os_unix.cpp:426.
ERROR: get_symbol: No valid library handle, can't get symbol from GDNative object
At: modules/gdnative/gdnative.cpp:501.
ERROR: init_library: No nativescript_init in "res://bin/x11/libmygame.so" found
At: modules/gdnative/nativescript/nativescript.cpp:1506.
The code compiles "just fine" most times, just in some situations (e.g. compiling with -O3) linking or loading the lib will fail. Not even the linking issue is consistent - but it will fail latest when godot tries to load the library.
This seems to be caused by a (more or less) circular dependency between Ref.hpp, Object.hpp and Godot.hpp.
-
Ref.hppusesObject::cast_to<T>()here: https://github.com/godotengine/godot-cpp/blob/master/include/core/Ref.hpp#L91 -
Object::cast_to<T>()is declared inObject.hpp(generated) -
Object::cast_to<T>()is defined inGodot.hpphere: https://github.com/godotengine/godot-cpp/blob/master/include/core/Godot.hpp#L545 -
Godot.hppincludesRef.hpp
This means, doing this will compile and link just fine:
#include <Godot.hpp>
#include <SpatialMaterial.hpp>
// [...]
Ref<SpatialMaterial> mat = SpatialMaterial::_new();
But this won't:
#include <Ref.hpp>
#include <SpatialMaterial.hpp>
// [...]
Ref<SpatialMaterial> mat = SpatialMaterial::_new();
I'm not sure that Godot.hpp even uses Ref<T> anywhere - is this include intentional?