Declaring a D pointer to a C function

Johannes Pfau spam at example.com
Tue Jul 12 06:42:22 PDT 2011


Steven Schveighoffer wrote:
>On Tue, 12 Jul 2011 05:36:15 -0400, Johannes Pfau <spam at example.com>
>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?
>
>extern(C) extern(C) maybe?  :)
>
>or maybe:
>
>extern(C) int function(void * test) extern(C) FT_Init_FreeType;

Nope, that doesn't work:
---------------------------------
test.d(3): no identifier for declarator int C function(void* test)
test.d(3): semicolon expected, not 'extern'
test.d(3): no identifier for declarator FT_Init_FreeType
---------------------------------

also, if that worked, shouldn't it be equal to this?
---------------------------------
extern(C) int function(void* test) FT_Init_FreeType;
---------------------------------
This works, but it's not what I want.
Derelict needs a _field_, FT_Init_FreeType with D name mangling. So the
usual {module}.{module}.FT_Init_FreeType in mangled form.
This _field_ should contain a pointer to a extern(C) function.

This code with alias does just that:
>> -------------------------------------------------------
>> alias extern(C) int function(void* test) FTInitFunc;
>> FTInitFunc FT_Init_FreeType
>> -------------------------------------------------------
but it's not practical for derelict to include aliases for all
functions, so the above must be accomplished without the alias.

I think it should be something like this:
---------------------------------
extern(C) int function(void * test) extern(D) FT_Init_FreeType;
(extern(C) int function(void * test)) FT_Init_FreeType;
extern(C)(int function(void * test)) FT_Init_FreeType;
extern(C){int function(void * test)} FT_Init_FreeType;
---------------------------------
None of these work, though.

>Just some thoughts, it probably doesn't work.
>
>-Steve


-- 
Johannes Pfau



More information about the Digitalmars-d-learn mailing list