Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 07, 2004 calling D from C | ||||
---|---|---|---|---|
| ||||
Is there a way to use D from C? What is the simple yet portable way to do the following: - have an imaginary C/C++ SDK giving a possibility to register a callback function to be called upon some event in SDK. - want to implement the callback functionality in D. - If possible, make the whole thing be thread-safe (If SDK wishes to have several threads calling the callback concurrently). Regards! |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to gbatyan | gbatyan wrote:
> Is there a way to use D from C?
You can declare your functions as "extern(C)",
which makes them callable from regular C too.
You have to be really careful with garbage
collected objects and exceptions, though...
But I've used it for simple GLUT callbacks ?
--anders
PS. Mapping "bit" is left an as an exercise :-)
|
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | In article <cp3u7q$aou$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >gbatyan wrote: > >> Is there a way to use D from C? > >You can declare your functions as "extern(C)", >which makes them callable from regular C too. > >You have to be really careful with garbage >collected objects and exceptions, though... > >But I've used it for simple GLUT callbacks ? > >--anders > >PS. Mapping "bit" is left an as an exercise :-) what about type conversion, in a D function exported to C - am I able to use D structs and dynamic arrays? How should those be declared in C? How to cope with the fact that C and D may use different alignments in structs? P.S., excuse me, :-) where do I find the article how exactly this extern (C) syntax looks like? I mean Not extern (C) when importing c functions into d, but instead for exporting d functions to c? |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to gbatyan | gbatyan wrote: > what about type conversion, in a D function exported to C - am I able > to use D structs and dynamic arrays? Not really arrays, since they are garbage collected in D... But the definition is something like { int len; void *data; } Structs should be more or less similar, though ? > How should those be declared in C? How to cope with the fact that C and D > may use different alignments in structs? You will have to explicitly align them then. > P.S., excuse me, :-) where do I find the article how exactly this extern (C) > syntax looks like? I mean Not extern (C) when importing c functions into d, but > instead for exporting d functions to c? It's the same syntax :-) C linkage is C linkage. It mostly affects the function name "mangling". --anders |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | In article <cp420a$hf6$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >gbatyan wrote: > >> what about type conversion, in a D function exported to C - am I able to use D structs and dynamic arrays? > >Not really arrays, since they are garbage collected in D... But the definition is something like { int len; void *data; } > >Structs should be more or less similar, though ? > >> How should those be declared in C? How to cope with the fact that C and D may use different alignments in structs? > >You will have to explicitly align them then. > >> P.S., excuse me, :-) where do I find the article how exactly this extern (C) >> syntax looks like? I mean Not extern (C) when importing c functions into d, but >> instead for exporting d functions to c? > >It's the same syntax :-) C linkage is C linkage. >It mostly affects the function name "mangling". > excuse me for endless newbieness, but where can I read about name mangling? is name mangling standardised across D implementations? Regards! |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to gbatyan | gbatyan wrote: >>It's the same syntax :-) C linkage is C linkage. >>It mostly affects the function name "mangling". > > excuse me for endless newbieness, but where can I read about name mangling? > is name mangling standardised across D implementations? I'm not sure if name mangling in D is either documented or even standard among D compilers, but you can read more about mangling in general: http://en.wikipedia.org/wiki/Name_mangling Looking at the disassembled output for some D code should give you examples of what D does ? --anders |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | "Anders F Björklund" <afb@algonet.se> wrote in message news:cp420a$hf6$1@digitaldaemon.com... > > How should those be declared in C? How to cope with the fact that C and D > > may use different alignments in structs? > You will have to explicitly align them then. The default struct alignment in D will match that of C's default alignment. |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | "Anders F Björklund" <afb@algonet.se> wrote in message news:cp4v4t$223h$3@digitaldaemon.com... > gbatyan wrote: > > >>It's the same syntax :-) C linkage is C linkage. > >>It mostly affects the function name "mangling". > > > > excuse me for endless newbieness, but where can I read about name mangling? > > is name mangling standardised across D implementations? > > I'm not sure if name mangling in D is either > documented or even standard among D compilers, > but you can read more about mangling in general: > http://en.wikipedia.org/wiki/Name_mangling > > Looking at the disassembled output for some D > code should give you examples of what D does ? No need to worry about name mangling, as declaring functions with extern(C) will cause them to match C's name mangling. |
December 07, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: >>I'm not sure if name mangling in D is either >>documented or even standard among D compilers, >>but you can read more about mangling in general: >>http://en.wikipedia.org/wiki/Name_mangling >> >>Looking at the disassembled output for some D >>code should give you examples of what D does ? > > No need to worry about name mangling, as declaring functions with extern(C) > will cause them to match C's name mangling. Right, "extern (C)" avoids most of the mangling. Is there a link to the names / ABI used for D ? (when declaring them as the usual, "extern(D)") Or is not supported to call D routines from C ? (as there is plenty of issues with that anyway) I could only find the page with mostly "TBD", i.e. http://www.digitalmars.com/d/abi.html Not that it matters much, to the casual D developer. I guess it's only useful for disassembly/debugging... --anders |
December 08, 2004 Re: calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | "Anders F Björklund" <afb@algonet.se> wrote in message news:cp56qd$2fhg$1@digitaldaemon.com... > Is there a link to the names / ABI used for D ? > (when declaring them as the usual, "extern(D)") I should write one, but at the moment all there is is the source code for the mangler, mangle.c. > Or is not supported to call D routines from C ? > (as there is plenty of issues with that anyway) It's not officially supported, though if you know what you're doing, you can make it work. But no need to bother, that's what the C interface support is for. |
Copyright © 1999-2021 by the D Language Foundation