Declaring a D pointer to a C function

Marco Cosentino cosentino.ma at gmail.com
Tue Jul 12 03:55:22 PDT 2011


On 12/07/2011 11:36, Johannes Pfau wrote:
>  From a discussion related to derelict:
> How do you write this:
> -------------------------------------------------------
> alias extern(C) int function(void* test) FTInitFunc;
> FTInitFunc FT_Init_FreeType
> -------------------------------------------------------
> without the alias?
> -------------------------------------------------------
> extern(C) int function(void* test) FT_Init_FreeType;
> -------------------------------------------------------
> is not the same!
> both are fields containing a C function pointer, but the first field
> has D name mangling (_D4test16FT_Init_FreeTypePUPvZi) and the second
> has C name mangling: (FT_Init_FreeType, which conflicts with the C
> function FT_Init_FreeType)
>
> And a related question from stackoverflow:
> (http://stackoverflow.com/questions/6257078/casting-clutteractor-to-clutterstage)
> How to write this:
> -------------------------------------------------------
> alias extern(C) void function(void*, const char*) setTitleFunc;
> auto clutter_stage_set_title =
> getSym!(setTitleFunc)("clutter_stage_set_title");
> -------------------------------------------------------
> without the alias?
>
> http://d.puremagic.com/issues/show_bug.cgi?id=2168 and
> http://d.puremagic.com/issues/show_bug.cgi?id=4288 seem to be related,
> extern(C) seems to work almost nowhere ;-)
>
>

You should declare the function pointer without the "extern(C)".

Example:
alias int function(void* test) FTInitFunc;

extern(C) int foo(void* test){ .... }

FTInitFunc foo_ptr = &foo;

This worked for me.


More information about the Digitalmars-d-learn mailing list