包装 C++ 时不完整类型 GLFWwindow class

incomplete type GLFWwindow when wrapping a C++ class

我正在尝试从 Python 中的 Application C++ 基础 class 和 Boost::Python 派生,并且正在努力包装 GLFW 回调,特别是由于 GLFWwindow* window 参数。

这是 MCVE:

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <boost/python.hpp>

class Application
{
public:
    Application();
    GLFWwindow* window;
    virtual int onKeyPressed(GLFWwindow*, int, int, int , int);
};

Application *app;

Application::Application()
{
    glfwInit();
    window = glfwCreateWindow(800.0, 600.0, "example", NULL, NULL);
    glfwMakeContextCurrent(window);
}

int Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    return 1;
}

struct ApplicationWrap : Application, boost::python::wrapper<Application>
{
    int onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
    {
        return this->get_override("onKeyPressed")(window, key, scancode, action, mods);
    }
};

static void _onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    app->onKeyPressed(window, key, scancode, action, mods);
}


int main() {
    // app = new Application();
    // glfwSetKeyCallback(app->window, _onKeyPressed);
    // delete app;
    return 0;
}


BOOST_PYTHON_MODULE(example)
{
    namespace python = boost::python;

    python::class_<ApplicationWrap, boost::noncopyable>("Application")
    //.def("onKeyPressed", _onKeyPressed).staticmethod("_onKeyPressed")
    ;
}

// import example
// class DerivedApplication(example.Application):
//     def __init__(self):
//         example.Application.__init__(self)
//     def onKeyPressed(window):
//         print("successfully overrides example.Application.onKeyPressed.")

// DerivedApplication()

编译:

g++ -I/usr/include/python3.7 main.cpp -lglfw -lpython3.7 -lboost_python3

error: invalid use of incomplete type ‘struct GLFWwindow’ typeid(T) ^~~~~~~~~

note: forward declaration of ‘struct GLFWwindow’ typedef struct GLFWwindow GLFWwindow;

感谢您提供有关如何解决此问题的任何反馈。

错误来自 template <class T> inline type_info type_id() 通过...

template <class T>
struct registered_pointee
    : registered<
        typename boost::python::detail::remove_pointer<  // <-- fails for incomplete types
           typename boost::python::detail::remove_cv<
              typename boost::python::detail::remove_reference<T>::type
           >::type
        >::type
    >
{
};

...最初来自 pointer_deep_arg_to_python<Ptr>::pointer_deep_arg_to_python(Ptr x).

要将 GLFWwindow * 注册为 opaque pointer,请在 #include 之后插入以下内容。

BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)