godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.74k stars 576 forks source link

Binding fails on Windows using Ninja + Clang #1250

Open BoHomola opened 1 year ago

BoHomola commented 1 year ago

Godot version

4.1.1 stable

godot-cpp version

4.1.1 stable 1009da4d7e395abadfdb454cff6623e9456181c4

System information

Windows 11

Issue description

When trying to compile godot_cpp + my lib that includes godot_cpp using clang and ninja on Windows 11, when calling

godot::ClassDB::bind_method(...)

I get a compile error

In file included from C:/Users/user/dev/project/GDClient/src/MyClass.h:11: In file included from C:/Users/user/dev/project/build/libs/godot-cpp/gen/include\godot_cpp/classes/engine.hpp:43: In file included from C:/Users/user/dev/project/libs/godot-cpp/include\godot_cpp/core/class_db.hpp:38: C:/Users/user/dev/project/libs/godot-cpp/include\godot_cpp/core/method_bind.hpp:506:49: error: cannot reinterpret_cast from member pointer type 'godot::NodePath (MyClass::)()' to member pointer type 'godot::NodePath (godot::_gde_UnexistingClass::)()' of different size MethodBind a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::)(P...)>(p_method)));

On Linux CMake + Ninja + Clang works flawlessly.

I tried to look where to issue could be occurring. I found out when I remove MSVC condition from CMakeLists.txt:157

from:

target_compile_definitions(${PROJECT_NAME} PUBLIC
    $<$<CONFIG:Debug>:
        DEBUG_ENABLED
        DEBUG_METHODS_ENABLED
    >
    $<${compiler_is_msvc}:
        TYPED_METHOD_BIND
    />
)

to:

target_compile_definitions(${PROJECT_NAME} PUBLIC
    $<$<CONFIG:Debug>:
        DEBUG_ENABLED
        DEBUG_METHODS_ENABLED
    >       
        TYPED_METHOD_BIND
)

This effectively resolved the errors and got a successful build. Although I am not really sure if that fixes things properly and does not create issues elsewhere.

Steps to reproduce

  1. Link godot_cpp to your library
  2. Bind one of your methods using godot::ClassDB::bind_method
  3. Run CMake with build system Ninja and compiler Clang

Minimal reproduction project

N/A

enetheru commented 1 month ago

Note to self: Read linked merge requests before spending the morning looking into an issue.

I have successfully re-produced your error with a fresh download of llvm binaries.

"When Clang compiles C++ code for Windows, it attempts to be compatible with MSVC. There are multiple dimensions to compatibility." MSVCCompatibility

And the associated godotengine issue

adding TYPED_METHOD_BIND to the definitions solves the compile error