c++sconsgodot4

Godot 4.1 How to load shared library with multiple C++ sources in GDExtension


I am working off the Summator example: https://github.com/paddy-exe/GDExtensionSummator

I have added a new C++ source, called Cppkinematics. I placed it and the orginal source Summator under a new name for the shared library: godotcppsharedlib. I can call Cppkinematics in GDScript, but the original source Summator does not work from the shared library. GDScript shows error: "Identifier Summator not declared in the current scope" in the scene script.

Here are the files I believe probably contain the culprit issue:

game/bin/godotcppsharedlib.gdextension:

[configuration]
entry_symbol = "godotcppsharedlib_library_init"
compatibility_minimum = 4.1
[libraries]
windows.x86_64.debug = "res://bin/libgdgodotcppsharedlib.windows.template_debug.x86_64.dll"
windows.x86_64.release = "res://bin/libgdgodotcppsharedlib.windows.template_release.x86_64.dll"

game.godot\extension_list.cfg

res://bin/godotcppsharedlib.gdextension

extension/src/register_types.cpp

#include "register_types.h"
#include "summator.h"
#include "cppkinematics.h"
#include <gdextension_interface.h>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>
using namespace godot;
void initialize_summator_types(ModuleInitializationLevel p_level)
{
    if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }
    ClassDB::register_class<Summator>();
}
void uninitialize_summator_types(ModuleInitializationLevel p_level) {
    if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }
}
void initialize_cppkinematics_types(ModuleInitializationLevel p_level)
{
    if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }
    ClassDB::register_class<Cppkinematics>();
}
void uninitialize_cppkinematics_types(ModuleInitializationLevel p_level) {
    if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }
}
extern "C"
{
    GDExtensionBool GDE_EXPORT godotcppsharedlib_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
        GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);
        init_obj.register_initializer(initialize_summator_types);
        init_obj.register_terminator(uninitialize_summator_types);
        init_obj.register_initializer(initialize_cppkinematics_types);
        init_obj.register_terminator(uninitialize_cppkinematics_types);
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
        return init_obj.init();
    }
}

extension\src\summator.cpp and extension\src\summator.h are unchanged from Summator example

extension/src/register_types.h:

#ifndef SUMMATOR_REGISTER_TYPES_H
#define SUMMATOR_REGISTER_TYPES_H
void initialize_summator_types();
void uninitialize_summator_types();
#endif // SUMMATOR_REGISTER_TYPES_H
#ifndef CPPKINEMATICS_REGISTER_TYPES_H
#define CPPKINEMATICS_REGISTER_TYPES_H
void initialize_cppkinematics_types();
void uninitialize_cppkinematics_types();
#endif // CPPKINEMATICS_REGISTER_TYPES_H

SConstruct:

#!/usr/bin/env python
import os
import sys
env = SConscript("godot-cpp/SConstruct")
env.Append(CPPPATH=["extension/src/"])
sources = Glob("extension/src/*.cpp")
if env["platform"] == "macos":
    library = env.SharedLibrary(
        "game/bin/libgdgodotcppsharedlib.{}.{}.framework/godotcppsharedlib.{}.{}".format(
            env["platform"], env["target"], env["platform"], env["target"]
        ),
        source=sources,
    )
else:
    library = env.SharedLibrary(
        "game/bin/libgdgodotcppsharedlib{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
    )
Default(library)

Solution

  • Your GDExtension can only register one initializer and terminator for the extension. Registering a second will overwrite the ones you registered before. However you can call multiple different functions in these functions if you want to organize your types like that.

    #include "register_types.h"
    #include "summator.h"
    #include "cppkinematics.h"
    #include <gdextension_interface.h>
    #include <godot_cpp/core/class_db.hpp>
    #include <godot_cpp/core/defs.hpp>
    #include <godot_cpp/godot.hpp>
    using namespace godot;
    void initialize_types(ModuleInitializationLevel p_level)
    {
        initialize_summator_types(p_level);
        initialize_cppkinematics_types(p_level);
    }
    void uninitialize_types(ModuleInitializationLevel p_level)
    {
        uninitialize_summator_types(p_level);
        uninitialize_cppkinematics_types(p_level);
    }
    void initialize_summator_types(ModuleInitializationLevel p_level)
    {
        if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
            return;
        }
        ClassDB::register_class<Summator>();
    }
    void uninitialize_summator_types(ModuleInitializationLevel p_level) {
        if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
            return;
        }
    }
    void initialize_cppkinematics_types(ModuleInitializationLevel p_level)
    {
        if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
            return;
        }
        ClassDB::register_class<Cppkinematics>();
    }
    void uninitialize_cppkinematics_types(ModuleInitializationLevel p_level) {
        if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
            return;
        }
    }
    extern "C"
    {
        GDExtensionBool GDE_EXPORT godotcppsharedlib_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
            GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);
            init_obj.register_initializer(initialize_types);
            init_obj.register_terminator(uninitialize_types);
    init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
            return init_obj.init();
        }
    }