godotengine / godotengine/godot-cpp

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

Open
#540 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

Depending on compiler and level of optimization something like this:
```cpp
#include
#include
// [...]
Ref 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()` here: https://github.com/godotengine/godot-cpp/blob/master/include/core/Ref.hpp#L91
* `Object::cast_to()` is declared in `Object.hpp` (generated)
* `Object::cast_to()` 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:
```cpp
#include
#include
// [...]
Ref mat = SpatialMaterial::_new();
```

But this won't:
```cpp
#include
#include
// [...]
Ref mat = SpatialMaterial::_new();
```

I'm not sure that `Godot.hpp` even uses `Ref` anywhere - is this include intentional?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.