godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

Undefined reference to Object::cast_to<T> when using Ref<T>

Open founderio opened this issue 4 years ago • 0 comments

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.hpp uses Object::cast_to<T>() here: https://github.com/godotengine/godot-cpp/blob/master/include/core/Ref.hpp#L91
  • Object::cast_to<T>() is declared in Object.hpp (generated)
  • Object::cast_to<T>() is defined in Godot.hpp here: https://github.com/godotengine/godot-cpp/blob/master/include/core/Godot.hpp#L545
  • Godot.hpp includes Ref.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?

founderio avatar Mar 28 '21 12:03 founderio