2014-07-26 85 views
5

我想用Dlang運行OpenGL示例。在Dlang中傳遞函數指針

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

回答

6

interfacing to C guidelines的回調:

D can easily call C callbacks (function pointers), and C can call callbacks provided by D code if the callback is an extern(C) function, or some other linkage that both sides have agreed to (e.g. extern(Windows)).

所以我認爲你需要你的onError功能,以便爲它的類型簽名匹配聲明爲extern(C)

+0

謝謝!現在它工作了! – Grigory