Function 在Dlang中传递函数指针
我正在尝试用Dlang运行OpenGL示例Function 在Dlang中传递函数指针,function,d,Function,D,我正在尝试用Dlang运行OpenGL示例 void onError(整型代码,常量(字符)*文本)nothrow { } 用法: glfwSetErrorCallback(&onError); 绑定代码: 共享{ da_glfwSetErrorCallback glfwSetErrorCallback; ... 外间(C)@nogc nothrow{ 别名da_glfwSetErrorCallback=GLFWerrorfun函数(GLFWerrorfun); ... 别名glfwrorf
void onError(整型代码,常量(字符)*文本)nothrow
{
}
用法:
glfwSetErrorCallback(&onError);
绑定代码:
共享{
da_glfwSetErrorCallback glfwSetErrorCallback;
...
外间(C)@nogc nothrow{
别名da_glfwSetErrorCallback=GLFWerrorfun函数(GLFWerrorfun);
...
别名glfwrorfun=void函数(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来自调用上的:
D可以很容易地调用C回调(函数指针),如果回调是extern(C)函数或双方同意的其他链接(例如extern(Windows)),C可以调用D代码提供的回调
因此,我认为您需要将
onError
函数声明为extern(C)
,以便它与类型签名匹配。您需要在D代码中声明函数原型:
//函数原型
外部(C)无效的一个错误(int,const(char)*)nothrow
外部(C)无效onError(内部代码,常量(字符)*文本)nothrow
{
//你的代码
}
请注意,在原型中只有类型