2014-07-26 7 views
5

Sto provando a eseguire l'esempio OpenGL con Dlang.Passaggio del puntatore alla funzione in Dlang

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

Usage:

glfwSetErrorCallback(&onError); 

codice vincolante:

__gshared { 

    da_glfwSetErrorCallback glfwSetErrorCallback; 

    ... 

extern(C) @ nogc nothrow { 

    alias da_glfwSetErrorCallback = GLFWerrorfun function(GLFWerrorfun); 

    ... 

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

e ottengo il seguente errore del compilatore:

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) 

Compiler: 2.065.0

risposta

6

Dal interfacing to C guidelines il callback:

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)).

Quindi penso che è necessario la funzione onError essere dichiarate come extern(C) in modo che esso corrisponda al tipo di firma.

+0

Grazie! Ora funziona! – Grigory