在Dlang中传递函数指针

格里高利

我正在尝试使用Dlang运行OpenGL示例。

void onError(int code, const(char)* text) nothrow
{
}

用法:

glfwSetErrorCallback(&onError);

绑定代码:

__gshared {

    da_glfwSetErrorCallback glfwSetErrorCallback;
    
    ...

extern( C ) @    nogc nothrow {

    alias da_glfwSetErrorCallback = GLFWerrorfun function( GLFWerrorfun );

    ...

    alias GLFWerrorfun = void function( int, const( char )* );

而且我得到以下编译器错误:

Error: function pointer glfwSetErrorCallback (extern (C) void function(int, const(char)*) nothrow) is not callable using argument types (void function(int code, const(char)* text) nothrow)

编译器:2.065.0

迈克·休斯顿

接口到C回调规范

D可以轻松地调用C回调(函数指针),并且如果回调是extern(C)函数或双方都同意的某些其他链接(例如extern(Windows)),则C可以调用D代码提供的回调。

因此,我认为您需要将onError函数声明为extern(C),以便使其与类型签名匹配。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章